hgiesel
hgiesel

Reputation: 5648

Is there a way to change keyboard shortcuts with shell in OS X

I have written some defaults scripts to have some standard settings I can port to other systems or re-install in the case of a computer crash.

Is there a defaults domain or some other way to add/delete keyboard shortcuts on OS X programmatically?

EDIT:

OK, I know there is com.apple.symbolichotkeys, but how do I write to it? I know that I have to use -string to write a string to an option, like so:

defaults write com.yourdomain.appname variable -string value

But this seems to be an array, containing several values, one of which is also an array. Of course I could just copy that content and write to the defaults file itself, but I would like to do that in the form of a shell command.

ALSO:

Is this really going to be portable? As I don't understand the contents of com.apple.symbolichotkeys, it might as well be non-portable.

Upvotes: 4

Views: 1506

Answers (2)

Michał Ziobro
Michał Ziobro

Reputation: 11762

To write to com.apple.symbolichotkeys defaults you can use from terminal such command:

defaults write com.apple.symbolichotkeys AppleSymbolicHotKeys 
-dict-add 73 "{enabled =1; value = { parameters = (65535, 53, 1048576);
 type = 'standard';}; }"

You can also use NSUserDefaults in Objective-C (I think also Swift)

And there is also C equivalent:

CFPreferencesSetAppValue( KEY, VALUE, CFSTR("com.apple.symbolichotkeys") );
CFPreferencesAppSynchronize( CFSTR("com.apple.symbolichotkeys") );

Upvotes: 1

rob mayoff
rob mayoff

Reputation: 385670

Since the structure of com.apple.symbolichotkeys isn't documented and is full of mysterious numbers, your best bet is to use System Preferences > Keyboard > Shortcuts to configure your hot keys, then export your configuration as XML:

defaults export com.apple.symbolichotkeys symbolichotkeys.plist

Add symbolichotkeys.plist to your setup scripts collection. To load it:

defaults import com.apple.symbolichotkeys symbolichotkeys.plist

Upvotes: 2

Related Questions