I’m trying to write a script that will go over each service in the Shortcuts > Services
section of the Keyboard Preference Pane
, and disable it.
This is the basic command to disable a service:
defaults write pbs NSServicesStatus -dict-add SERVICE "
<dict>
<key>enabled_context_menu</key><false/>
<key>enabled_services_menu</key><false/>
<key>key_equivalent</key><string>""</string>
</dict>
"
It is inefficient to get all services upfront and put them in the script, since many services are incrementally added upon installing new software. It has to be some kind of a loop.
So far, I’ve been trying to use defaults
and awk
to get every line that contains a service.
a=$(defaults read pbs NSServicesStatus | awk 'NR%5==2' ) && echo $a
And, this is the output:
"com.ScooterSoftware.BeyondCompare - Compare Files - bcCompareFiles" = { "com.ScooterSoftware.BeyondCompare - Compare Folders - bcCompareFolders" = { "com.ScooterSoftware.BeyondCompare - Select Left File for Compare - bcSelLeftFile" = { "com.ScooterSoftware.BeyondCompare - Select Left Folder for Compare - bcSelLeftFolder" = { "com.apple.BluetoothFileExchange - Send File To Bluetooth Device - sendFileUsingBluetoothOBEXService" = { "com.apple.ChineseTextConverterService - Convert Text from Simplified to Traditional Chinese - convertTextToTraditionalChinese" = { "com.apple.ChineseTextConverterService - Convert Text from Traditional to Simplified Chinese - convertTextToSimplifiedChinese" = { "com.apple.ChineseTextConverterService - Convert Text to Full Width - convertTextToFullWidth" = { "com.apple.ChineseTextConverterService - Convert Text to Half Width - convertTextToHalfWidth" = { "com.apple.Dictionary - Look Up in Dictionary - doLookupService" = { "com.apple.FolderActionsSetup - Folder Actions Setup - openFilesFromPasteboard" = { "com.apple.Grab - Capture Selection from Screen - variableSelection" = { "com.apple.ImageCaptureService - Import Image - ImportImage" = { "com.apple.QuickTime.service.encodeSelectedAudioFiles - Encode Selected Audio Files - runWorkflowAsService" = { "com.apple.Safari - Add to Reading List - addToReadingList" = { "com.apple.Safari - Search With %WebSearchProvider@ - searchWithWebSearchProvider" = { "com.apple.ScriptEditor2 - Script Editor/Get Result of AppleScript - runAsAppleScript" = { "com.apple.SpotlightService - SEARCH_WITH_SPOTLIGHT - doSearchWithSpotlight" = { "com.apple.Stickies - Make Sticky - makeStickyFromTextService" = { "com.apple.Terminal - New Terminal Tab at Folder - newTerminalAtFolder" = { "com.apple.Terminal - New Terminal at Folder - newTerminalAtFolder" = { "com.apple.Terminal - Open man Page in Terminal - openManPage" = { "com.apple.Terminal - Search man Page Index in Terminal - searchManPages" = { "com.apple.finder - Finder/Open - open" = { "com.apple.finder - Finder/Reveal - reveal" = { "com.apple.finder - Finder/Show Info - showInfo" = { "com.apple.mail - Mail/New Email To Address - mailTo" = { "com.apple.mail - Mail/New Email With Selection - mailSelection" = { "com.apple.services.addToiTunesAsSpokenTrack - Add to iTunes as a Spoken Track - runWorkflowAsService" = { "com.apple.services.encodeSelectedVideoFiles - Encode Selected Video Files - runWorkflowAsService" = { "com.apple.services.setDesktopPicture - Set Desktop Picture - runWorkflowAsService" = { "com.apple.services.showMap - Show Map - runWorkflowAsService" = { "com.apple.systemuiserver - Open URL - openURL" = { "com.evernote.Evernote - ENApplicationServiceMenuItemTitle - serviceCreateNote" = { "com.flexibits.cardhop.mac - Send to Cardhop - sendToCardhop" = { "com.flexibits.fantastical2.mac - Send to Fantastical 2 - sendToFantastical" = { "com.houdah.HoudahSpot4 - New HoudahSpot Search - newSearch" = { "com.houdah.HoudahSpot4 - Search Folders in HoudahSpot - folderSearch" = { "com.houdah.HoudahSpot4 - Search in HoudahSpot - search" = { "com.kapeli.dashdoc - Create Snippet in Dash - createSnippetService" = { "com.kapeli.dashdoc - Look Up in Dash - lookupService" = { "net.pornel.ImageOptim - ImageOptimize - handleServices" = { }
Where each quoted string represents a service.
Here are my issues:
I’ve tried to remove all of the extraneous data and whitespaces (=
, {
and }
) with this sed
command, but couldn’t get rid of that single, trailing }
.
b=$(echo $a | sed -E -e 's/ = { / /g' ) && echo "${b}"
Even without the trailing }
(which would yield an error only in the last iteration anyway, or so I believe), not a single string is parsed correctly within this loop I made:
for service in "${b}"; do
defaults write pbs NSServicesStatus -dict-add "${service}" "
<dict>
<key>enabled_context_menu</key><false/>
<key>enabled_services_menu</key><false/>
<key>key_equivalent</key><string>""</string>
</dict>
"
done
I used the following command just for testing, and didn’t get a valid result:
/usr/libexec/PlistBuddy -c "Print :NSServicesStatus:${service}" ~/Library/Preferences/pbs.plist
It seems like only is service is parsed correctly, and I don’t know which one:
Dict {
key_equivalent =
enabled_context_menu = false
enabled_services_menu = false
}
How do I solve this?
Thank you!
I don't actually condone parsing or generating structured data with structure-unaware tools, but ignoring that --
while IFS= read -r service; do
service=${service%'"'}; service=${service#'"'}
defaults write pbs NSServicesStatus -dict-add "${service}" '
<dict>
<key>enabled_context_menu</key><false/>
<key>enabled_services_menu</key><false/>
<key>key_equivalent</key><string>""</string>
</dict>
'
done < <(egrep -o '"[^"]+"' <<<"$a")