Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why AppleScript always send keystrokes with Command down?

Tags:

applescript

I see a lot of examples of AppleScript like this

tell application "TextEdit"
    activate
    tell application "System Events"
        keystroke "s"
    end tell
end tell

Expected result is that letter "s" will be typed into active document in TextEdit (assume there is at least one document window). But instead it always tries to save document (did it for changed document and open save dialog if it is a new). Same things happen for any key in any application at any time…

Does anybody know why System Events always send keystrokes like "… using {command down}"?

like image 922
Andrew Bridge Avatar asked Dec 21 '22 11:12

Andrew Bridge


1 Answers

I run script from AppleScript Editor using Cmd+R, not by clicking Run button. Script begins executing immediately after I press key "R" down and that script sends keystroke "S" before I release Cmd or R. That's why sent keystroke "S" interprets by TextEdit with modifier Cmd.

The workaround is to click button Run or add delay at the beginning of script and use Cmd+R:

delay 0.2 -- 0.2 second delay is enough

tell application "TextEdit"
    activate
like image 187
Andrew Bridge Avatar answered Mar 07 '23 10:03

Andrew Bridge