#!/bin/sh # Mimeapp script for adding torrent to transmission-daemon, but will also start the daemon first if not running. # transmission-daemon sometimes fails to take remote requests in its first moments, hence the sleep. pgrep -x transmission-daemon >/dev/null 2>&1 || ( transmission-daemon && osascript -e 'display notification "💡 Starting transmission daemon..." with title "transmission-daemon"' ) directory="$HOME/Torrents" [ "$1" = "-l" ] && { URL=$(xclip -selection clipboard -o) case "$URL" in http://* | https://* | magnet:?*) transmission-remote -a "$URL" && notify-send "🔽 Torrent added." exit 0 ;; *) added_torrents=$(transmission-remote -l | grep -vE '^ID|Sum' | awk '{print $NF}' | sed 's/\.torrent$//') filtered_files=$(ls "$directory"/*.torrent 2>/dev/null | sed "s|^$directory/||" | sed 's/\.torrent$//' | grep -vF "$added_torrents") [ -n "$filtered_files" ] && { choice=$(echo "$filtered_files" | dmenu -i -l 20 -p "Select Torrent:") [ -n "$choice" ] && transmission-remote -a "$directory/$choice.torrent" && notify-send "🔽 Torrent added." } || notify-send "🤷 No new torrent found." ;; esac } || { transmission-remote -a "$@" && notify-send "🔽 Torrent added." "$TR_TORRENT_NAME" }