Tell section being executed several times with no loop

Good morning everyone,

I need help handling System Events. I have the following script coded in a CI for work

if application "System Events" is not running then
    log "System Events wasn't running"
    launch application "System Events"
    delay 0.5
end if

if application "System Events" is not running then
    repeat 10 times
        if application "System Events" is not running then
            log "Not running yet"
            delay 1
        else
            log "Now is running"
            exit repeat
        end if
    end repeat
else
    log "System Events is running"
end if

tell application "System Events"
    log "First tell System Events"
    tell process "MyApp"
        log "Tell MyApp process"
        set position2 to position of window 2
        set position of window 1 to position2
        set frontmost to true
    end tell
end tell

tell application "System Events"
    log "Second tell System Events"
    delay 0.5
    keystroke "g" using {command down, shift down}
    delay 0.5
    keystroke /Users/my_user/Documents/sound.ogg
    delay 0.5
    keystroke return
    delay 0.5
    keystroke return
end tell

The first two sections were a fix I had to add because the System Events process would play funny sometimes. Currently, my issue is that the first “tell” section seems to be repeating for some reason since I receive the “First tell System Events” log multiple times in each execution. My logs:

System Events is running
First tell System Events
First tell System Events
First tell System Events
First tell System Events
First tell System Events
First tell System Events
First tell System Events
609:639: execution error: System Events got an error: Application isn’t running. (-600)

Does anyone know how can I patch this so it works?
Osascript is getting particularly tiresome, each day a new casuistry of bug appears, it’s a nightmare.
My lifetime appreciation to anyone how can explain to me how to work safely with System Events :smiling_face_with_tear:

Extra query if I may: anyone knows if using JS is less prone to fail? safer to use process wise?

In general, JXA is less polished than AppleScript‘s integration with apps. JS as a language is not „prone to fail“. Nor is AppleScript.

In the case you presented here, it might be worthwhile to explain what you’re trying to achieve.

My script started being the two last tell without the logs.

What they basically do is:

  • Move the second window of a process in top of the first one. This is because this second window is a file handler to select a file to load in the app. Sometimes the window would open in another monitor and osascript would fail. Moving one over the other fixes this.

  • The second tell simply inputs the path of a file in Finder and accepts the file so it gets loaded in the app.

The two “if sections” on top of the first tell where added because in the CI / certain machines the System Events process would not always work/be up as in my local. They are a safety measure to ensure System Events is running. And yes, I have all the “Accessibility + Automation + File Access” permissions in order, which is usually the typical response.

The only thing I’m trying to achieve is to find a safe script that doesn’t fail and allows me to perform the first two actions mentioned at the start of this explanation.

Regarding the “it’s not prone to fail”, giving that there is more than one post in the internet which says “try to restart your computer / try to kill the System Events process and retry” I consider this kind of unreliable and given that I HAD to add the countermeasure mentioned above for this script to work in certain machines, I do consider it’s not “smoothly working”.
Maybe “prone to fail” it’s not the correct way of saying it but I think you understand what I mean.

EDIT: edited the main post to avoid this “prone to fail” wording.

In that case, it’s the System Events process that’s failing, not the language itself. That’s what I was referring to. If that’s the case, it is completely independent of the language you use to talk to it.

In my mind, GUI scripting is never very robust.

I get your point and your clarification. Unfortunately, even if it’s not robust, it’s the only solution we currently have to test this automatically.
I was just hoping someone comes with a weird workaround as the first two if statements for this new “issue”

Simply this:

launch application "System Events"

repeat while (running of application "System Events" is false)
	delay 0.2
end repeat

Also, you can’t get the position of window 2 of your process, because the process has only 1 window - window 1, the frontmost. You should tell to app instead of the process to change the position. Instead of:

tell application “System Events”
log “First tell System Events”
tell process “MyApp”
log “Tell MyApp process”
set position2 to position of window 2
set position of window 1 to position2
set frontmost to true
end tell
end tell

simply this:

tell application "MyApp" to set position of window 1 to position of window 2
1 Like

Your simplification could block the CI indefinitely if something goes wrong with the System Events process but I get your point.

Regarding the second part, the current code works, you can get the position of a window. My query is not that the code does not work but that does not work always because of System Events.

Also, just for the sake of curiosity, how do you accomplish this “You should tell to app instead of the process to change the position”? Because if I use directly “tell application “My App”” Applescript informs me it can not access its windows.

This is the result of running the line you proposed:

error "MyApp got an error: Can’t set position of window 1 to position of window 2." number -10006 from position of window 1

So, your app is not scriptable.

That’s why I was using System Events. But you raised a nice point, I could see if development can add scriptable commands into the app. That’s something to explore, thanks you :slight_smile:

However, doesn’t solve the issue in the thread unfortunately

It’s strange that you’re having problems with System Events. I assumed that somehow System Events ended up working for you. For the test, I ran a separate script

tell application “System Events” to quit

I then ran the short test above, and it successfully completed in 50 msek. When System Events is running, the test completes almost instantly - about 0 msek. Try to restart the Mac.