MacOS – Applescript Won’t Invoke Shortcut Command

applescriptkeyboardmacostextedit

I have a long TextEdit document I frequently open (via a keystroke, thanks to Alfred). It contains many non-standard spellings, so I de-select "Check Spelling While Typing" (though I want spell checking to be default on with other TextEdit documents). Note that I've created the shortcut "Command/Option/Control – S" (in Prefs/Keyboard/Shortcuts) to toggle that command.

To save myself a step, I want to create an applescript to open the document and trigger that command. But I just can't make it work.

The document opens, but "Check Spelling While Typing" won't toggle off.

tell application "Finder"
activate
open document file "reference.rtf" of folder "Documents" of folder "MYSTUFF" of folder "Users" of startup disk
end tell

tell application "TextEdit" to activate
tell application "System Events"
keystroke "s" using {command down, option down, control down}
end tell

==============

EDIT:

I've inserted "Delay", but that doesn't help; the spellings are highlighted.

Is it possible Applescripts for some reason can't invoke custom shortcuts within applications (that have been rigged up via Prefs/Keyboard/Shortcuts)?

Is there another way to invoke the "Check Spelling While Typing" command? It doesn't seem to be one of TextEdit's scriptable commands.

Best Answer

I suspect it wasn't accepting the custom shortcut because the shortcut's solely used in TextEdit, whereas the script was sending it to System Events.

Whether that's true or not, the following script works. It chooses the menu item rather than triggering it via shortcut:

tell application "Finder"
activate
open document file "reference.rtf" of folder "Documents" of folder "MYSTUFF" of folder "Users" of startup disk
end tell
delay 1
activate application "TextEdit"
delay 1
tell application "System Events"
tell process "TextEdit"


click menu item "Check Spelling While Typing" of menu 1 of menu item "Spelling and Grammar" of menu 1 of menu bar item "Edit" of menu bar 1

end tell
end tell