Trigger launchd Agent When Frontmost App Changes?

Greetings, folks!

Can I set up a launchd agent (or eq.) to change the keyboard layout (or run some other [shell] script) when I change the frontmost application, i.e. switch between apps? I use different keyboard layouts and need to change them for different apps and also might like to use such for other purposes as well. Switching the keyboard layout manually is just so . . . déclassé. :wink:



Richard Fairbanks


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.”;
“” = “German”;
“” = “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

Bless you, Stefan! (yet again!!)

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:

“default” = “U.S.”;
“” = “German”;
“” = “German”;

Switching between Safari and Finder alternates the language between German and U.S., instead of remaining in German.


Bless you!!

Model: 2.33GHz MBPC2D Running OS X 10.6.8
Browser: Google Chrome 14.0.835.94
Operating System: Mac OS X (10.6)

Try it again, same link

What a Godsend . . .



Very useful.

I am Canadian French however. The keyboard I’m using is “Français canadien - CSA”. I need to switch between this one and US Extended all the time.

My issue is that I don’t know which tag to put between the quotation marks.

I tried: “default” = “CA-FR” and: “default” = “CSA”, etc.

No results.

Is there a list available, so I could put the right keyboard name?



Actually the identifiers expect the english localization

Canadian French - CSA

PS: I added a menu “Languages” which displays the proper names of the available languages.

Same link

It has been quite a blessing, Stefan!

Also, by opening the app (control-click for “Show Package Contents”) and using Apple’s Property List Editor to edit, 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!

The applescript:

quit application "InputSwitcher"

will quit it.

Thanks, Stefan!!