Switch between Spelling and Grammar languages

Hey, hopefully this is the right place to ask for help.

TLDR: How do I best toggle between two languages in the Spelling and Grammar window, how do I know the state of the drop-down before making a change?

I like a lot of people, use two languages frequently. Because of dyslexia a Spellchecker is really helpful for me. And macOS can auto switch installed spellchecking languages, but my primary Language is not supported, so I had to add it manually, and I don't have auto switching. So I need to open the Spelling and Grammar "window" and select the right language in the drop down.

For years I looked if there was a shortcut, or a accessibility option.. And found nothing, the best I have is to pop up the window by a keyboard shortcut (cmd+:).

Today I decided to fix this no matter how long it takes.

To open the Spelling and Grammar window I used Select in Menu, keyboard shortcut works too.
The window opens in an "un-selected state", and can't be selected in any way I have found yet, apart from the accessibility option control+F6 "Move the focus to the floating window"

That's almost fine, the biger problem is that that Spelling and Grammar is still not the "selected" to "top window" as far as Maestro is concerned, things like move mouse to corner, move it to the corner of the original window where I triggered the macro.

Pressing "tab" works to move the selection box.. but it never goes to the Language drop-down menu. (I have "All text boxes" selected in keyboard shortcuts)

Is there a way to open that menu and "read" the selected language option without hijacking the mouse? (I store the location at the start and return the mouse at the end, I would prefer not to have to do that)

Now I pop open the window, search for the image of the Find Next button, move the mouse down and left, click it to open the dropdown, then press up arrow 5 times to "reset" the selected language, and then move it down once or twice, based on a variable that gets set after making a selection, forming what I hope is a switch. :slight_smile: This is like my third Keyboard maestro macro.

I'm hoping there's a better way. Surely some Apple Script can solve this :wink:

Cycle Spellchecker v3.kmmacros (32.1 KB)

This AppleScript solution, which you can drop into an Execute AppleScript action, uses UI Scripting to toggle between two languages:

tell application "System Preferences"
    activate
    reveal anchor "Text" of pane id "com.apple.preference.keyboard"
    delay 0.5
    tell application "System Events"
        tell pop up button 3 of tab group 1 of window 1 of application process "System Preferences"
            if (value) is "U.S. English" then
                click
                click menu item "Türkçe" of menu 1
            else if (value) is "Türkçe" then
                click
                click menu item "U.S. English" of menu 1
            else if (value) is "Automatic by Language" then
                click
                click menu item "U.S. English" of menu 1
            end if
        end tell
    end tell
    quit
end tell

Neat, didn't think to do it from there, yep it works. :+1:t3: Now to see if I can hide the Prefs panel form "jumping" at my face when I trigger that :stuck_out_tongue_winking_eye:

Is there any documentation someone can point me towards how to get the values like tell pop up button 3 of tab group 1 of window 1 of application process "System Preferences"

Update: To answer my own question about finding labels that can be used in apple scripts. https://developer.apple.com/library/archive/documentation/Accessibility/Conceptual/AccessibilityMacOSX/OSXAXTestingApps.html

Unfortunately the apple script method fails when going from U.S. English to my language "Slovenščina (Library)", but works the other way.

tell application "System Preferences"
    activate
    reveal anchor "Text" of pane id "com.apple.preference.keyboard"
    delay 0.5
    tell application "System Events"
        tell pop up button 3 of tab group 1 of window 1 of application process "System Preferences"
            if (value) is "U.S. English" then
                click
                click menu item "Slovenščina (Library)" of menu 1
            else if (value) is "Slovenščina (Library)" then
                click
                click menu item "U.S. English" of menu 1
            else if (value) is "Automatic by Language" then
                click
                click menu item "U.S. English" of menu 1
            end if
        end tell
    end tell
    quit
end tell

Yeah, I have a few options in that menu that have parenthetical sources like (Spell Catcher) that don't get selected by the script. When I drop the leading space and parenthetical phrase, the script works.

Could you please give me an example, I wasn't able to make it work.

Try changing

                click menu item "Slovenščina (Library)" of menu 1
            else if (value) is "Slovenščina (Library)" then

to

                click menu item "Slovenščina" of menu 1
            else if (value) is "Slovenščina" then