Add Items To Finder Window Toolbar Programmatically

mcrispin
Contributor II

Hello Apple Nerds,

Every now and then I get really weird requests from techs who are trying to automate processes. In this case, someone wanted to automatically add the "Create Folder" and "Trash" icons to their Finder window toolbar. I'm not terribly sure why they could'nt teach their end users on how to do this, but perhaps it was a lab environment (it wasn't). Anyway, here is how to do that. The tricky part was lexical, because in this case, the DICT and ARRAY had spaces in them.

#!/bin/sh

over500=$( /usr/bin/dscl . list /Users UniqueID | awk '$2 > 500 { print $1 }' )

for u in $over500 ;
do
    /usr/libexec/PlistBuddy -c "delete :'NSToolbar Configuration Browser':'TB Item Identifiers'" /Users/"$u"/Library/Preferences/com.apple.finder.plist
done

killall Finder

for u2 in $over500 ;
do
    /usr/libexec/PlistBuddy -c "add :'NSToolbar Configuration Browser':'TB Item Identifiers' array" /Users/"$u2"/Library/Preferences/com.apple.finder.plist
    /usr/libexec/PlistBuddy -c "add :'NSToolbar Configuration Browser':'TB Item Identifiers':0 string 'com.apple.finder.BACK'" /Users/"$u2"/Library/Preferences/com.apple.finder.plist
    /usr/libexec/PlistBuddy -c "add :'NSToolbar Configuration Browser':'TB Item Identifiers':2 string 'NSToolbarFlexibleSpaceItem'" /Users/"$u2"/Library/Preferences/com.apple.finder.plist
    /usr/libexec/PlistBuddy -c "add :'NSToolbar Configuration Browser':'TB Item Identifiers':3 string 'com.apple.finder.NFLD'" /Users/"$u2"/Library/Preferences/com.apple.finder.plist
    /usr/libexec/PlistBuddy -c "add :'NSToolbar Configuration Browser':'TB Item Identifiers':4 string 'com.apple.finder.TRSH'" /Users/"$u2"/Library/Preferences/com.apple.finder.plist
    /usr/libexec/PlistBuddy -c "add :'NSToolbar Configuration Browser':'TB Item Identifiers':5 string 'com.apple.finder.SWCH'" /Users/"$u2"/Library/Preferences/com.apple.finder.plist
    /usr/libexec/PlistBuddy -c "add :'NSToolbar Configuration Browser':'TB Item Identifiers':6 string 'com.apple.finder.ARNG'" /Users/"$u2"/Library/Preferences/com.apple.finder.plist
    /usr/libexec/PlistBuddy -c "add :'NSToolbar Configuration Browser':'TB Item Identifiers':7 string 'com.apple.finder.ACTN'" /Users/"$u2"/Library/Preferences/com.apple.finder.plist
    /usr/libexec/PlistBuddy -c "add :'NSToolbar Configuration Browser':'TB Item Identifiers':8 string 'com.apple.finder.SHAR'" /Users/"$u2"/Library/Preferences/com.apple.finder.plist
    /usr/libexec/PlistBuddy -c "add :'NSToolbar Configuration Browser':'TB Item Identifiers':9 string 'com.apple.finder.LABL'" /Users/"$u2"/Library/Preferences/com.apple.finder.plist
    /usr/libexec/PlistBuddy -c "add :'NSToolbar Configuration Browser':'TB Item Identifiers':10 string 'NSToolbarFlexibleSpaceItem'" /Users/"$u2"/Library/Preferences/com.apple.finder.plist
    /usr/libexec/PlistBuddy -c "add :'NSToolbar Configuration Browser':'TB Item Identifiers':11 string 'NSToolbarFlexibleSpaceItem'" /Users/"$u2"/Library/Preferences/com.apple.finder.plist
    /usr/libexec/PlistBuddy -c "add :'NSToolbar Configuration Browser':'TB Item Identifiers':12 string 'com.apple.finder.SRCH'" /Users/"$u2"/Library/Preferences/com.apple.finder.plist
done

killall Finder

exit 0
0 REPLIES 0