Change keyboard whenever iTerm is selected Using either Applescript or Automator is it possible to switch the software keyboard(Input Source) to a certain keyboard(input source) whenever iTerm becomes foreground?

Scenario is as follows


I click on iTerm window
iTerm window goes foreground
A script changes the keyboard(input source) to one that I specify
You could for example save this plist as ~/Library/LaunchAgents/itermchangeinput.plist:

&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
&lt;plist version="1.0"&gt;
&lt;dict&gt;
  &lt;key&gt;Label&lt;/key&gt;
  &lt;string&gt;itermchangeinput&lt;/string&gt;
  &lt;key&gt;ProgramArguments&lt;/key&gt;
  &lt;array&gt;
    &lt;string&gt;osascript&lt;/string&gt;
    &lt;string&gt;-e&lt;/string&gt;
    &lt;string&gt;set prev to missing value
repeat
    set cur to name of application (path to frontmost application as text)
    if cur is "iTerm" and prev is not "iTerm" then do shell script "changeInput U.S."
    delay 1
    set prev to cur
end repeat&lt;/string&gt;
  &lt;/array&gt;
  &lt;key&gt;RunAtLoad&lt;/key&gt;
  &lt;true/&gt;
  &lt;key&gt;KeepAlive&lt;/key&gt;
  &lt;true/&gt;
&lt;/dict&gt;
&lt;/plist&gt;


Then run launchctl load ~/Library/LaunchAgents/itermchangeinput.plist.

You could also use a shell script like this:

while sleep 1;do cur=$(lsappinfo find ASN=$(lsappinfo front)|cut -d\" -f2);[[ $cur = iTerm &amp;&amp; $prev != iTerm ]]&amp;&amp;changeInput U.S.;prev=$cur;done

It might be less efficient though because it executes two new processes every second.

The original blog post about changeInput was deleted by the author, but I uploaded the binary to http://19a5b0.s3-website-us-west-2.amazonaws.com/changeInput.tgz.

If you always switch to iTerm with a keyboard shortcut (like F7), you could use a private.xml like this with KeyRemap4MacBook:

&lt;?xml version="1.0"?&gt;
&lt;root&gt;
  &lt;vkopenurldef&gt;
    &lt;name&gt;KeyCode::VK_OPEN_URL_ITERM&lt;/name&gt;
    &lt;url&gt;file:///Applications/iTerm.app&lt;/url&gt;
  &lt;/vkopenurldef&gt;
  &lt;inputsourcedef&gt;
    &lt;name&gt;US&lt;/name&gt;
    &lt;inputsourceid_prefix&gt;com.apple.keylayout.US&lt;/inputsourceid_prefix&gt;
  &lt;/inputsourcedef&gt;
  &lt;item&gt;
    &lt;name&gt;test&lt;/name&gt;
    &lt;identifier&gt;test&lt;/identifier&gt;
    &lt;autogen&gt;__KeyToKey__ KeyCode::F7, ModifierFlag::NONE, VK_OPEN_URL_ITERM, VK_CHANGE_INPUTMODE_US&lt;/autogen&gt;
  &lt;/item&gt;
&lt;/root&gt;


You can see the IDs of input sources with EventViewer (/Applications/KeyRemap4MacBook.app/Contents/Applications/EventViewer.app/).