MacOS – Add item to Finder toolbar with terminal

customizationfindermacosterminal

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…

Best Answer

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.