繁体   English   中英

从Python调用AppleScript而不使用osascript或appscript?

[英]Calling AppleScript from Python without using osascript or appscript?

有什么方法可以在不使用osascript命令行实用程序或appscript (我真的不想使用(我认为吗?)的情况下)从python执行AppleScript代码(并从中获取结果),因为不再开发/支持/推荐 )?

原理:在我刚刚发布的另一个问题中 ,我描述了通过osascript运行某些AppleScript时遇到的奇怪/不想要的行为。 当我实际上是从python脚本调用它时,我想知道是否有一种可以完全绕开osascript的方法,因为这似乎是问题所在-但是appscript(明显的选择?)现在看起来很有风险...

您可以使用PyObjC桥:

>>> from Foundation import *
>>> s = NSAppleScript.alloc().initWithSource_("tell app \"Finder\" to activate")
>>> s.executeAndReturnError_(None)

PyPI是您的朋友...

http://pypi.python.org/pypi/py-applescript

例:

import applescript

scpt = applescript.AppleScript('''
    on run {arg1, arg2}
        say arg1 & " " & arg2
    end run

    on foo()
        return "bar"
    end foo

    on Baz(x, y)
        return x * y
    end bar
''')

print(scpt.run('Hello', 'World')) #-> None
print(scpt.call('foo')) #-> "bar"
print(scpt.call('Baz', 3, 5)) #-> 15

暂无
暂无

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

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