Change language for already entered text

I was looking for a way to automate something i have to do quite often. I’m working in two languages and quite often it happens that i write something in Russian in Pages and then need to research something online, so i go to Chrome switch language to English and do my research. Then when i go back to Pages and if i forget to switch back to Russian and start typing i might type 3 to 4 symbols in English expecting Russian letters to appear. So i’d have to switch to Russian, backspace 3 or 4 letters and retype in actual Russian. So i was wondering if i could automate this. By pressing a shortcut KM would backspace last entered word (something like back until whitespace or line break) switch input language to Russian and type exactly the same symbols but in Russian locale.

Hey RuslanI,

If you have the Input menu visible you can change languages with AppleScript and System Events.

It’s a trifle awkward, because it drives the user-interface – but it’s faster than doing it manually.

You can try it out in Script Editor.app.

Use an Execute AppleScript Action (text) in your macro.

------------------------------------------------------------
# Change Input Menu Language
------------------------------------------------------------
tell application "System Events"
  if quit delay ≠ 0 then set quit delay to 0
  
  tell application process "SystemUIServer"
    tell menu bar 1
      try
        set inputMenu to first menu bar item whose accessibility description is "text input"
      on error
        error "The Input Menu is probably not visible in the menu bar."
      end try
    end tell
  end tell
  
  tell inputMenu
    perform action "AXPress"
    tell menu 1
      tell menu item "U.S."
        perform action "AXPress"
      end tell
    end tell
  end tell
  
end tell
------------------------------------------------------------

Then probably a Type a Keystroke action to type Opt-Delete.

You’ll have to adjust the language in the AppleScript. I don’t have Russian on my system to test with.

There could be some other tweaking necessary due to system language differences, but we’ll hope it works the first time.

This is tested with KM 7.0.1 and OSX 10.10.5.

-Chris

Hello Chris,
Thank you for your input. This method indeed works to switch keyboard layouts. The biggest hurdle for me is to figure out how to make KM type exactly the same text that’s been deleted but in Russian locale. In other words, i want KM to switch keyboard layout to Russian and then press exactly the same keys.
So if i typed “ghbdtn” i need KM to delete these 6 characters, switch to Russian and then press exactly the same keys in the same order. But since the keyboard layout is already in Russian the KM would type proper Russian word (it would be “привет” which is “hello”).

Ruslani, I don’t have the expertise for this, but wouldn’t it be more efficient to have KM switch the languages when you change the pages from foreground to the background? That way you’re always typing in the language you’re thinking in?

Hey RuslanI,

I know a way to do this, although it's a trifle complicated.

I've got a question pending on the Applescript Users List that may uncomplicate it a bit, and hopefully I'll get an answer soon.

-Chris

Hey RuslanI,

You are aware of this option in System Prefs > Keyboard > Input Sources?

If not it might make some things more convienient for you.

Keyboard Maestro “Translate Characters from English to Russian” Macro

Okay, let's forget about trying to recreate key-code-based key-presses and just translate characters.

You may need to add things to the translation table in the first AppleScript, but this should get you started.

Translate Characters from English to Russian & Change Input Language.kmmacros (6.9 KB)

—Chris

Peter_Morgan
Yes, that’s a possible option but a very rigid one. I was looking for a more flexible solution that would work across the whole OS even in the same application.

Chris,
Thank you for your help. This KM script is really cool! It is definitely a start for me, i’ll tinker with it. So basically you’re just remapping the characters?
I am aware of the option of automatically switching to document input source but for some reason this worked very inconsistently for me in some programs (like Alfred) and would switch the language when i did not want it to.
BTW, have you seen this: Change input source
This will let you switch input source without involving GUI elements.

Hey RuslanI,

The word remap would tend to suggest that I'm doing something with the keyboard, but in this case I'm really just doing a character-level find/replace on a string and pasting it back in.

Yes. Unfortunately that is low level stuff:

From a friend of mine who's an ASObjC Guru:

“You can't do that from ASObjC -- it's a C-based Carbon API. Very few functions involving CF (Core Foundation) references are accessible for ASObjC.”

So this can't be done with the shell or with plain AppleScript or with AppleScript-Objective-C.

However.

I got him to add a programming method to his BridgePlus ASObjC-Library to make this accessible to AppleScript.

The script to change input-methods looks like this:

use framework "Foundation"
use script "BridgePlus"
load framework

set langSwitchResult to current application's SMSForder's changeInputSourceTo:"com.apple.keylayout.Russian"

HOWEVER – the above script will not work unless the BridgePlus ASObjC library is installed on your system.

AND the current release version does not contain the feature.

It's in beta right now, although I think Shane will release it very soon. (I've asked him when it will go final but haven't heard back yet.)

So – in summation:

It will shortly be possible to smoothly and seamlessly switch language inputs using AppleScript.

I will post here when the new version of BridgePlus is available and will also provide a working sample macro.

-Chris

Hey RuslanI,

Okay, the new version is out.

BridgePlus Script Library v1.2.0:
http://macosxautomation.com/applescript/apps/BridgePlus.html
http://macosxautomation.com/applescript/apps/BridgePlus_stuff.zip

It should be installed on your system here:

~/Library/Script Libraries/BridgePlus.scptd

I’ve tested on my 10.10.5 system, and it works like a charm.

-Chris

Hi Chris!
Thanks for this!
I tested it in El Capitan and it works great!