我试图在我的Python脚本中运行AppleScript,但它不起作用。当我在AppleScriptEditor中运行相同的AppleScript时,它工作得很好!
这是我的代码:
script = '''
tell application "System Events"
set position of first window of application process "%(app)s" to {100, 100}
end tell
''' % {'app': app}
print(script)
p = Popen(['osascript', '-'], stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True)
stdout, stderr = p.communicate(script)
我正在打印脚本,一切都是正确的
tell application "System Events"
set position of first window of application process "Terminal" to {100, 100}
end tell
但我不知道它为什么不移动终端窗口。有什么想法吗?
发布于 2018-07-19 07:00:39
最有可能的情况是,Python进程不允许接触窗口。操纵其他进程的Windows是一种特权操作,需要将您的应用程序列入白名单。请参阅https://apple.stackexchange.com/questions/291574/osascript-is-not-allowed-assistive-access-1728
要查看是否是这种情况,请检查从Popen返回的stderr值,并查看它是否包含类似“osascript is not allowed assistive access”之类的内容。
编辑:在Mojave中,Apple Events现在是沙盒,必须得到用户的批准。https://mjtsai.com/blog/2018/06/28/apple-event-sandboxing-in-macos-mojave-lacks-essential-apis/
https://stackoverflow.com/questions/51412000
复制相似问题