以编程方式获取/设置 Mac OSX 默认系统键盘快捷键

发布于 2024-07-18 08:40:46 字数 224 浏览 5 评论 0原文

我正在尝试找到一种方法来以编程方式获取/设置系统偏好设置 -> 中找到的默认 OSX 系统键盘快捷键(热键)。 键盘& 鼠标-> 键盘快捷键选项卡。 我需要能够在后台执行此操作,因此 GUI 脚本不是解决方案。

我无法找到 plist 或任何可能存储此信息的内容。 我尝试在使用系统偏好设置时使用仪器“文件活动”跟踪,但再次空手而归。

任何帮助表示赞赏。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

活雷疯 2024-07-25 08:40:46

实际上有一个 Plist,信息存储在 com.apple.symbolichotkeys AppleSymbolicHotKeys 中,这是一个复杂的嵌套字典和列表:

$ 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;
        };
    };
[...]
}

假设您想以编程方式修改系统中的“显示帮助菜单”快捷方式首选项-> 键盘-> 快捷方式选项卡 -> 应用程序快捷方式 -> 所有应用程序。 要找到正确的条目,请打印文本文件中的所有 Plist,请修改系统偏好设置中的快捷方式,再次打印第二个文件中的 Plist 并比较它们:

$ 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;
                 };
             };
         };

因此要修改的条目是 98 ,因为它是一个复杂的结构,您必须使用 /usr/libexec/PlistBuddy 来完成它:

# 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

注意

  • 我必须删除 bool > 要修改参数,
  • 必须重新启动计算机才能应用更改

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
离不开的别离 2024-07-25 08:40:46

哎呀,我重新运行了 Instruments,但这次确保关闭了系统偏好设置,直到那时才写出快捷方式。

事实证明该文件位于 ~/Library/Preferences/com.apple.symbolichotkeys.plist
但它非常神秘。 尽管如此,这就是我所追求的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文