Question

I wrote a script to add a users corporate network shares to their "connect to servers" list in Mac OS X (Finder --> Go --> Connect to Server...). This script writes directly to a backup of com.apple.sidebarlists.plist then replaces it after validation using mv. I'm having trouble getting Finder to recognize that changes have been made to this plist. Rebooting the system displays the new servers in the list just fine. However logout/login and relaunching (killing) Finder does not. I've also tried killall SystemUIServer and killall cfprefsd (http://blog.designed79.co.uk/?p=1761) with no luck.

Has anyone run into this and is there a solution to get the list to update without a reboot?

Thank you!

Here is the part of my script that does the writes/moves/etc for reference.

# ...
    cp "$enduserhomefolder/Library/Preferences/com.apple.sidebarlists.plist" "$enduserhomefolder/Library/Preferences/com.apple.sidebarlists.mod.plist"
    if [[ "$?"  != 0 ]]; then
        echo "$(date):ERROR: The users com.apple.sidebarlists.plist could not be backed up. Aborting...."
        exitfunction
    else
        echo "$(date): com.apple.sidebarlists.plist was backed up."
    fi  

    defaults delete "$enduserhomefolder/Library/Preferences/com.apple.sidebarlists.mod.plist" favoriteservers
    if [[ "$?"  != 0 ]]; then
        echo "$(date):ERROR: Could not remove favoriteservers from com.apple.sidebarlists.mod.plist. Aborting...."
        exitfunction
    else
        echo "$(date): Favoriteservers was removed from com.apple.sidebarlists.plist."
    fi  

    defaults write "$enduserhomefolder/Library/Preferences/com.apple.sidebarlists.mod.plist" favoriteservers -dict Controller CustomListItems CustomListItems REPLACEME
    if [[ "$?"  != 0 ]]; then
        echo "$(date):ERROR: Could not add favoriteservers to com.apple.sidebarlists.mod.plist. Aborting...."
        exitfunction
    else
        echo "$(date): Favoriteservers was re-added to com.apple.sidebarlists.plist with placeholder."
    fi  

    plutil -convert xml1 "$enduserhomefolder/Library/Preferences/com.apple.sidebarlists.mod.plist"
    if [[ "$?"  != 0 ]]; then
        echo "$(date):ERROR: Could not convert com.apple.sidebarlists.mod.plist to XML1 format. Aborting...."
        exitfunction
    else
        echo "$(date): com.apple.sidebarlists.mod.plist was successfully converted to XML1."
    fi  

    sed -i "" "/ *<string>REPLACEME<\/string>/r $tempfile5" "$enduserhomefolder/Library/Preferences/com.apple.sidebarlists.mod.plist"

    if [[ "$?"  != 0 ]]; then
        echo "$(date):ERROR: Could not inject new server list. Aborting...."
        exitfunction
    else
        echo "$(date): New server list was injected into com.apple.sidebarlists.mod.plist."
    fi  

    sed -i "" 's/ *<string>REPLACEME<\/string>//g' "$enduserhomefolder/Library/Preferences/com.apple.sidebarlists.mod.plist"
    if [[ "$?"  != 0 ]]; then
        echo "$(date):ERROR: Could not remove placeholder tag in com.apple.sidebarlists.mod.plist. Aborting...."
        exitfunction
    else
        echo "$(date): Placeholder tag was removed from com.apple.sidebarlists.mod.plist."
    fi  

    plutil -convert binary1 "$enduserhomefolder/Library/Preferences/com.apple.sidebarlists.mod.plist"
    if [[ "$?"  != 0 ]]; then
        echo "$(date):ERROR: Could not convert com.apple.sidebarlists.mod.plist into binary format. Aborting...."
        exitfunction
    else
        echo "$(date):com.apple.sidebarlists.mod.plist was successfully converted into binary1."
    fi

    plutil -lint "$enduserhomefolder/Library/Preferences/com.apple.sidebarlists.mod.plist"
    if [[ "$?"  != 0 ]]; then
        echo "$(date):ERROR: Could not validate com.apple.sidebarlists.mod.plist. Aborting...."
        exitfunction
    else
        echo "$(date):com.apple.sidebarlists.mod.plist was successfully verified as a valid XML file."
    fi  

    mv "$enduserhomefolder/Library/Preferences/com.apple.sidebarlists.plist" "$enduserhomefolder/Library/Preferences/com.apple.sidebarlists.plist.old"
    if [[ "$?"  != 0 ]]; then
        echo "$(date):ERROR:  com.apple.sidebarlists.plist could not be moved. Aborting...."
        exitfunction
    else
        echo "$(date):com.apple.sidebarlists.mod.plist was successfully renamed to com.apple.sidebarlists.plist.old."
    fi  

    mv "$enduserhomefolder/Library/Preferences/com.apple.sidebarlists.mod.plist" "$enduserhomefolder/Library/Preferences/com.apple.sidebarlists.plist"
    if [[ "$?"  != 0 ]]; then
        echo "$(date):ERROR:  apple.sidebar.mod.plist could not be moved. Aborting...."
        mv "$enduserhomefolder/Library/Preferences/com.apple.sidebarlists.plist.old"  "$enduserhomefolder/Library/Preferences/com.apple.sidebarlists.plist"
        echo "$(date):ERROR:  com.apple.sidebarlists.plist plist restored from backup"
        exitfunction
    else
        echo "$(date):com.apple.sidebarlists.mod.plist was successfully renamed to com.apple.sidebarlists.plist.old."
    fi  

# ....
Was it helpful?

Solution

You evidently already know about the defaults command. So why are you using it as a way to manipulate preference plist files? You shouldn't do that. Use the defaults command the way it's meant to be used to just modify the preferences in the cached database and let cfprefsd write the updated cache to the file at its leisure.

defaults write com.apple.sidebarlists favoriteservers -dict-add CustomListItems '( { Name = "whatever"; URL = "smb://server.example.com"; } )'

That said, the Finder still won't pick up the changes immediately, but it will once it's restarted. It might be sufficient to do killall -HUP Finder or something like that. (That will probably just kill the Finder at which point it will be automatically restarted.) You could also do something like:

osascript -e 'tell app "Finder" to quit'
sleep 1
osascript -e 'tell app "Finder" to launch'

Mind you, only do this with the permission of the user. Killing the Finder when the user isn't expecting it would be very rude and has the potential for data loss.

OTHER TIPS

You could try deleting the cache after changes are affected and before relaunching Finder :

~/Library/Caches/com.apple.finder
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top