0
votes

I'm trying to use Applescript to fire a python script. When I fire it from terminal, everything works fine, but when I fire from Applescript, it appears to run but nothing happens.

I've tried all manner of combinations for everything I can find in searches and other posts for using "python file.py" or "/usr/bin/python file.py" with "#!/usr/bin/env python" and "#!/usr/bin/python".
If I enter "which python" in terminal, I get "/usr/bin/python"

Right now I have both scripts broken down to their base components. I'll eventually be using Applescript to pass a file path into python using sys.argv[1] (which is why I'm using Applescript to fire the python script) but I'm not even that far along yet as the below doesn't work yet.

Applescript

do shell script "/usr/bin/python $HOME/Desktop/test.py"

Python

#!/usr/bin/python

import sys
import os

# The notifier function
def notify(title, subtitle, message):
    t = '-title {!r}'.format(title)
    s = '-subtitle {!r}'.format(subtitle)
    m = '-message {!r}'.format(message)
    os.system('terminal-notifier {}'.format(' '.join([m, t, s])))

# Calling the function
notify(title    = 'Message Test',
       subtitle = 'Test1:',
       message  = 'Test2')

sys.exit(0)

The python script sends a notifier message. Every time I run in terminal, I receive the message without issue. Every time I run the applescript to do as shell script it runs without error-ing in AS, but no message comes from Python.

Anyone have thoughts on where I've gone wrong?

1
I re-created your Python code and AppleScript on my Mac and it worked (after I commented out the line with the terminal-notifier (because I don't have it installed) and replacing with a simple print). When I ran it with the terminal-notifier present, I got an error in Python, but nothing from AppleScript. I believe AppleScript can only return an output sent to stdout by the code you are calling. Perhaps terminal-notifier doesn't print anything and adding a print statement might help you understand what's going on... - Jonathan

1 Answers

0
votes

Does it work for you if you use a full path to the binary? It worked for me in both BBEdit and Smile (script editor). My path is:

/Applications/terminal-notifier-2.0.0/terminal-notifier.app/Contents/MacOS/terminal-notifier

So I used:

#!/usr/bin/python

import sys
import os

# The notifier function
def notify(title, subtitle, message):
    t = '-title {!r}'.format(title)
    s = '-subtitle {!r}'.format(subtitle)
    m = '-message {!r}'.format(message)
    os.system('/Applications/terminal-notifier-2.0.0/terminal-notifier.app/Contents/MacOS/terminal-notifier {}'.format(' '.join([m, t, s])))

# Calling the function
notify(title    = 'Message Test',
       subtitle = 'Test1:',
       message  = 'Test2')

sys.exit(0)