AppleScript for Transmission App

Need help figuring out what is wrong with this AppleScript for use in a macro.

apologize if this isn't the right place for this type of question.

If the number of application processes with that name is 0, then there is no application process that needs to quit.

Perhaps, if the intention of the script is to quit any running instance of that app, what you need is if count ... > 0

or

if 0 < ....

If "Transmission" is the actual name of the app that is running, I'd just use this:

if application "Transmission" is running then
  tell application "Transmission" to quit
end if
1 Like

but the goal is to quit when application is complete...since we confirmed that you can't add a km action based on a process right?

The intension of the script is to quit when no "download process" are going.

Transmission doesn't have an AppleScript interface, and the processes exposed by System Events only tell you whether the application itself is running or not.

You may be able to do something with shell scripting, however:

This doesn’t really have anything to do with Keyboard Maestro, but since we're all here anyway, I guess we might as well try to solve the problem.


The bad news is that there is no direct/foolproof way to do this.

The good news is that we should be able to fake it to an acceptable level.

The quick and dirty way

Transmission has built-in support for running a script when a download finishes. You could run a script at that point and quit the app.

#!/usr/bin/env zsh -f
/usr/bin/osascript -e 'tell application "Transmission" to quit'
exit 0

The obvious flaw here is that if you are downloading more than 1 torrent at a time, you probably don't want to quit after only one finishes, because the other one will be left incomplete. However, if you only download one torrent at a time, it’s by far the easiest solution.

The “not-foolproof but better” way

There is a command-line interface (CLI) for Transmissions. This used to be one tool called transmission-cli but now it's a group of tools which are, collectively, referred to as transmission-cli.

  1. The first thing we need to do is install the command-line tool for Transmission:

brew install transmission-cli

Unfortunately I'm not sure how to install it if you do not use brew. So… hopefully you use brew.

  1. Once installed, you’ll want to enable the “Remote” feature of the regular Transmission.app, like so:

20-Transmission-Enable-Remote

You don’t have to limit it to 127.0.0.1 but if you do, that prevents anyone who is not on your local computer from seeing your Transmission activity.

  1. Now we can use the transmission-remote tool to list any active torrents being downloaded on localhost by running this command

transmission-remote localhost --list

If no torrents are active, the output will look like this:

ID     Done       Have  ETA           Up    Down  Ratio  Status       Name
Sum:              None               0.0     0.0

If active, it will look like this:

ID     Done       Have  ETA           Up    Down  Ratio  Status       Name
   1     0%   49.15 kB  1 days       0.0    34.0    0.0  Downloading  ubuntu-18.04.3-desktop-amd64.iso
Sum:          49.15 kB               0.0    34.0

and when it's finished downloading, it will look like this:

ID     Done       Have  ETA           Up    Down  Ratio  Status       Name
   1   100%    2.08 GB  Unknown      0.0     0.0    0.0  Idle         ubuntu-18.04.3-desktop-amd64.iso
Sum:           2.08 GB               0.0     0.0

Next you need to set “Remove from the transfer list when seeding completes” as shown here:

With that setting enabled, you would want to look for an empty list of transfers. To do that, we run transmission-remote localhost --list and strip away the default output lines (one that starts with “ID” and one that starts with “Sum:”) like this:

transmission-remote --list | egrep -v '^(ID|Sum:) '

If there is any other output, it means that something is active. So we assign the output of that command to a variable STATUS like so:

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

Now we just need to check to see if $STATUS is empty or not.

#!/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

I don’t see any obvious flaws here.

Set your seeding limit low, if you like, and set a low “timeout if idle” too (I set mine to 30 minutes, but you could go lower).

This should prevent Transmission from being quit if there are any active downloads/uploads happening.

I hope that helps. Let me know if you have questions.

3 Likes

This is how my macro looks now.

If all your shell script is doing is quitting Transmission, you can just have Keyboard Maestro quit the app for you (Quit Specific Application).

I need it to quit when and only when downloads are finished/complete.

Yes, I understand. That was the intent behind what I wrote above.

I see this is also posted at https://apple.stackexchange.com/q/373018 so I have re-posted my answer there, as it may be a better place for the discussion.