简体   繁体   English

使用Python代替AppleScript

[英]Using Python in Place of AppleScript

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. 我经常使用Applescript来完成诸如打开和关闭程序之类的基本任务,有时会更深入地进行诸如运行特定Xcode程序的单元测试之类的工作。 I'm learning Python, and I love it. 我正在学习Python,我喜欢它。 I haven't been able to find much documentation relating AppleScript to Python. 我找不到很多有关AppleScript与Python的文档。 My question is: On Mac OS X, can Python be used in place of AppleScript to accomplish the tasks mentioned above? 我的问题是:在Mac OS X上,可以使用Python代替AppleScript来完成上述任务吗? And if so, does anyone know of a good resource to learn more about the topic? 如果是这样,是否有人知道一个很好的资源来了解有关该主题的更多信息?

Python can't be used to replace the UI & automation duties that AppleScript offers. Python不能用来代替AppleScript提供的UI和自动化职责。 However since OS X 10.10 (Yosemite) JavaScript can also be used. 但是,从OS X 10.10(Yosemite)开始,也可以使用JavaScript。

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

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. 为了避免包括PyObjC在内的所有过时且所谓的“ app脚”模块,只需在脚本编辑器或脚本调试器(我选择)中调试脚本,然后通过Popen使用osascript即可执行。 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. 我更喜欢这样做,这样可以确保应用程序实现的特质得到解决,并且Script Debugger具有出色的调试和浏览工具。

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. 将列表中的args传递给osascript,但是它们必须是字符串,因此复杂的数据结构对于封送和取消封送而言可能是乏味的,但是简单性是有代价的。 I left off error checking and exception handling/raising for simplicity. 为了简单起见,我不再进行错误检查和异常处理/引发。 TANSTAAFL 坦斯塔夫

不能真正替代它,但是有一个包装器可用: https : //pypi.python.org/pypi/py-applescript/1.0.0 ...因此您可以在python程序中与它进行交互。

With Cocoa you can send events directly using AESendMessage. 使用Cocoa,您可以直接使用AESendMessage发送事件。 It is a lot more work though. 但是,还有很多工作要做。 Here is a gist which provides an example: 这是一个要点,提供了一个示例:

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

While you can produce convoluted python that invokes the same Apple Events as AppleScript, some things are handled more easily in AppleScript. 虽然您可以生成卷积的python来调用与AppleScript相同的Apple事件,但在AppleScript中可以更轻松地处理某些事情。

It's easy enough to execute some AS in python though: 尽管很容易在python中执行一些AS:

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. 如果需要获取数据,则结果为NSAppleEventDescriptor。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM