In responding to a post in another thread, I did some testing with GUI Scripting and thought I would report the results.
I expected the following script to fail, but it worked flawlessly on my Sonoma computer. It appears that consecutive commands sent to an application process may not need a delay.
-- note the number of times the repeat loop is executed
tell application "TextEdit" to activate
tell application "System Events" to tell process "TextEdit"
repeat 50 times
key code 45 using command down -- keyboard shortcut for File > New
key code 9 using command down -- keyboard shortcut for Edit > Paste
end repeat
end tell
The script included below works correctly as written. In further testing:
-
I enabled the last line of the script. The Stickie app activated and quit, but no Stickie notes were closed.
-
I enabled the 0.5 second delay. The Stickie app activated and quit, and the frontmost Stickie note was closed.
-
I disabled the 0.5 second delay and enabled the repeat loop. The Stickie app activated and quit, and the frontmost Stickie note was closed. So, the test in the repeat loop did seem to work as an effective delay.
-- this script is for testing purposes only
-- it deletes a Stickie note and the contents of the note cannot be retrieved
tell application "Stickies" to activate
delay 0.3
tell application "System Events" to tell process "Stickies"
key code 0 using command down -- select all text
key code 51 -- delete text
key code 13 using command down -- close note
-- repeat until menu "File" of menu bar 1 exists -- thanks KniazidisR
-- delay 0.02
-- end repeat
end tell
-- delay 0.5
-- tell application "Stickies" to quit
There are three basic approaches that can be used to select an item from an app’s menu, and they all work well. However, the click-menu-item approach takes more than twice as long to execute:
tell application "TextEdit" to activate
tell application "System Events" to tell process "TextEdit"
repeat 10 times
click menu item "New" of menu "File" of menu bar 1 -- 830 milliseconds
-- keystroke "n" using command down -- 230 milliseconds
-- key code 45 using command down -- 230 milliseconds
end repeat
end tell