AppleScript changing Input Source when Application is activated

I have several macros with AppleScripts changing the Input Source as I open certain apps.
It works like a charm, but I would like the script to check if the Input Source in question is already in use, and in that case do nothing.
See below the script I’m using at the moment.
Any thoughts?

Thanks,
Hakan

on changeKeyboardLayout(layoutName)
    tell application "System Events" to tell process "SystemUIServer"
        tell (1st menu bar item of menu bar 1 whose description is "text input") to {click, click (menu 1's menu item layoutName)}
    end tell
end changeKeyboardLayout

changeKeyboardLayout("U.S.")

Hello Hakan,

See this thread:

-Chris

Thanks a lot Chris, but the script in the link does the same thing as the one I already use.
I want the script to do nothing at all if the Input Source in question is already in use.

Hello Hakan,

The point was not the script; it was the BridgePlus Library.

With that you can write your own script without having to fool with System Events.

You HAVE to install the BridgePlus Library in order for this script to work.

See “Deployment” on this page:

http://macosxautomation.com/applescript/apps/BridgePlus.html

------------------------------------------------------------
use framework "Foundation"
use script "BridgePlus"
load framework
use scripting additions

set currentLayout to current application's SMSForder's currentKeyboardInputSourceID as text

if currentLayout is not "com.apple.keylayout.US" then
  set langSwitchResult to current application's SMSForder's changeInputSourceTo:"com.apple.keylayout.US"
else
  # Comment-out the beep to make the script do nothing if nothing is to be changed.
  beep
end if
------------------------------------------------------------

You can get a list of the available input IDs by running this:

------------------------------------------------------------
use framework "Foundation"
use script "BridgePlus"
load framework
use scripting additions

set currentLayout to current application's SMSForder's availableInputSourceIDs as list
------------------------------------------------------------

-Chris

2 Likes

Ok, I see.
Thank you Chris!

Perhaps you can try “Set Keyboard Layout” action. It can be used to change input source. Change input source

Thanks you, YuktimH.

I saw that post when I searched before I posted myself, but I don’t have that action.
I’m on KM 6.4.8 since I’m still on Mavericks, if that has anything to do with it.

I also delayed upgrading to Yosemite, but it has been a painless experience, and some great new benefits, KM 7 being one of them. :wink:

You may be interested in this thread I started just before I upgraded: