Running python script when download completes

Ask for help and report issues with the Mac OS X version of Transmission
Post Reply
NoSmokingBandit
Posts: 5
Joined: Thu Nov 13, 2008 10:10 pm

Running python script when download completes

Post by NoSmokingBandit »

I'm scratching my head here trying to figure this one out. I have a python script I'd like to run when a download finishes because I'm much more fluent in python than shell scripts and the python transmissionrpc module is pretty awesome.

First I simply picked the python script in Transmission to be run when downloads complete. I didn't think it would work. I was correct.

So then I made a simple shell script to launch the python script:
complete.sh

Code: Select all

#!/bin/sh
python /Users/My\ Name/Documents/scripts/transmission_complete.py
Then I chmod +x it in terminal and select it in Transmission. No luck.

So then I try to determine where the problem is. Is the shell script running, or is the python script not launching? I know the py script works when I run it directly in Terminal. So maybe the shell script doesn't work.

If i run the shell script directly in Terminal (ie cd to directory, run ./complete.sh) it works perfectly.

I modified it to this:

Code: Select all

#!/bin/sh
osascript -e 'display notification "It Worked!" with title "Success"'
python /Users/My\ Name/Documents/scripts/transmission_complete.py
When a download completes I get the notification as shown in the script. So the shell script is launching, and it can run applescripts, but it doesn't run python...

It seems like everything works until I want Transmission to handle running the script.

What's going on here?
mike.dld
Transmission Developer
Posts: 306
Joined: Wed Dec 25, 2013 10:56 pm

Re: Running python script when download completes

Post by mike.dld »

Try to either setup environment properly by using "#!/usr/bin/env sh" shebang instead of "#!/bin/sh", or use full path to python executable e.g. "/usr/bin/python".

Another good way to debug would be to redirect all the output to a file, something like

Code: Select all

#!/bin/sh
python /Users/My\ Name/Documents/scripts/transmission_complete.py > /tmp/complete.log 2>&1
NoSmokingBandit
Posts: 5
Joined: Thu Nov 13, 2008 10:10 pm

Re: Running python script when download completes

Post by NoSmokingBandit »

Thanks for the pointers. Some day I'll read up on shell scripting so I know what I'm doing.

The environment didn't have any effect on the script, but I found in the log that python wasn't importing transmissionrpc correctly. Or at all.

To fix it I had to give python the complete path to the transmissionrpc egg rather than simply "import transmissionrpc"

Thanks again, you helped a lot.
Post Reply