0

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.

4
  • What's the result? Does it beep or give an error? Just guessing but is it possible TextEdit isn't active yet when the keystroke is executed? Can you try inserting the line delay 1 between the activate and the keystroke line? (causes a 1 second delay, increase/decrease the number if necessary)
    – Arthur
    Oct 2, 2015 at 21:30
  • The result is the document opens....and highlights all the spelling errors. Even delay 5 doesn't help.
    – Jimmbo
    Oct 3, 2015 at 15:04
  • Re your edit: It should still work with custom shortcuts. In fact, to test I set up the exact shortcut you mentioned and using sufficient delay it works without problems for me. Alternatively, try selecting the menu item by its name. You should find pointers how to do that by googling (sorry, I'm not at my computer currently).
    – Arthur
    Oct 3, 2015 at 15:15
  • Perhaps it's not accepting the custom shortcut because the applescript sends that shortcut to "System Events" rather than to "Text Edit". in any case, yes, menu item selection works. I'll reply to my post to show the winning script.
    – Jimmbo
    Oct 4, 2015 at 19:52

3 Answers 3

1

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
3
  • And, sigh, after leaving the document open for a couple minutes, I came back to it and....spelling errors are highlighted after all. It takes an unpredictable length of time for the highlights to appear, which also might account for some of the confusion in the replies.
    – Jimmbo
    Oct 4, 2015 at 19:56
  • I still think it's the unpredictability of the initial state of that toggle. I don't think Applescript can do if(toggle is off) {switch on} though - 'cos that would be the fix.
    – Tetsujin
    Oct 5, 2015 at 6:30
  • or maybe it can... stackoverflow.com/questions/16504541/… I'll have a think about this, after another couple of cups of coffee ;)
    – Tetsujin
    Oct 5, 2015 at 6:32
0

Note added after comments...
It may not be the delay that is causing the issue - it looks more likely to be the difficulty in determining the initial state of the auto-check switch.


I do think 'delay' is your actual issue, as Arthur mentioned, but you can hone that script down to 3 lines, even adding the delay
If you're running from HD rather than SSD, you may need to lift the delay value to 2 or 3, just to give the document time to open & come to the front.

tell application "Finder" to open file ((path to documents folder as text) & "reference.rtf")
delay 1
tell application "System Events" to keystroke "s" using {command down, option down, control down}

I'm assuming your users/mystuff/Documents path is your regular ~/Documents folder with the account name changed. My new version doesn't rely on your user name being in the script, it will just use the current user's ~/Documents folder

7
  • Per my comment above, delays don't help (and it's a SSD). The document opens, but spelling errors are highlighted.
    – Jimmbo
    Oct 3, 2015 at 15:04
  • I wouldn't have posted it if it didn't work for me ;-) My guess, then, is your default is opposite to the result you're trying to achieve. It's perfectly possible to have spellcheck on for one document & off for another. Check what is shows as being when it is opened without the key command to change it. Also check that you can actually see the menu command being activated - the Edit menu should briefly flick blue.
    – Tetsujin
    Oct 3, 2015 at 15:39
  • It appears that TextEdit retains the state of Check Spelling at Quit & after next launch, all opening documents start with that setting, until it is re-set at next Quit. I know of no way to check the state & switch only if on, by Applescript.
    – Tetsujin
    Oct 3, 2015 at 15:49
  • Further research - it appears the state of that switch may be saved within the document itself, or at least in the app's saved.state file. It might be just a 'last set' switch, remembered as whatever the last document closed at quit was set to.
    – Tetsujin
    Oct 3, 2015 at 16:00
  • Last gasp attempt - this shows all the toggleable switches - superuser.com/questions/367533/… - but none of them work for TextEdit if set as defaults read/write in Terminal, I tested. Needs someone smarter than me in Terminal to check the current state
    – Tetsujin
    Oct 3, 2015 at 16:21
0

I'm adding this as a second answer, as I think the research in getting here is worth keeping in the flow.

Using a lot of Google-Fu & Apple's own UI helper, UIElementInspector, which I obtained via FDMlib (which links directly to Apple for the download) I managed to cobble this together...

tell application "Finder" to open file ((path to documents folder as text) & "reference.rtf")
delay 1

tell application "System Events" to tell process "TextEdit"
    set v to (value of attribute "AXMenuItemMarkChar" of 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)

    if v = "✓" then
        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 if
end tell

The 'set v to...' is the part that examines the current toggle state of that menu item. It's a bit rough & ready, but it works by checking for the actual checkmark on the menu item itself. If it's not checked it does nothing, if it is, it hands over to be switched off.

I'm sure a proper scripter could refine this in a dozen ways - but it works :-)

14
  • It seemed to work the first time I ran it. The second, third, and fourth time, spelling errors were once again highlighted. For testing, you need to wait as long as 30 secs for highlighting to appear; it seems to be unpredictable.
    – Jimmbo
    Oct 7, 2015 at 15:16
  • how large is your file? I'm back to thinking it might need more delay again - I tested this one til I was certain, whichever state the switch was in, it would leave it the right way.
    – Tetsujin
    Oct 7, 2015 at 16:59
  • It's a large file.
    – Jimmbo
    Oct 7, 2015 at 20:58
  • I changed it to "delay 10". First launch worked. Subsequent ones showed spelling errors.
    – Jimmbo
    Oct 7, 2015 at 21:05
  • Then I give up. This works, repeatedly & predictably here, whatever the state of the toggle before opening the test file, & tbh, you're not really helping me out here, Took me 4 hours to research that version & you're not even telling me how you're testing it, whether it is specifically not toggling, toggling the wrong way, acting randomly, works on a smaller file... there's nothing more I can do.
    – Tetsujin
    Oct 8, 2015 at 5:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .