Page 1 of 1

Python Scripts

PostPosted: Mon Nov 17, 2014 9:37 pm
by ronbrown
I'm trying to use Hazel to trigger a python script to rename files by adding a code to the front.

Here is what I have in Hazel:
Name:RenameCueSheet
If any of the following conditions are met:
Extension is pdf
Do the following to the matched file or folder:
Display notification with pattern: Rule running
Run shell script embedded script

which then has:

Shell: /bin/bash
Use $1 to refer to the file being processed.
python /Users/ronbrown/Dropbox/Python/test.py $1

When I force the script to run I see this in the log:

2014-11-17 20:20:51.310 hazelworker[24162] Processing folder Upload (forced)
2014-11-17 20:20:53.415 hazelworker[24162] tour_10.xlsx.pdf: Rule RenameCueSheet matched.
2014-11-17 20:20:53.415 hazelworker[24162] Hazel Alert: Rule running
2014-11-17 20:20:53.415 hazelworker[24162] [Custom Message] Hazel Alert: Rule running
2014-11-17 20:20:53.453 hazelworker[24162] [Error] Shell script failed: Error processing shell script on file /Users/ronbrown/Dropbox/MAFW/Upload/tour_10.xlsx.pdf.
2014-11-17 20:20:53.454 hazelworker[24162] Shellscript exited with non-successful status code: 1

but when I run the python script from the terminal:

RBs-iMac-2:Upload ronbrown$ pwd
/Users/ronbrown/Dropbox/MAFW/Upload
RBs-iMac-2:Upload ronbrown$ python /Users/ronbrown/Dropbox/Python/test.py tour_10.xlsx.pdf
RBs-iMac-2:Upload ronbrown$

The script runs successfully.

Here is the python file:
import os
import sys
import csv

# check that input name is given
if len(sys.argv) != 2:
print "Usage: python test.py <name>"
sys.exit()

#read in the mapping file of sheet # to tour #
with open('/Users/ronbrown/Dropbox/Python/mapping.csv', mode='rU') as infile:
reader = csv.reader(infile, dialect=csv.excel)
mydict = {rows[0]:rows[1] for rows in reader}

name = sys.argv[1]
tour_num = name[name.find('_')+1:name.find('.')]
sheet_num = mydict[tour_num]
new_name = "/Users/ronbrown/Dropbox/MAFW/Upload/renamed/" + sheet_num + '-' + name[:name.find('.xlsx')] + name[name.find('.xlsx')+len('.xlsx'):]
os.rename(name,new_name)


Any ideas?

Re: Python Scripts

PostPosted: Tue Nov 18, 2014 12:58 pm
by Mr_Noodle
One thing to try is to not use bash as an intermediary. You can specify python as the shell so you can run your script directly. Also, turn on debug logging as described here: viewtopic.php?f=4&t=296

That will show you the output of the script. You may want to add print statements to the script to isolate where things go wrong.

Re: Python Scripts

PostPosted: Wed Nov 19, 2014 10:30 pm
by ronbrown
Thanks for the suggestions. I was super-frustrated trying to do this as a folder action because I could not debug the script. Going to Hazel and actually editing the script in Hazel and being able to see the print messages in the log made this possible.

Thanks so much.

Is there any way for Hazel to act on the output of the script? Display the print statements in the notification center instead of the log? Or generate one message on normal exit and another on abnormal exit?

Ron

Re: Python Scripts

PostPosted: Thu Nov 20, 2014 11:07 am
by Mr_Noodle
That's something you'll have to do in your script. You can try redirecting stdout and stderr and processing it somehow. As for exit status, if the script fails, subsequent actions won't be run. So you can add a notification action after the script for success. For failure, you can try adding a whole other rule after it to provide a different notification.