r/applescript • • Aug 18 '22

Script to change spelling language

I work in two languages (English and French) and frequently have to switch back and fort between the two. I already have scripts to change the spelling language (written in Automator and imported into Shortcuts), but those scripts actually open System Preferences, navigate to Keyboard menu > Text tab and select the correct language in the dropdown, then quit System preferences. It looks like this:

https://i.imgur.com/7ONWV9e.gif

The scripts to set the language to FR:

tell application "System Preferences"
activate
reveal anchor "Text" of pane id "com.apple.preference.keyboard"
delay 0.5
tell application "System Events"
delay 0.5
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 "Français" of menu 1
end if
if (value) is "Automatic by Language" then
click
click menu item "Français" of menu 1
end if
end tell
end tell
quit
end tell

The script to set the language to EN just swaps "U.S. English" and "Français" .

> Is there a way to modify the script so that the operation is done in the background without opening the GUI?

Thx.

3 Upvotes

8 comments sorted by

View all comments

Show parent comment

1

u/CounterBJJ Aug 19 '22

Thanks so much for taking the time to do this! I just changed GB English to US English (I am a translator and work almost exclusively with US English documents).

One question regarding the script above: Without much GUI involvement, it's difficult to know what language was just toggled on since there isn't a way in macOS to display the current spelling language in the menu bar or any location readily viewable as far as I know. That's why I have been using 2 separate scripts. How would I need to modify the script above to split it into 2 and turn on each language separately? Of course no need to spend any time on the above if you think you can make the non-GUI method work, but the same thing will apply there and 2 separate scripts will be needed (unless you can think of another way to solve the issue).

Thanks again!

1

u/ChristoferK Aug 21 '22

You don't need two scripts, you can just place reusable code inside a handler and call the handler with the desired language. I'll show you how to do this as soon as I can.

1

u/CounterBJJ Aug 21 '22

Sounds good. Thank you.