Set service keyboard shortcut and have change take effect programtically

applescriptkeyboardplistshortcut-menusystem-prefs

I have the following script to set a services shortcut:

set serviceName to "(null) - VimAnywhere - runWorkflowAsService"
set libPrefPath to POSIX path of (path to preferences folder from user domain as text)
set plistFile to "pbs.plist"
set plistPath to libPrefPath & plistFile as text
set mainPropertyItem to "NSServicesStatus"
set keyBindingKey to "key_equivalent"
set keyBinding to "^@v"

tell application "System Events"
  set gp to (get property list item serviceName of property list item mainPropertyItem of property list file plistPath)
  tell gp
    set value of property list item keyBindingKey to keyBinding
  end tell
end tell

do shell script "defaults read > /dev/null"

This sets a keyboard shortcut for a service I have defined in the appropriated property list and it even displays properly when viewing in System Preferences. The problem is, the change doesn't seem to take effect. Invoking the keyboard shortcut has no effect.

How can I set this shortcut and have it take effect immediately (completely through shell/applescript)?

Best Answer

The changes made by the AppleScript are applied for me after I quit and reopen applications. Same for this command:

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

I don't know any way to apply the changes without reopening applications. Running /System/Library/CoreServices/pbs -flush or terminating pbs doesn't seem to work.