Skip to content

Instantly share code, notes, and snippets.

@ryankanno
Last active November 19, 2020 05:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryankanno/5f471ac2973b315b027bae6a7c139e98 to your computer and use it in GitHub Desktop.
Save ryankanno/5f471ac2973b315b027bae6a7c139e98 to your computer and use it in GitHub Desktop.
Small ObjectiveC program to switch your Mac Catalina input source.
#import <Foundation/Foundation.h>
#include <Carbon/Carbon.h>
void printAvailableInputSources (NSArray *availableInputSources) {
NSLog(@"Please pass in one of the following input sources:\n");
for (id object in availableInputSources) {
NSLog(@"%@", TISGetInputSourceProperty((TISInputSourceRef)object, kTISPropertyLocalizedName));
}
}
int main (int argc, const char * argv[]) {
NSArray *arguments = [[NSProcessInfo processInfo] arguments];
NSArray *availableInputSources = (NSArray *)TISCreateInputSourceList(NULL, false);
if (argc != 2) {
printAvailableInputSources(availableInputSources);
return -1;
}
TISInputSourceRef newSource = nil;
for (id object in availableInputSources) {
if ([arguments[1] isEqualToString:TISGetInputSourceProperty((TISInputSourceRef)object, kTISPropertyLocalizedName)]) {
newSource = (TISInputSourceRef) object;
break;
}
}
if (nil == newSource) {
printAvailableInputSources(availableInputSources);
return -1;
}
OSStatus status = TISSelectInputSource (newSource);
if (status != noErr) {
NSLog(@"Unable to set input source to %@ (error code: %d)", newSource, status);
return -1;
}
return 0;
}

Hic sunt dracones

My partner needed an Alfred script to switch their Mac Catalina input source to Japanese using a keyboard shortcut because of the Moonlander.

After looking at a few (outdated) solutions on their forums and on the Internet, there didn't seem to be any reliable ones, so I just cobbled a script together.

*I'm not a Objective C developer so if you have any improvements, feel free to update this.

General idea is to get a list of your available input sources, iterate until we find one that has a localized name that matches the parameter you pass in, then set the input source to it.

How to Build

If you have XCode installed and can use the command line, run the following:

clang -framework Carbon -framework Foundation input_switcher.m -o input_switcher

Then, setup Alfred to run the following commands on a key binding:

./input_switcher U.S.

./input_switcher Hiragana

Run it with no parameters to see what values you can pass in:

./input_switcher

(You'll probably need to give the binary some permissions.)

Enjoy! :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment