Services keyboard shortcuts are a bit different than Application-specific or system-wide shortcuts, although they can be managed using defaults read
and defaults write
.
The easiest way I've found to figure this sort of thing out is:
defaults read > /tmp/a
Change the keyboard shortcut manually using System Preferences
defaults read > /tmp/b
diff /tmp/[ab]
For the specific case of keyboard shortcuts for Services, in OS-X 10.8 the important domains are "com.apple.ServicesMenu.Services" and "pbs". You need to look at the
com.apple.ServicesMenu.Services domain to get the information needed to create an entry in the pbs domain.
For example:
The CFVendedServices
key from com.apple.ServicesMenu.Services
for "Unarchive to Current Folder is:
{
NSBundleIdentifier = "cx.c3.theunarchiver";
NSBundlePath = "/opt/homebrew-cask/Caskroom/the-unarchiver/3.9.1/The Unarchiver.app";
NSKeyEquivalent = { };
NSMenuItem = {
default = "The Unarchiver/Unarchive To Current Folder";
};
NSMessage = unarchiveToCurrentFolderWithPasteboard;
NSPortName = "The Unarchiver";
NSSendTypes = (
NSFilenamesPboardType
);
NSUUID = <be1910e8 cb1a3475 90854bc5 09f0c0ab>;
},
To create a keyboard shortcut for it, the pbs NSServices key is constructed from the NSBundleIdentifier, NSMenuItem and NSMessage. Example:
defaults read pbs
{
"cx.c3.theunarchiver - The Unarchiver/Unarchive To Current Folder - unarchiveToCurrentFolderWithPasteboard" = {
"enabled_context_menu" = 1;
"enabled_services_menu" = 1;
"key_equivalent" = "@~^u";
};
"org.vim.MacVim - MacVim/New MacVim Buffer Here - newFileHere" = {
"key_equivalent" = "@~^v";
};
};
}
Unfortunately, the pbs domain is constructed as nested dictionaries which cannot be handled by defaults write
like this simpler cases for other keyboard shortcuts. You'll need to read the entire NSServicesStatus key, insert your new entry, then write it back:
defaults write pbs NSServicesStatus '{
"com.cocoatech.PathFinder - Path Finder/Show Info - getInfoInPathFinderService" = {
"key_equivalent" = "@~^$i";
};
"cx.c3.theunarchiver - The Unarchiver/Unarchive To Current Folder - unarchiveToCurrentFolderWithPasteboard" = {
"enabled_context_menu" = 1;
"enabled_services_menu" = 1;
"key_equivalent" = "@~^u";
};
"org.vim.MacVim - MacVim/New MacVim Buffer Here - newFileHere" = {
"key_equivalent" = "@~^v";
};
}'
It may also be necessary to force pbs to flush its cache:
/System/Library/CoreServices/pbs -flush