I am trying to embed an AppleScript in a Python script. I don\'t want to have to save the AppleScript as a file and then load it in my Python script. Is there a way to enter
You can use os.system
:
import os
os.system('''
osascript -e
'[{YOUR SCRIPT}]'
'[{GOES HERE}]'
''')
or, as suggested by Alex Martelli you can use a variable:
import os
script = '''
[{YOUR SCRIPT}]
[{GOES HERE}]
'''
os.system('osascript -e ' + script)
Here's a simple python3 synchronous example, if you want your python code not to wait for Applescript to finish. In this example, both say
commands are executed in parallel.
from subprocess import Popen
def exec_applescript(script):
p = Popen(['osascript', '-e', script])
exec_applescript('say "I am singing la la la la" using "Alex" speaking rate 140 pitch 60')
exec_applescript('say "Still singing, hahaha" using "Alex" speaking rate 140 pitch 66')