0

I'm trying to run the AppleScript inside of my Python script but it does not work. When I run the same AppleScript in the AppleScriptEditor it works perfectly!

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?

1 Answer 1

1

Most likely the Python process is not allowed to touch the window. Manipulating Windows of other processes is a privileged operation that require your app to be whitelisted. See 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”.

Edit: In Mojave, Apple Events are now sandboxes and will have to be approved by the user. https://mjtsai.com/blog/2018/06/28/apple-event-sandboxing-in-macos-mojave-lacks-essential-apis/

Sign up to request clarification or add additional context in comments.

7 Comments

What did stdout/stderr contain?
How can I check it? I run my script in terminal and do not get anything there
By printing the variables?
Not authorised to send Apple events to System Events. (-1743)
Are you by any chance using the Mojave beta? felix-schwarz.org/blog/2018/06/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.