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
Extra query if I may: anyone knows if using JS is less prone to fail? safer to use process wise?
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.
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â
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
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
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
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.