Skip to content

Instantly share code, notes, and snippets.

@zhanggang807
Created January 26, 2018 07:03
Show Gist options
  • Save zhanggang807/e3a9683a833b56442ae722150b916452 to your computer and use it in GitHub Desktop.
Save zhanggang807/e3a9683a833b56442ae722150b916452 to your computer and use it in GitHub Desktop.
im-select:change mac input method via command line. copied from https://github.com/CodeFalling/smart-im-osx
//
// im-select
//
// Created by Ying Bian on 8/21/12.
// Copyright (c) 2012 Ying Bian. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <Carbon/Carbon.h>
int main(int argc, const char * argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int returnCode = 0;
if (argc > 1) {
NSString *imId = [NSString stringWithUTF8String:argv[1]];
NSDictionary *filter = [NSDictionary dictionaryWithObject:imId forKey:(NSString *)kTISPropertyInputSourceID];
CFArrayRef keyboards = TISCreateInputSourceList((CFDictionaryRef)filter, false);
if (keyboards) {
TISInputSourceRef selected = (TISInputSourceRef)CFArrayGetValueAtIndex(keyboards, 0);
returnCode = TISSelectInputSource(selected);
CFRelease(keyboards);
} else {
returnCode = 1;
}
} else {
TISInputSourceRef current = TISCopyCurrentKeyboardInputSource();
NSString *sourceId = (NSString *)(TISGetInputSourceProperty(current, kTISPropertyInputSourceID));
fprintf(stdout, "%s\n", [sourceId UTF8String]);
CFRelease(current);
}
[pool release];
return returnCode;
}
CC=/usr/bin/clang
NAME=im-select
INSTALL_PATH=/usr/local/bin/
OPTS=-framework foundation -framework carbon
all:
$(CC) $(OPTS) -o $(NAME) $(NAME).m
install:$(NAME)
cp $(NAME) $(INSTALL_PATH)
uninstall:
rm $(INSTALL_PATH)$(NAME)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment