Question

I'm trying to find a way to programatically get/set the default OSX system keyboard shortcuts (hotkeys) found in the System Preferences -> Keyboard & Mouse -> Keyboard Shortcuts tab. I need to be able to do this in the background, so GUI scripting is not a solution.

I'm unable to find a plist or anything where this info might be stored. I tried using Instruments "File Activity" trace while using System Preferences, but again came up empty handed.

Any help is appreciated.

Was it helpful?

Solution

There is an API for this (getting, not setting).

OTHER TIPS

Ooop, I re-ran Instruments, but made sure to close out System Preferences this time, the shortcuts weren't getting written out until then.

Turns out the file is located at ~/Library/Preferences/com.apple.symbolichotkeys.plist But it's pretty cryptic. None the less, this is what I was after.

Actually there's a Plist for that, informations are stored in com.apple.symbolichotkeys AppleSymbolicHotKeys which is a complex nested dicts and lists as :

$ defaults read com.apple.symbolichotkeys AppleSymbolicHotKeys
{
    10 =     {
        enabled = 1;
        value =         {
            parameters =             (
                65535,
                96,
                8650752
            );
            type = standard;
        };
    };
    11 =     {
        enabled = 1;
        value =         {
            parameters =             (
                65535,
                97,
                8650752
            );
            type = standard;
        };
    };
[...]
}

Let's say you want to programatically modify the "Show Help Menu" shortcut in System Preferences -> Keyboard -> Shortcuts tab -> App Shortcut -> All Applications. To find the correct entry print all the Plist in a text file, modify the shortcut in the System Preferences, print again the the Plist in a second file and diff them:

$ defaults read com.apple.symbolichotkeys AppleSymbolicHotKeys > 1
$ # modify System Preferences
$ defaults read com.apple.symbolichotkeys AppleSymbolicHotKeys > 2
$ diff -U 5 1 2
--- 1   2019-05-27 23:37:58.000000000 -0300
+++ 2   2019-05-27 23:38:24.000000000 -0300
@@ -5063,13 +5063,13 @@
             };
             98 =             {
                 enabled = 1;
                 value =                 {
                     parameters =                     (
-                        32,
-                        49,
-                        524288
+                        105,
+                        34,
+                        655360
                     );
                     type = standard;
                 };
             };
         };

So the entry to be modified is 98, since it's a complex structure you'll have to use /usr/libexec/PlistBuddy to do it:

# Set "alt + Space" as shortcut for "Help menu"
/usr/libexec/PlistBuddy -c "Delete :AppleSymbolicHotKeys:98:value:parameters" ~/Library/Preferences/com.apple.symbolichotkeys.plist
/usr/libexec/PlistBuddy -c "Add :AppleSymbolicHotKeys:98:value:parameters array" ~/Library/Preferences/com.apple.symbolichotkeys.plist
/usr/libexec/PlistBuddy -c "Add :AppleSymbolicHotKeys:98:value:parameters: integer 32" ~/Library/Preferences/com.apple.symbolichotkeys.plist
/usr/libexec/PlistBuddy -c "Add :AppleSymbolicHotKeys:98:value:parameters: integer 49" ~/Library/Preferences/com.apple.symbolichotkeys.plist
/usr/libexec/PlistBuddy -c "Add :AppleSymbolicHotKeys:98:value:parameters: integer 524288" ~/Library/Preferences/com.apple.symbolichotkeys.plist
/usr/libexec/PlistBuddy -c "Delete :AppleSymbolicHotKeys:98:enabled" ~/Library/Preferences/com.apple.symbolichotkeys.plist
/usr/libexec/PlistBuddy -c "Add :AppleSymbolicHotKeys:98:enabled bool true" ~/Library/Preferences/com.apple.symbolichotkeys.plist

Note:

  • I had to delete the bool parameter in order to modify it
  • Computer must be restarted for the changes to be applied
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top