Archive

Archive for January 10, 2013

Updating server bookmarks in com.apple.sidebarlists.plist

January 10, 2013 1 comment

As part of server and storage changes, you may find that you’ve got obsolete bookmarks stored in Connect to Server‘s Favorite Servers: list. This is usually an inconvenience more than anything else, but I wanted to see if I could script a fix in the aftermath of a couple of shares moving from one server to another at work.

In my case, I wanted to update the Connect to Server server bookmarks for our local admin account. These bookmarks are stored in /Users/username/Library/Preferences/com.apple.sidebarlists.plist. I didn’t want to replace the existing com.apple.sidebarlists.plist file and I didn’t want to disturb any of the other settings in the file. I just wanted to change the values of a couple of keys in the plist without worrying about the order in which they were stored.

Thanks to the sed command, there is a way to do this. After some trial and error with the syntax, I found that this command did what I wanted:


/usr/bin/sed -i "" -e 's/smb:\/\/oldservername\/oldsharename/smb:\/\/newservername\/newsharename/g' /Users/username/Library/Preferences/com.apple.sidebarlists.plist

In order to be able to run sed on the file, I needed to convert it to XML first. Also, since the search and replace work is being done by the root account, I needed to fix the permissions on the file once sed had finished its work. To this, I’ve written the following script:

Update – January 16, 2013: I made some updates to the script to include OS and error checking.


#!/bin/sh

# Determine OS version
osvers=$(sw_vers -productVersion | awk -F. '{print $2}')

# Get current date
FILE_DATE=`date +%Y%m%d`


# If the Mac is running 10.5.8 or lower, the script should exit

if [[ ${osvers} -lt 6 ]]; then
   exit 0
fi

# If the Mac is running 10.6.0 or higher, the script should should run

if [[ ${osvers} -ge 6 ]]; then
   
   # Check for the /Users/username/Library/Preferences/com.apple.sidebarlists.plist file

   if [ -f /Users/username/Library/Preferences/com.apple.sidebarlists.plist ]; then
      
      # Back up the existing file

      /bin/cp /Users/username/Library/Preferences/com.apple.sidebarlists.plist /Users/username/Library/Preferences/com.apple.sidebarlists-$FILE_DATE.plist

      # Fix permissions on backup file

      /usr/sbin/chown username /Users/username/Library/Preferences/com.apple.sidebarlists-$FILE_DATE.plist
      
      # Convert plist to XML

      /usr/bin/plutil -convert xml1 /Users/username/Library/Preferences/com.apple.sidebarlists.plist

      # Search and replace in /Users/username/Library/Preferences/com.apple.sidebarlists.plist
      /usr/bin/sed -i "" -e 's/smb:\/\/oldservername\/oldsharename/smb:\/\/newservername\/newsharename/g' /Users/username/Library/Preferences/com.apple.sidebarlists.plist

      # Fix permissions on file
      
      /usr/sbin/chown username /Users/username/Library/Preferences/com.apple.sidebarlists.plist

   fi
fi

exit 0

As written, the script does a find and replace on /Users/username/Library/Preferences/com.apple.sidebarlists.plist as follows:

Find: smb://oldserver/oldsharename

Screen Shot 2013-01-10 at 10.31.58 AM

Replace with: smb://newserver/newsharename

Screen Shot 2013-01-10 at 10.32.14 AM

I’ve tested on Mac OS X 10.6.8, 10.7.5 and 10.8.2 and the scripted search and replace works on all three OSs. You will need to log out and log back in to see the changes.

For those interested, the script is available here on my GitHub repo:
https://github.com/rtrouton/rtrouton_scripts/tree/master/rtrouton_scripts/updating_connect_to_server_favorites

Follow

Get every new post delivered to your Inbox.

Join 125 other followers

%d bloggers like this: