1

I have a Python 3 script that I'd like to add as a Service using Automator. The idea is that I can right click on a text file, and an option will display to run my script on said file.

The shebang for my Python script is as follows:

#!/usr/bin/env python3

This script works fine and as expected in Terminal. However, when I attempt to automate this with AppleScript/Automator like so:

on run {input, parameters}

tell application "Terminal"
    do shell script "/path/to/script.py $@"
end tell

return input
end run

I run into issues, namely:

Terminal got an error: env: python3: No such file or directory

I've read here, among other places, that AppleScript by design only allows access to Unix commands, and will not for example source your bash profile (thus we do not have access to our $PATH by default). However I was under the impression that providing explicit paths was a workaround.

I've tried changing the do shell to this:

do shell script "/usr/bin/python3 /path/to/script.py $@"

and I've tried changing the shebang to this:

#!/usr/bin/env /usr/bin/python3

But nothing seems to work. Is it simply just not possible to execute a Python 3 script via AppleScript? I've not been able to find a viable workaround in my searches.

Edit: Additional info:

$ type -a python3
python3 is /usr/local/bin/python3
  • Start the Terminal application and type the following into your shell : type -a python3 then edit your post with the command and the results. – fd0 Apr 5 '16 at 18:24
  • Done. Since it looks like that's a different path, I wonder if I can symlink /usr/bin/python3 to /usr/local/bin/python3 or vice versa to avoid this issue across the environment. – Aidan H. Apr 5 '16 at 18:41
  • Why do you expect do shell script "/usr/bin/python3 /path/to/script.py $@" to work? Your python3 is in /usr/local/bin What happens if you use the correct path? – Mark Apr 5 '16 at 20:23
  • Touche. I got confused, the path on my Linux server is /usr/bin/python3 so I used that without checking. – Aidan H. Apr 5 '16 at 21:33
1

Solved my problem by changing the do shell to:

do shell script "/usr/local/bin/python3 /path/to/script.py $@"

and letting the shebang stay as:

#!/usr/bin/env python3

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .