AppleScript to quit Transmission App

applescripttorrent

I need help fixing AppleScript for an app called "Transmission"

I am trying to write code to get it to quit once downloads are finished

Is this the proper code:

    -- Quit Transmission, if running
tell application "System Events"
    if ((count (every process whose name is "Transmission Download")) = 0) then
        tell application "Transmission"
            -- Quit Transmission
            quit
        end tell
    end if
end tell

Best Answer

Transmission does not support AppleScript specifically, so there is no way to use AppleScript to check if downloads are still active or not.

There's a concurrent discussion of this idea going on at https://forum.keyboardmaestro.com/t/applescript-for-transmission-app/15789 but this is probably a better place for it, so I'll summarize my answer from there.

  1. Install transmission-cli using brew: brew info transmission-cli

  2. Enable the "Remote" feature of Transmission app:

Transmission Remote Preferences

You do not have to limit to 127.0.0.1 but unless you need to access from another computer, there's no harm in leaving that on.

  1. Enable the feature to remove torrent from the Transmission list after seeding is done:

Transmission Management preferences

  1. Now you can run this script periodically:
#!/usr/bin/env zsh -f

PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

    # the `pgrep` line says 'If Transmission is not running, exit immediately
    # just in case the script happens to be run when the app isn't even running
pgrep -qx Transmission || exit 0

`STATUS=$(transmission-remote --list | egrep -v '^(ID|Sum:) ')`

if [[ "$STATUS" == "" ]]
then
    osascript -e 'tell application "Transmission" to quit'
else
    echo "Transmission is still active:\n$STATUS"
fi

exit 0

If Transmission is running, but there are no items in the list, it will be quit.

There may be other ways of doing this, but I cannot think of any myself (other than just using Transmission's preference to run a script when a download is complete, and quitting the app then, but that will fail if there are multiple concurrent downloads active).