Hazel, AppleScript and Python

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Hazel, AppleScript and Python Fri Jan 26, 2018 1:59 pm • by ATLTarheel1
I need some help making Hazel, AppleScript and Python work together. My Hazel action is pretty simple:

do shell script "python /Users/MyName/HazelTest/Read_CSV_Then_PDF_Then_Search_Array_for_Hazel.py"

If I understand the Hazel User Guide, Hazel will pass "theFile" to AppleScript. I therefore used "theFile" in my Python program, but the Hazel log shows:

NameError: name 'theFile' is not defined"

How do I pass the the file name from Hazel to Python?
ATLTarheel1
 
Posts: 2
Joined: Mon Feb 23, 2015 4:32 pm

Re: Hazel, AppleScript and Python Mon Jan 29, 2018 9:38 am • by Mr_Noodle
The variable is not called "theFile" in python. Also, is there a reason why you are using AppleScript for this? If you are just running a Python script, you should use "Run shellscript" and specify python as the shell there.
Mr_Noodle
Site Admin
 
Posts: 11251
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Hazel, AppleScript and Python Mon Jan 29, 2018 3:21 pm • by ATLTarheel1
I want to use a vendor file to standardize filenames and tags for a paperless application. The input to Hazel would be an OCR'd pdf file from Doxie. The Python program would refer to a vendor file to match the OCR info to the right vendor name and provide a tag to use. Then Hazel would rename the file, add the tag and move the file to the right directory. So, for example, if the OCR information contained "WH&LE FOODS", Python would tell Hazel to rename the file using "whole foods" and add a tag for "groceries".

I have written the Python program, but am struggling with how to integrate it into Hazel. If I understand correctly, a shell script can only be passed one parameter and only return one parameter. I am open to suggestions on how best to do this.

Thanks for your help!
ATLTarheel1
 
Posts: 2
Joined: Mon Feb 23, 2015 4:32 pm

Re: Hazel, AppleScript and Python Tue Jan 30, 2018 11:35 am • by Mr_Noodle
I don't think it will be possible to return more than one parameter from Python. Even if python supports it, shell scripts (regardless of how you call it - directly or via AppleScript) can only return a single integer.

The whole thing would probably have to be done in AppleScript or you need to get tricky with communicating from the Python script to AppleScript.
Mr_Noodle
Site Admin
 
Posts: 11251
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Hazel, AppleScript and Python Mon Apr 02, 2018 4:17 pm • by ajonsson
Hey, following up on this because I had the same question and found a solution.

As has been mentioned, the easiest way is to call "python" as the interpreter in the "Run shell script" settings.

To get the filename that is being processed, import the "sys" library and check the sys.argv array. The second entry should contain the filename. Here's an example that will print the matched filenames into a "file.txt" file:

Code: Select all
import sys

filename = sys.argv[1]

with open('file.txt', 'a') as opened_file:
    opened_file.write(filename + '\n')

sys.exit(0)
ajonsson
 
Posts: 1
Joined: Mon Apr 02, 2018 4:10 pm


Return to Support