Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically add keyboard shortcut to Mac System Preferences

If I go to System Preferences, Keyboard, Keyboard Shortcuts, then Application Shortcuts, I can define custom shortcuts to be used on a Mac.

Any way to access this functionality via Applescript?

like image 547
Monopol Avatar asked Aug 28 '11 05:08

Monopol


People also ask

How do you assign a keyboard on a Mac?

On your Mac, choose Apple menu > System Preferences, then click Keyboard . Click Change Keyboard Type, then follow the onscreen instructions.

How do I assign a keyboard shortcut in Word for Mac?

Create a keyboard shortcutOn the Tools menu, select Customize Keyboard. In the Categories list, select a tab name. In the Commands list, select the command that you want to assign a keyboard shortcut to. Any keyboard shortcuts that are currently assigned to the selected command will appear in the Current keys box.


2 Answers

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.

like image 102
Lri Avatar answered Oct 19 '22 17:10

Lri


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.

like image 23
Philip Regan Avatar answered Oct 19 '22 18:10

Philip Regan