Last active
September 24, 2021 03:34
-
-
Save jarmitage/ad961c63024cc1f827097f161ecf4211 to your computer and use it in GitHub Desktop.
Running AppleScripts from Python
This file contains hidden or 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 | |
| def applescript_to_list (file, path="/my/default/path/"): | |
| # open an applescript and return its contents as a list | |
| applescript = open(path + file + ".applescript", "r") | |
| lines = applescript.readlines() | |
| applescript.close() | |
| return lines | |
| def applescript_list_to_string (script_list): | |
| # take a list of applestring commands and return it as a single string | |
| return ''.join(script_list) | |
| def applescript_string_to_args (script_string): | |
| # take an applescript formatted as a single string | |
| # and return a list of arguments to pass to a subprocess | |
| # see https://gist.github.com/peter-jung/c1ba664e5306fe029cf02383b4145d0a | |
| return [item for x in [("-e",l.strip()) for l in script_string.split('\n') if l.strip() != ''] for item in x] | |
| def applescript_run_script (script_args): | |
| # run applescript in a subprocess and return results | |
| proc = subprocess.Popen(["osascript"] + script_args, stdout = subprocess.PIPE) | |
| results = proc.stdout.read().strip() | |
| return results | |
| def applescript_run (script_name, path="/my/default/path/"): | |
| # open an applescript, format as subprocess args, run & return results | |
| as_list = applescript_to_list (script_name, path) | |
| as_str = applescript_list_to_string (as_list) | |
| as_args = applescript_string_to_args (as_str) | |
| return applescript_run_script (as_args) | |
| print (applescript_run ("hello_world")) |
This file contains hidden or 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
| use AppleScript version "2.4" | |
| use scripting additions | |
| do shell script "echo " & " Hello, World!" |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this also works, lol:
!{'osascript ' + path + 'hello_world' + '.applescript'}