在不使用osascript或appscript的情况下从Python调用AppleScript?

2024-04-19 08:10:47 发布

您现在位置:Python中文网/ 问答频道 /正文

是否有任何方法可以在不使用osascript命令行实用程序或appscript(我不想使用(我认为?)的情况下从python执行(并获得)AppleScript代码的结果因为it's no longer developed/supported/recommended)?

理由:在another question I've just posted中,我描述了通过osascript运行一些AppleScript时遇到的一种奇怪/不受欢迎的行为。因为我实际上是从一个python脚本调用它,所以我想知道是否有一种方法可以绕过osascript,因为这似乎是问题所在-但是appscript(明显的选择?)现在看起来很危险。。。


Tags: 方法no代码命令行实用程序情况itsupported
2条回答

皮皮是你的朋友。。。

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

您可以使用PyObjC桥:

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

相关问题 更多 >