Add item to Finder toolbar with terminal?

This is as far as I’ve got, and it’s not working for me on El Capitan (though, that could be something to do with the beta…).

It seems that defaults isn’t comprehensive enough (anymore?) for you to achieve what you want, however you can use PlistBuddy, which on 10.11 is currently found under /usr/libexec/PlistBuddy, to add/remove/edit plist files.

Here are the two commands you’ll can use to add the _CFURLString and _CFURLStringType:

/usr/libexec/PlistBuddy -c 'Add "NSToolbar Configuration Browser":"TB Item Plists":8:_CFURLString string "file:///Applications/Chess.app"' ~/Library/Preferences/com.apple.finder.plist

/usr/libexec/PlistBuddy -c 'Add "NSToolbar Configuration Browser":"TB Item Plists":8:_CFURLStringType integer 15' ~/Library/Preferences/com.apple.finder.plist

The “8” found in both of the commands above is the array index of the item.

No _CFURLAliasData is generated (as suggested in several places). I’ve tried creating an empty _CFURLAliasData, but Finder didn’t populate it.

Another issue is that if I add/remove another item using cmd-drag all the data I’ve added with PlistBuddy gets erased…

Hopefully this information will get someone else partway there.


In addition to adding an entry to TB Item Plists you also need to add an entry to TB Item Identifiers.

The following code will add Go2Shell to the Finder toolbar:

#!/usr/bin/env bash

PLIST=~/Library/Preferences/com.apple.finder.plist
ITEMS=`/usr/libexec/PlistBuddy -c "Print 'NSToolbar Configuration Browser:TB Default Item Identifiers'" $PLIST | sed '1d; $d'`
APP=/Applications/Go2Shell.app/Contents/MacOS/Go2ShellHelper.app/
POSITION=8

/usr/libexec/PlistBuddy -c "Delete 'NSToolbar Configuration Browser:TB Item Identifiers'" $PLIST
/usr/libexec/PlistBuddy -c "Delete 'NSToolbar Configuration Browser:TB Item Plists'" $PLIST

/usr/libexec/PlistBuddy -c "Add 'NSToolbar Configuration Browser:TB Item Identifiers' array" $PLIST
i=1
for ITEM in $ITEMS
do
  if [ $i -ne $POSITION ]; then
    /usr/libexec/PlistBuddy -c "Add 'NSToolbar Configuration Browser:TB Item Identifiers:$i' string $ITEM" $PLIST
  fi
  ((i++))
done

/usr/libexec/PlistBuddy -c "Add 'NSToolbar Configuration Browser:TB Item Plists:$POSITION:_CFURLString' string 'file://$APP'" $PLIST
/usr/libexec/PlistBuddy -c "Add 'NSToolbar Configuration Browser:TB Item Plists:$POSITION:_CFURLStringType' integer 15" $PLIST
#/usr/libexec/PlistBuddy -c "Import 'NSToolbar Configuration Browser:TB Item Plists:$POSITION:_CFURLAliasData' alias_data" $PLIST
/usr/libexec/PlistBuddy -c "Add 'NSToolbar Configuration Browser:TB Item Identifiers:$POSITION' string 'com.apple.finder.loc '" $PLIST

killall -HUP Finder
  1. The default toolbar items are read
  2. Existing identifiers and plists are removed
  3. New identifiers are added leaving a space for Go2Shell
  4. New plist and identifier are added for Go2Shell (the space after com.apple.finder.loc is required)

The _CFURLAliasData import is commented out as it's not required for the Finder toolbar, and probably a bad idea too. If the alias data is required (it may be for the Finder sidebar or Dock), the following code will generate an alias_data file for Go2Shell and add it to the plist:

#!/usr/bin/env bash

APP=/Applications/Go2Shell.app/Contents/MacOS/Go2ShellHelper.app/
POSITION=8

python2 -c "from Carbon import File; print File.FSNewAlias(None, File.FSRef('$APP')).data" > alias_data
/usr/libexec/PlistBuddy -c "Import 'NSToolbar Configuration Browser:TB Item Plists:$POSITION:_CFURLAlias_Data' alias_data" ~/Library/Preferences/com.apple.finder.plist
rm alias_data