Once used on the iPhone, turning off MobileMe sync for bookmarks will result in deletion of all the bookmarks on the iPhone. Furthermore, there is no way to force the iPhone bookmarks to over-write things on the Mac if the sync ever goes awry.

If, like me, you ever end-up with the "good" bookmarks on the iPhone and "bad" on the Mac, there is just no way to sync the good over and replace the bad. However, one can do some tricks with the iPhone back-up preferences file (plist) and Mac Safari export/import bookmarks to achieve a transfer from iPhone to Mac. Once complete, two-way sync will be restored via iTunes. Unlike the MobileMe sync, you can turn off the iTunes bookmark sync to Safari without losing bookmarks on you iPhone.

I'm not responsible for lost data. Back-up everything before you start this process. Do NOT sync the iPhone until the process is complete. Others may have some shortcuts or improvements to this process [eg, convert Mobile Safari plist to Mac Safari plist instead, or use XSLT or shell script improvements, etc.]. I welcome those suggestions and will re-post them. Leave a comment or send email to me at mobilemind (at) pobox.com.

Here's how I managed to save my iPhone bookmarks, recover them to Mac, and switch off MobileMe bookmark sync without losing data. Now I just use iTunes for Bookmark sync.

1. Download iPhone / iPod Touch Backup Extractor by clicking on the big honking icon on that page. (I sent him $10 USD because it worked so well for me. Send him a few Euro if it works for you too.)

2. Launch the Extractor, open a backup and extract “iPhone OS Files”.

If your current back-up is questionable or to deliberately use an older version of iPhone bookmarks do the following. First make a copy of the current back-up, and put it in another location. Then use Time Machine or your back-up utility to find and restore a "good" previous version of the backup, from:

  ~/Library/Application Support/MobileSync/Backup

Remember that “~” is shorthand for the user home folder (Home icon in Finder, or Shift-Command-H).

3. Save the “iPhone OS Files” extract to a new folder, and find the Bookmarks preferences file. It should be in:
  newfolder/iPhone OS Files/Library/Safari/Bookmarks.plist

4. Convert the binary preferences file to XML. Open a terminal window and go to the folder with Bookmarks.plist. Then issue this command:

  plutil -convert xml1 Bookmarks.plist

Leave the terminal window open, you'll need it in a minute.

5. Open Mac Safari and do File > Export Bookmarks… to save a backup. We'll use this if anything goes wrong, and it's a handy reference example of the bookmark file format. I saved mine as “Safari Bookmarks.html” and put an extra copy in the same folder as “Bookmarks.plist”.

6. Get or make some shell magic with grep to reformat the XML plist preferences file into a rough approximation of a Mac Safari bookmarks import/export file. I found a great bit of code from Dave Taylor here.

I took Dave's example and removed some stuff like the sorting (more on that in a minute). Then I added the necessary opening and closing tags for a Bookmark file, with some comments. Here is my shell file for you to download: plist2bookmarks.sh

7. Save the shell script and make it executable. Save the shell file to the same folder as the converted Bookmarks.plist file. Then go to the terminal window and make it executable by you (the owner) and your group.

  chmod 774 plist2bookmarks.sh

8. Execute the script and direct the output to a new file. I called my output file "MobileSafariBookmarks.html" (no spaces to make the shell command easier).

  ./plist2bookmarks.sh >MobileSafariBookmarks.html

9. Clean-up the “MobileSafariBookmarks.html” in your favorite editor to make it match the folder hierarchy of your iPhone Bookmarks. I used Dreamweaver CS4 and TextMate to open my reference file ("Safari Bookmarks.html") and the new file ("MobileSafariBookmarks.html"), bouncing back & forth to make sure I got the tags right.

Have your iPhone handy to check the folder structure. That helped me as I did edits. The comments put in by “plist2bookmarks.sh” should help you understand how and where to create folders (or nest them).

Keep in mind that the iPhone has a few built-in bookmark folders and bookmarks, like the “History” folder and the bookmarks for “Yahoo!”, “Google”, “AT&T MyAccount”, and “iPhone User Guide”.

10. Prepare to import Mobile Safari Bookmarks into Mac Safari. Make sure you have a back-up of your Mac Safari bookmarks. Then open Safari, use Bookmarks > Show All Bookmarks, and delete ONLY your bookmarks. Do NOT delete Address Book or other items. In m case, I selected Bookmarks Bar and deleted all the items there. Then I selected Bookmarks Menu and deleted all those items.

11. Import the Mobile Safari Bookmarks into Mac Safari. Use File > Import Bookmarks… to import “MobileSafariBookmarks.html” as cleaned-up in step 9.

12. Verify Mac Safari against Mobile Safari before you disable MobileMe sync of bookmarks. Remember, that will delete the iPhone bookmarks. Carefully check the list and URL addresses of the bookmarks now in Mac Safari against the list and structure in Mobile Safari on iPhone. Quit Safari on the Mac when you are done. Quit Mobile Safari on the iPhone too.

13. Take a deep breath and delete the bookmarks on Mobile Safari by disabling MobileMe sync of Bookmarks. On iPhone, go to Settings > Mail, Contacts, Calendars. Then, under Accounts select your MobileMe account. Change Bookmarks to OFF. iPhone will warn you about deleting bookmarks. That is OK, because we now have them in Safari and will soon sync with iTunes from Mac Safari back to iPhone.

14. Disable iTunes automatic syncing for devices to allow us to make changes. Do not connect the iPhone yet. Start iTunes. Open Preferences and select Devices. Check the box that says "Disable automatic syncing for iPhones and iPods". Close Preferences.

15. Connect the iPhone and change iTunes to Sync Safari bookmarks. After the iPhone connects, it may sync anyway. That is OK. Select the iPhone in the left panel. Then select the tab labeled Info in the large main panel of iTunes. Scroll down to the shaded bar for Web Browser and check the box next to Sync Safari bookmarks.

16. Apply the change to sync. Click the Sync button in the lower right corner of iTunes and wait.

17. When the sync is done, all the bookmarks moved manually from the backup to Mac Safari, will be restored to the iPhone. From now on, the iPhone and Mac Safari will sync via iTunes. Changes on one will be reflected on the other after each sync. As an extra bonus, the MobileMe "push" should be a little shorter and a little less battery draining, since bookmarks are no longer synced over-the-air.

If you want, you can even turn off iTunes bookmark sync and add/restore your original Mac Safari bookmarks, either to keep things separate or to merge them back to iPhone later.