Skip to content

Instantly share code, notes, and snippets.

@peter-jung
Created February 22, 2017 09:28
Show Gist options
  • Save peter-jung/c1ba664e5306fe029cf02383b4145d0a to your computer and use it in GitHub Desktop.
Save peter-jung/c1ba664e5306fe029cf02383b4145d0a to your computer and use it in GitHub Desktop.
How to call an Applescript from Python on Mac?
import subprocess, sys
applescript='''
tell application "System Events"
set processName to name of processes whose frontmost is true
do shell script "echo " & processName
end tell
'''
args = [item for x in [("-e",l.strip()) for l in applescript.split('\n') if l.strip() != ''] for item in x]
proc = subprocess.Popen(["osascript"] + args ,stdout=subprocess.PIPE )
progname = proc.stdout.read().strip()
sys.stdout.write(str(progname))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment