How do I embed an AppleScript in a Python script?

后端 未结 8 753
灰色年华
灰色年华 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:26

    See https://pypi.org/project/applescript/

    import applescript
    resp = applescript.tell.app("System Events",'''
    set frontApp to name of first application process whose frontmost is true
    return "Done"
    ''')
    assert resp.code == 0, resp.err
    print(resp.out)
    

    etc. Most of suggestions, including "applescript" I quoted, are missing one important setting to osascript -- setting an -s option to "s", otherwise you will be having difficulty parsing the output.

提交回复
热议问题