r/applescript • u/spenney09 • Jan 08 '26
When is “delay” needed?
Sometimes MacOS keeps up with AppleScript, yet other times I have to slow my script down a bit with “delay 0.1” or so.
Which MacOS situations typically require me to slow down my scripts and which ones don’t?
5
Upvotes
View all comments
Show parent comment
2
u/bprime43 Jan 08 '26
This is generally what my use case is also. That said, I’ve taken to doing something like
———
…blah blah, something that opens a window
Repeat While 0=0
Tell system events Tell process app name Set allWindows to name of all windows End tell End tell
If allWindows contains window I need to have open Exit repeat End if Delay 0.1 End repeat
Do next steps
—————
So, longest I’m waiting after a window to open is however long it takes system events to make the list and then check it, otherwise it waits another 10th of a second and goes again. You can also add more safeties in by counting your loops, and adding an exit that way, or trying to reopen the window every 10 loops in case something went weird.