开发者

Programmatically add keyboard shortcut to Mac System Preferences

开发者 https://www.devze.com 2023-03-31 11:53 出处:网络
If I go to System Preferences, Keyboard, Keyboard Shortcuts, then Applicat开发者_如何学JAVAion Shortcuts, I can define custom shortcuts to be used on a Mac.

If I go to System Preferences, Keyboard, Keyboard Shortcuts, then Applicat开发者_如何学JAVAion Shortcuts, I can define custom shortcuts to be used on a Mac.

Any way to access this functionality via Applescript?


The shortcuts are stored in NSUserKeyEquivalents dictionaries in ~/Library/Preferences/.GlobalPreferences.plist and the property lists of applications.

defaults write -g NSUserKeyEquivalents -dict-add Duplicate '~@d' Minimize '\0'
defaults write com.apple.finder NSUserKeyEquivalents '{"Show Package Contents"="@\r";}'

The shortcut format is described in the Cocoa Text System article.


System Preferences isn't scriptable with Applescript natively, you have to interact with it by GUI Scripting. GUI Scripting is really a last resort that allows you to interact with controls by explicitly declaring and calling them, and it doesn't take much to throw off a GUI script. It is difficult to implement even for experienced Applescript programmers. You'll find a few introductory tutorials out there in the web, but not much else.


The internals of the Mac OS are quickly becoming a lost art. Run the following in a shell, or run via shell in Applescript:

defaults write com.google.Chrome NSUserKeyEquivalents '{ "Search the Web…" = "@k"; }'


Also good to know: shortcuts for Services are in the pbs.plist, and they're stored using a different property than the NSUserKeyEquivalents.

There's info on how to write a basic script to change/add shortcuts for Services here:

Set Services keyboard shortcut via script OSX

This was the method I found the simplest, and it worked well for me (macOS 10.12.6 Sierra):

You can also use PlistBuddy and defaults:

/usr/libexec/PlistBuddy -c 'Delete NSServicesStatus:"(null) - test2 - runWorkflowAsService"' ~/Library/Preferences/pbs.plist 2>/dev/null;defaults write pbs NSServicesStatus -dict-add '"(null) - test2 - runWorkflowAsService"' '{key_equivalent = "^~@2";}'

The PlistBuddy command is not needed if there is not an existing entry for the service. Replace test2 with the name of the service. Quit and reopen applications to apply the changes.

^~@2 is control-option-command-2. See http://osxnotes.net/keybindings.html.

And in case this might help those who are a bit less familiar with writing/executing scripts on macOS, I saved the script as a shell script (.sh extension), and ran it from Terminal using the following commannd:

sh "/path/to/script/scriptfile.sh"


I have written an AppleScript library to programmatically add Global & Application shortcuts. It took a tremendous amount of effort to work around the various quirks in how macOS implements shortcuts.

Version 1.0 is available here: https://forum.latenightsw.com/t/setting-other-applications-keyboard-shortcuts-using-nsuserdefaults-defaults-not-updating/3537/5

I will be making some minor API additions & changes when I find the time, and will post the source code here in full at that time. Note that as mentioned above, Services shortcuts are stored using a completely different system & are not handled here.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号