Using Python in Place of AppleScript

一世执手 提交于 2021-01-21 08:55:51

问题


I often use Applescript to accomplish basic tasks like opening and closing programs, sometimes going a little more in-depth like running a specific Xcode program's unit tests. I'm learning Python, and I love it. I haven't been able to find much documentation relating AppleScript to Python. My question is: On Mac OS X, can Python be used in place of AppleScript to accomplish the tasks mentioned above? And if so, does anyone know of a good resource to learn more about the topic?


回答1:


Python can't be used to replace the UI & automation duties that AppleScript offers. However since OS X 10.10 (Yosemite) JavaScript can also be used.

See here: https://developer.apple.com/library/mac/releasenotes/InterapplicationCommunication/RN-JavaScriptForAutomation/index.html




回答2:


To avoid all the out-dated and so-called "crappy" modules including PyObjC, one can simply debug a script in the Script Editor or Script Debugger (my choice) and then execute it using osascript via Popen. I prefer this so I can ensure the idiosyncrasies of the applications implementation are worked around and Script Debugger has great debugging and browsing tools.

For example:

from subprocess import Popen, PIPE

def get_front_win_id():
    """
    Get window id of front Chrome browser window
    """
    script = '''
        on run {}
            set winID to 0
            tell application "Google Chrome"
                set winID to id of front window
                return winID
            end tell
        end run
    '''
    args = []
    p = Popen(['/usr/bin/osascript', '-'] + args,
              stdin=PIPE, stdout=PIPE, stderr=PIPE)
    stdout, stderr = p.communicate(script)
    winID = stdout.strip()
    return int(winID)

Pass args in a list to osascript but they have to be strings so complex data structures can be tedious to marshal and and unmarshal but there is a price for the simplicity. I left off error checking and exception handling/raising for simplicity. TANSTAAFL




回答3:


While you can produce convoluted python that invokes the same Apple Events as AppleScript, some things are handled more easily in AppleScript.

It's easy enough to execute some AS in python though:

from Foundation import NSAppleScript
textOfMyScript = """
   *(Your AppleScript Here)*
"""
myScript = NSAppleScript.initWithSource_(NSAppleScript.alloc(), textOfMyScript)
results, err = myScript.executeAndReturnError_(None)

The results are an NSAppleEventDescriptor, if you need to get data out.




回答4:


Can't really replace it but there is a wrapper available for it: https://pypi.python.org/pypi/py-applescript/1.0.0... so you can interact with it from within your python program.




回答5:


With Cocoa you can send events directly using AESendMessage. It is a lot more work though. Here is a gist which provides an example:

https://gist.github.com/pudquick/9683c333e73a82379b8e377eb2e6fc41


来源:https://stackoverflow.com/questions/28058546/using-python-in-place-of-applescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!