2

I often need to change the spelling language in OS X from English to Turkish and vice versa, how can I do it with an AppleScript?

Note: I have read this question already, but in there, the answer suggests using Automatic by Language, which does not work properly, so I need to make these specific changes.

I'm using a Mid-2015 MacBook Air with OS X El Capitan 10.11.6.

  • 1
    I've updated the answer with some info about using it as a AppleScript Application and also how to use it as an Automator Service with a keyboard shortcuts. – user3439894 Oct 4 '17 at 16:56
3

The AppleScript code, below, employs UI Scripting and because a popup menu is involved, System Preferences will be made visible (activate) during the process. Had a popup menu not been involved, this could have been done without making it visible.

That said, technically it still can be done without making the primary window visible, but then all you would see is the popup menu flash for a moment on the screen. As this might be more of an annoyance, wondering what just flashed on the screen, showing the whole System Preferences window is probably less of an annoyance. You can see the difference and try it by commenting out activate, e.g. -- activate, and choose which you prefer.

The AppleScript code does the following:

  • Opens System Preferences to: Keyboard > Text
  • Changed the language in the Spelling: popup menu based on the following:
    • If set to Automatic by Language, changes it to U.S. English.
    • If set to U.S. English, changes it to Türkçe.
    • If set to Türkçe, changes it to U.S. English.
  • Closes System Preferences.

Essentially, once it is not set to Automatic by Language, it then toggles between U.S. English and Türkçe. each time it's run.

You can save this as an AppleScript application, place it in the Dock, then each time it's clicked it will toggle between the two languages. (Once it's first set to one of the languages.)

Note: Saved an AppleScript application it will need to be added to System Preferences > Security & Privacy > Privacy > Accessibility, adding the app to the Allow the apps below to control your computer list.

This also could be made into an Automator service, where a keyboard shortcut could be assigned. (See the directions at the end of this answer.)

Note: The following AppleScript Code was tested under OS X El Capitan 10.11.6 and may need to be reworked for use with other versions of OS X/macOS.

AppleScript Code:

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

Note: Because UI Scripting can sometimes be dependent on the timing of things, the value of the delay command may need to be adjusted and or additional delay commands may need to be added as/if necessary when run on your system. Make those changes as appropriate.

Also, if you want to make it just always toggle between the two languages, having already set it to one or the other under normal circumstances, you can remove the following lines of code from the if statement block in the script:

else if (value) is "Automatic by Language" then
    click
    click menu item "U.S. English" of menu 1

To use this AppleScript code as an Automator Service, do the following:

  • In Automator, select: File > New > Service
  • Set: Service receives no input in any application
  • Add a Run AppleScript Action.
    • Delete the lines containing (* Your script goes here *) and return input.
    • Paste in the AppleScript code within the on run handler.
  • Save the Automator Service, giving it an appropriate name, e.g.: Toggle Spelling Language

In System Preferences > Keyboard > Shortcuts > Services > (Name You Gave The Service), you can add the keyboard shortcut to trigger this service.

You must log in to answer this question.

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