as far as I know launchd cannot monitor the active process.
But it is possible with a (faceless) Cocoa application which responds to the NSWorkspaceDidActivateApplicationNotification notification.
I wrote a simple little app, you can download it here.
It requires an Intel Mac and 10.6 or higher.
To customize the app there is a file Applications.strings in Contents/Resources in the application bundle.
At the moment the file contains these string pairs
“default” = “U.S.”;
“com.apple.Safari” = “German”;
“com.apple.finder” = “U.S.”;
the first line is the default keyboard layout, change U.S. to your default value.
The next lines contain application bundle identifiers and the appropriate keyboard layouts.
If an application becomes active, which is not in the list, the layout will be reset to the default value
The challenge comes when I switch between two of the apps that are not the default and call the same language. If I change the Applications.strings file to:
Also, by opening the app (control-click for “Show Package Contents”) and using Apple’s Property List Editor to edit InputSwitcher.app/Contents/Info.plist, it is possible to make it a background app as well, such that it does not appear in the cmd-tab app list, etc.:
Click the “Add Child” button and add “LSUIElement” to the plist in the “Key” column. A long list of available options will appear and one of them is “Application is background only.” If you select that, a checkbox in the value column will immediately appear. Checking that checkbox (i.e. making it “True”) will make the app a background app!