4

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.

hgiesel
  • 5,430
  • 2
  • 29
  • 56
  • 1
    Writing to com.apple.symbolichotkeys.plist with defaults while it can add/modify the information just as if it was done via System Preferences it doesn't however do everything that is done via the GUI and would require writing something in Objective-C for the changes to actually take place. Exporting/importing as suggested rob mayoff will not work in this particular case. – user3439894 Jan 02 '16 at 02:52
  • I have found some other settings in .GlobalPreferences – hgiesel Jan 02 '16 at 03:29
  • 1
    See [this question on Apple.SE](http://apple.stackexchange.com/questions/201816/how-do-i-change-mission-control-shortcuts-from-the-command-line) and [this SO question](http://stackoverflow.com/questions/21878482/what-do-the-parameter-values-in-applesymbolichotkeys-plist-dict-represent). – Gordon Davisson Jan 02 '16 at 03:47
  • @user3439894: `defaults import ...` works in principle, but requires logging out or rebooting for the changes to take effect. – mklement0 Sep 04 '16 at 21:17

2 Answers2

2

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
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • Seems arbitrary to me as most of the default subsystem is pretty much straightforward, but thank you – hgiesel Jan 02 '16 at 02:45
  • 1
    ++ for a helpful and pragmatic solution, which, however, requires logging out or rebooting for the changes to take effect. Do you know of a way around this? – mklement0 Sep 04 '16 at 21:15
  • 2
    Quibble: `defaults export com.apple.symbolichotkeys symbolichotkeys.plist` creates a _binary_ plist file (at least as of OSX 10.11); use `defaults export com.apple.symbolichotkeys - >symbolichotkeys.plist` to create an XML file. – mklement0 Sep 04 '16 at 21:33
1

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") );
Michał Ziobro
  • 10,759
  • 11
  • 88
  • 143
  • 1
    The OP is looking for a command that can be used from a _shell script_, and while the `defaults` command should work - assuming you figure out all the arcane constants - you still need a logout or reboot for the changes to take effect. – mklement0 Sep 04 '16 at 21:18
  • Why logout or reboot? Isn't it changing in the live time, I will test it today but I think that change is reflected after defaults command execution in .plist Preferences file and shortcut can be used from that moment – Michał Ziobro Sep 05 '16 at 09:05
  • Using `defaults` commits changes to _disk_, but you also need to _notify the process_ whose defaults have been changed in order to pick up the changes - I don't know what process that is, and how to get it to re-read its preferences while _running_ (calling `CFPreferenceAppSynchronize` with application ID `com.apple.symbolichotkeys` appears _not_ to work). Logging out / rebooting, however, causes the process (via the `cfprefsd` daemon / agent) to pick up the changes from disk. – mklement0 Sep 05 '16 at 16:33
  • 2
    Yeah I have tested it and it of course commit changes on the disk in .plist file but as You have said it doesn't get reflected in Keyboard Shortcuts pane in System Preferences. So any idea how to notify system to reload changes. – Michał Ziobro Sep 05 '16 at 17:49