execute a python script

Talk, speculate, discuss, pontificate. As long as it pertains to Hazel.

Moderators: Mr_Noodle, Moderators

execute a python script Sun Jul 01, 2007 12:16 am • by Dee
I'm new to Hazel and scripting in general. One of the steps I want Hazel to do is execute a python script. Linking directly to it from Shell Script option in Hazel, didn't seem to work. Manually executing the python script works fine. I made a simple Bash script:

Code: Select all
#!/bin/bash
pythonscript.py


(saved in same directory as the python script). And manually executing that script works. But in Hazel it doesn't. Tried AppleScript:
Code: Select all
ignoring application responses
   tell application "Terminal"
      activate
      do script with command ¬
         "pythonscript.py ;"
   end tell
end ignoring

Manually executing in this case, launches Script Editor, but pressing RUN still makes it launch the python script. However, doesn't work in Hazel. Saving in all the other Save As.. options available in Script Editor, is not even selectable by Hazel.

Clearly I'm missing something here.

[/code]
Dee
 
Posts: 2
Joined: Sat Jun 30, 2007 11:44 pm

Mon Jul 02, 2007 5:45 pm • by Mr_Noodle
The "Run shell script" action is somewhat of a misnomer as it actually can execute anything that is basically executable.

I don't know Python but it's my understanding that you should be able to execute it directly as long as:

(a) It is set to executable
(b) It starts with "#!/usr/bin/python"

Try that out. Also, check the Console logs and see if there are any errors there.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Execute a python script?! - HA HA HA Tue Nov 04, 2008 10:10 am • by Gnashing Teeth
I've had nothing but trouble trying to get Hazel to properly execute bash and Python scripts. Scripts that work reliably & without error in the shell fail in various ways when run from Hazel, whether called as external scripts, or as "internal" scripts.

With great trouble I very occasionally can get a script to work by fiddling with it. Because Hazel doesn't seem to generate error codes or logs related to executing scripts, there's no way to troubleshoot - it's trial and error.

It is not a problem with passing in the argument. It's simply a defect in however Hazel has been coded to execute scripts written in bash or Python.
Gnashing Teeth
 
Posts: 6
Joined: Tue Nov 04, 2008 10:02 am

Re: execute a python script Wed Nov 05, 2008 3:00 pm • by Mr_Noodle
Can you provide me with the actual errors you are seeing? There should be some sort of log output in Hazel's log (go to the "Info" pane and click "View Log".

Also, a copy of the script that is failing would help.

Thanks.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: execute a python script Sun Nov 09, 2008 6:24 am • by Gnashing Teeth
Thanks for reminding me about the Log. That gave just enough to indicate the problem. I am using an external Python script to make shell calls to various utilities - (Python is easier to code than bash and I am already familiar with it) - for example to make use of the pdfinfo utility to check which software was used to create various PDFs. In the shell & in the original Python script run separately this works fine. But in Hazel the error message is this:

sh: pdfinfo: command not found

... even though both sh and bash find pdfinfo without a problem for my username.

The fix in this case was to code the entire path to pdfinfo. It looks like certain environment variables may be missing when Hazel runs shell scripts. I think I got pdfinfo through Macports - dim memory tells me that back when I installed Macports, I set up my user shell to have whatever environment variables are necessary to run these commands without the full path.

But the Log at least is something to work with. Sorry for the earlier rant.
Gnashing Teeth
 
Posts: 6
Joined: Tue Nov 04, 2008 10:02 am

Re: execute a python script Tue Nov 11, 2008 2:52 pm • by Mr_Noodle
No problem. Scripts aren't executed in your user shell so you need to source any resource files (.bashrc, etc.) in your script. Let me know if you run into any more problems.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Open Discussion