How do I embed an AppleScript in a Python script?

后端 未结 8 718
灰色年华
灰色年华 2020-12-25 09:13

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

相关标签:
8条回答
  • 2020-12-25 09:35

    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)
    
    0 讨论(0)
  • 2020-12-25 09:37

    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')
    
    0 讨论(0)
提交回复
热议问题