简体   繁体   English

在Python脚本中运行AppleScript

[英]Running AppleScript inside of Python script

I'm trying to run the AppleScript inside of my Python script but it does not work. 我试图在我的Python脚本中运行AppleScript,但是它不起作用。 When I run the same AppleScript in the AppleScriptEditor it works perfectly! 当我在AppleScriptEditor中运行相同的AppleScript时,它可以完美运行!

This is my code: 这是我的代码:

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)

I'm printing the script and eveything is correct 我正在打印脚本,一切正确

tell application "System Events"
                set position of first window of application process "Terminal" to {100, 100}
            end tell

but I do not know why it does not move the Terminal window. 但我不知道为什么它不移动终端窗口。 Any idea? 任何想法?

Most likely the Python process is not allowed to touch the window. 很可能不允许Python进程触摸窗口。 Manipulating Windows of other processes is a privileged operation that require your app to be whitelisted. 操纵Windows的其他进程是一项特权操作,需要将您的应用列入白名单。 See https://apple.stackexchange.com/questions/291574/osascript-is-not-allowed-assistive-access-1728 参见https://apple.stackexchange.com/questions/291574/osascript-is-not-allowed-assistive-access-1728

To see if this is the case, inspect the stderr value returned from Popen and see if it contains something like “ osascript is not allowed assistive access”. 要查看是否是这种情况,请检查从Popen返回的stderr值,并查看其中是否包含“不允许osascript进行辅助访问”之类的内容。

Edit: In Mojave, Apple Events are now sandboxes and will have to be approved by the user. 编辑:在Mojave中,Apple Events现在是沙箱,并且必须由用户批准。 https://mjtsai.com/blog/2018/06/28/apple-event-sandboxing-in-macos-mojave-lacks-essential-apis/ https://mjtsai.com/blog/2018/06/28/apple-event-sandboxing-in-macos-mojave-lacks-essential-apis/

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

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