gists/bookmark_manager_script.html

Bookmark Manager Script

This script can be useful if you use multiple browsers and want one common bookmark manager.

It uses fuzzel to show a popup where bookmarks can be fuzzy found and opened. It uses the $BROWSER environment variable to select default browser but it can be overridden by selection in the menu or by modifying this script.

Choices for adding bookmarks or editing the bookmarks file are in the popup.

The BM variable in the script sets the path to the bookmark file. Default value is ~/.bookmarks. The format used in the bookmark-file is <URL> <TITLE OF BOOKMARK> on each line.

It should be fairly easy to modify to your needs.

Currently this script is written in Bash but perhaps some time in a not so distant future I will rewrite it i Raku.

#!/usr/bin/env bash
#
# bookmarks - a simple bookmark finder using fuzzel
#
# Dependencies:
# - fuzzel
# - foot
# - wl-clipboard (wl-paste)
# - notify-send
# - $EDITOR
# - $BROWSER
#

# Bookmark file
BM="$HOME/.bookmarks"
# Available browsers
BROWSERS="zen\nbrave\nqutebrowser\nlibrewolf"
# Search engine prefix
SEARCH_PREFIX="https://duckduckgo.com/?q="

# Override default browser
if [[ "$HOSTNAME" == "grov" ]]; then
  BROWSER="zen"
fi
if [[ "$HOSTNAME" == "tetsuo" ]]; then
  BROWSER="brave"
fi

# Override default browser
if [[ "$1" != "" ]]; then
  BROWSER="$1"
fi

# Concatenate bookmarks files and make list uniqe
input=$(cat "$BM" | sort -k2 | uniq)

# Switch order so URL is the last word
putin=$(while read -ra line; do
  url="${line[0]}"
  name="${line[*]:1}"
  if [[ "$name" == "" ]]; then
    name="?"
  fi
  printf "%s -> %s\n" "$name" "$url" 
done<<<"$input")

cmds="<search>\n<add>\n<edit>\n<browser>\n"
cmds="${cmds}-------------------------------------------------------------------------------"

selection=$(printf "$cmds\n%s\n" "$putin" | fuzzel --dmenu --counter --auto-select --width=80 --prompt="bookmarks ($BROWSER) > ")

case "$selection" in
    "") exit ;;
    "----------"*) exit ;;
    "<search>")
      terms=$(fuzzel --dmenu --width=120 --prompt-only=" search > ")
      if [[ "$terms" == "" ]]; then
        exit
      fi
      terms=${terms/ /+}
      "$BROWSER" "$SEARCH_PREFIX$terms"
      ;;
    "<add>")
      url=$(wl-paste)
      if [[ "$url" != "http"* ]]; then
        notify-send "Error" "No URL in clipboard"
        exit
      fi
      name=$(fuzzel --dmenu --width=120 --prompt-only=" ${url:0:80} > ")
      if [[ "$name" == "" ]]; then
        exit
      fi
      echo "$url $name" >> "$BM"
      ;;
    "<edit>")
      foot --title bookmarks -e "$EDITOR" "$BM" &
      ;;
    "<browser>")
      browser=$(echo -e "$BROWSERS\n" | fuzzel --dmenu --auto-select --width=30 --prompt="browser > ")
      "$0" "$browser"
      ;;
    *)
      IFS=' ' read -ra match <<< "$selection"
      "$BROWSER" "${match[-1]}"
esac