From 2d074075dc36f80d1b76a87214f2d4b38f629b12 Mon Sep 17 00:00:00 2001 From: Zukky Baig Date: Wed, 27 May 2026 20:45:35 +0100 Subject: [PATCH] Add Bookmarks plugin Stores URL bookmarks in $HOME/.xbar-bookmarks.txt. Click an entry to open it, add via an AppleScript prompt, remove from a submenu. Pure bash with osascript, no external dependencies. --- Web/bookmarks.1d.sh | 102 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100755 Web/bookmarks.1d.sh diff --git a/Web/bookmarks.1d.sh b/Web/bookmarks.1d.sh new file mode 100755 index 000000000..f29b13686 --- /dev/null +++ b/Web/bookmarks.1d.sh @@ -0,0 +1,102 @@ +#!/usr/bin/env bash + +# Bookmarks +# v1.0 +# Zukky Baig +# ZukkyBaig +# Quick-access URL bookmarks in the menubar. Click to open, add via a prompt, remove from a submenu. +# https://raw.githubusercontent.com/ZukkyBaig/swiftbar-bookmark-plugin/main/assets/menubar-with-bookmark.png +# bash,osascript +# https://github.com/ZukkyBaig/swiftbar-bookmark-plugin + +SCRIPT="$0" +BOOKMARKS_FILE="$HOME/.xbar-bookmarks.txt" + +[ -f "$BOOKMARKS_FILE" ] || touch "$BOOKMARKS_FILE" + +# Strip leading and trailing whitespace from a string. +trim() { + local s="$1" + s="${s#"${s%%[![:space:]]*}"}" + s="${s%"${s##*[![:space:]]}"}" + printf '%s' "$s" +} + +case "$1" in + add) + # Prompt the user for a name and URL via AppleScript, then append to the bookmarks file. + result=$(osascript <<'EOF' 2>/dev/null +try + set theName to text returned of (display dialog "Bookmark name:" default answer "" with title "Add Bookmark") + set theURL to text returned of (display dialog "URL:" default answer "https://" with title "Add Bookmark") + return theName & "§" & theURL +on error + return "" +end try +EOF +) + if [ -n "$result" ]; then + name="${result%%§*}" + url="${result#*§}" + name="$(trim "$name")" + url="$(trim "$url")" + if [ -n "$name" ] && [ -n "$url" ]; then + case "$url" in + http://*|https://*) ;; + *) url="https://$url" ;; + esac + printf '%s | %s\n' "$name" "$url" >> "$BOOKMARKS_FILE" + fi + fi + exit 0 + ;; + remove) + # Delete the bookmark whose name matches the given argument. + target="$2" + if [ -n "$target" ]; then + tmp="$(mktemp)" + awk -F'\\|' -v t="$target" ' + { + name=$1 + sub(/[[:space:]]+$/, "", name) + sub(/^[[:space:]]+/, "", name) + if (name != t) print + } + ' "$BOOKMARKS_FILE" > "$tmp" + mv "$tmp" "$BOOKMARKS_FILE" + fi + exit 0 + ;; +esac + +# Render the menubar dropdown: bookmarks list, then add/remove/edit/refresh actions. +echo ":bookmark:" +echo "---" + +if [ ! -s "$BOOKMARKS_FILE" ]; then + echo "No bookmarks yet | color=gray" +else + while IFS= read -r line; do + [ -z "$line" ] && continue + name="$(trim "${line%%|*}")" + url="$(trim "${line#*|}")" + [ -z "$name" ] || [ -z "$url" ] && continue + echo "$name | href=$url" + done < "$BOOKMARKS_FILE" +fi + +echo "---" +echo "Add bookmark… | shell=\"$SCRIPT\" param1=add refresh=true terminal=false" + +if [ -s "$BOOKMARKS_FILE" ]; then + echo "Remove bookmark" + while IFS= read -r line; do + [ -z "$line" ] && continue + name="$(trim "${line%%|*}")" + [ -z "$name" ] && continue + echo "-- $name | shell=\"$SCRIPT\" param1=remove param2=\"$name\" refresh=true terminal=false" + done < "$BOOKMARKS_FILE" +fi + +echo "Edit file… | shell=open param1=\"$BOOKMARKS_FILE\" terminal=false" +echo "Refresh | refresh=true"