Need help with using Applescript to fire a Python script

Posted on stack overflow but posting here too hoping you AS geniuses can help me figure out what I’m doing wrong.

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” or “/usr/bin/python” 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.


do shell script "/usr/bin/python $HOME/Desktop/"



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')


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?

Try this:

set python_Path to ((path to applications folder) as string) & "Python 3.7:Python" as alias
set executable_Path to ((path to desktop) as string) & "" as alias
tell application "Finder" to open executable_Path using python_Path

NOTE: I believe you have installed Python 3.7 on your computer. Python is a standard part of installed folder.

Other way is this:

tell application "Terminal" to activate
tell application "System Events" to tell application process "Terminal"
	keystroke "/usr/bin/python $HOME/Desktop/"
	key code 36
end tell

Other important note is this:

Python takes in considering PROPERLY setted TABS and SPACES in source code. You can get some info HERE.
For example,


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])))

will be only in this form (and don’t mix never spaces and tabs):

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])))

Right Python editors puts TABs automatically, when you press ENTER for next code line. I use Thonny and xCode for editing

I found the solution of this problem. That was: 1) mixed spaces and tabs in python source code, when I copy it from here (I fixed this problem in xCode, by retyping the code) and 2) AppleScript was needed properly provided path to the Terminal-Notifier executable file. In my case this path was:


Now 2 my scripts and 1 your do shell script all works fine.
Just replace:

os.system('terminal-notifier {}'.format(' '.join([m, t, s])))

with something such this:

os.system('/usr/local/Cellar/terminal-notifier/2.0.0/ {}'.format(' '.join([m, t, s])))

Tip: No need Python code at all, as you can use AppleScript to set terminal-notifier option’s values too, and then invoke terminal-notifier instead of python with do shell script command. This is the reason Apple is trying to limit AppleScript lately. “It can do too much,” much more than some users imagine.

Thanks for your help with this.

Python Launcher does indeed work so that’s an option. Wasn’t aware of that so thanks for enlightening me. I’ll have to read up on using it and how to pass an argument.

Trying not to go the keystroke route and the last suggestion of replacing the line didn’t work for me. I’m assuming the path is different for me and haven’t located the right adjustment yet.

The last solution I post here is much better, as 1) no need extra Python Launcher and 2) no extra windows opens. Just find and provide your own full path to terminal-notifier executable