-
-
Save gwthompson/c41ce03deee87b83c27270816bbc5476 to your computer and use it in GitHub Desktop.
How to call an Applescript from Python on Mac?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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