![](/img/trans.png)
[英]Python passing file path variable into Applescript using osascript
[英]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.