5

You can cmd-drag an item to the Finder toolbar to have it there permanently, but is there a way you do this in the Terminal?

The closest I've found is this old thread: http://macscripter.net/viewtopic.php?id=21344

Which suggests something like this:

defaults write com.apple.finder FXToolbarItems -array-add '<dict><key>file-data</key><dict><key>_CFURLString</key><string>"/Applications/Chess.app"</string><key>_CFURLStringType</key><integer>0</integer></dict></dict>'
killall -HUP Finder

.. or perhaps ...

defaults write com.apple.finder FXToolbarItems -array-add '<dict><key>file-data</key><dict><key>_CFURLString</key><string>"/Applications/Chess.app"</string><key>_CFURLStringType</key><integer>0</integer></dict><key>item-id</key><string>loc%20</string></dict>'
killall -HUP Finder

Neither seem to work. Is there something which works with Yosemite and above?


I notice that if I manually add the Chess app to the Finder toolbar, and then output the finder prefs like so:

defaults read com.apple.finder >> finderprefs

I can see it puts it in like so:

"TB Item Plists" =         {
        7 =             {
            "_CFURLAliasData" = <(lots of hex code)>;
            "_CFURLString" = "file:///Applications/Chess.app";
            "_CFURLStringType" = 15;
        };
    };

So I guess I need to find a way to do that with defaults write


More research suggests adding items programmatically is not advisable: http://prod.lists.apple.com/archives/cocoa-dev/2015/May/msg00212.html

If you know different, I'd love to know...

  • I recommend using the excellent python script dockutil for programmatically handling dock items. dockutil --add /Applications/TextEdit.app – Horselover Fat Jul 30 '15 at 15:04
4
+50

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.

| improve this answer | |
1

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
| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .