
Can I extract all my Safari bookmarks with a shell script?
Alright, this is pretty geeky, but is there a way I can grab all my Mac OS X Safari bookmarks using a shell script so I can manipulate the data? I've peeked into the bookmarks file and yow, it's one hairy data file!
You're right. That's pretty darn geeky, especially when there are some very nice bookmark management tools available like BookmarkTool, URLManager Pro (which looks like the best of the bunch), and Bookit. Further, if you use a little trick to turn on the Safari Debug menu, one of the options on that menu is ... ta da! ... Export Bookmarks. How to enable the Safari Debug Menu: first off, quit Safari. Now, restart Safari because you actually need to copy the following before you quit (that's what happens when you're too eager!): defaults write com.apple.Safari IncludeDebugMenu 1. Now, quit Safari, then paste that command into the Terminal and restart Safari. You'll have a shiny new Debug menu available. Alright, you ask, I deliver! It turns out that the Safari bookmarks are stored in an XML format file (as detailed in the question How do I keep my Safari bookmarks in sync?) which makes it a nightmare to parse and unwrap within the confines of a shell script. The simplest is to just suck out all the URLs themselves, which can be done on the command line: $ grep http: ~/Library/Safari/Bookmarks.plist <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <string>http://www.coloradodaily.com/</string> <string>http://www.coloradodaily.com/</string> <string>http://news.google.com/</string> <string>http://news.google.com/</string> <string>http://www.nytimes.com/</string> <string>http://www.nytimes.com/</string> <string>http://online.wsj.com/home/us</string> etc, etc, etcThis isn't a particularly satisfying solution because of all the XML litter that comes along for the ride and the fact that each URL is duplicated. Instead, a bit of analysis of the XML file itself reveals that it's the values URLstring and title that we're interested in, and that XML has the name, followed on the next line by the value. So here's a little magic: $ bm="$HOME/Library/Safari/Bookmarks.plist" $ grep -A1 -E '(>URLString<|>title<)' $bm | grep -v -E '(>URLString|>title|^--)' <string>CD</string> <string>http://www.coloradodaily.com/</string> <string>Gnews</string> <string>http://news.google.com/</string> <string>NYT</string> <string>http://www.nytimes.com/</string> <string>WSJ</string> <string>http://online.wsj.com/home/us</string>A huge improvement! Now to remove the tags so that it's just the data fields. There are a bunch of ways to do this ranging from cruelly hackish to slick and sophisticated. I'll opt for the former: $ grep -A1 -E '(>URLString<|>title<)' $bm | grep -v -E '(>URLString|>title|^--)' | cut -d\> -f2 | cut -d\< -f1 CD http://www.coloradodaily.com/ Gnews http://news.google.com/ NYT http://www.nytimes.com/ WSJ http://online.wsj.com/home/usOkay. That's 90% of the work. Let's just pour this into a shell script and use a while loop to read the information in the pipe, rewriting it as an attractive html href tag: #!/bin/sh # get Safari Bookmarks bm="$HOME/Library/Safari/Bookmarks.plist" grep -A1 -E '(>URLString<|>title<)' $bm | grep -v -E '(>URLString|>title|^--)' | cut -d\> -f2 | cut -d\< -f1 | while read theTitle do read theUrl echo "<!-- $theTitle --><a href=\"$theUrl\">$theTitle</a><br />" done | sort -f | uniq exit 0You'll notice I've slapped a few refinements into the script, not the least of which is producing output that has the title of the link as a comment for sorting, and then using the sort -f | uniq pipe after the href's have been output to remove any dupes. If you'd rather not have anything sorted, just remove the sort -f ('-f' ignores case) and if you'd rather sort by the URL, just chop out the snippet in the echo statement that adds the title as an HTML comment. Here's the output of running this script on my current bookmark file (well, part of it. Turns out I have a staggering 355 bookmarks). I have wrapped the text so as not to completely drive your Web browser insane trying to figure out how to render this page, but the output's the same: $ get-safari-bookmarks.sh <!-- .Mac --><a href="http://www.mac.com/"> .Mac</a><br /> <!-- 1-800-FLOWERS --><a href="http://www.1800flowers.com/"> 1-800-FLOWERS</a><br /> <!-- 4D --><a href="http://www.4D.com/"> 4D</a><br /> <!-- Aaron Faby's Mac OS X/Mac OS X Server Resources --> <a href="http://www.aaronfaby.com/osx.shtml"> Aaron Faby's Mac OS X/Mac OS X Server Resources</a><br /> <!-- Aatrix --><a href="http://www.aatrix.com/"> Aatrix</a><br /> <!-- Activision --><a href="http://www.activision.com/"> Activision</a><br /> <!-- Adobe Systems --><a href="http://www.adobe.com/"> Adobe Systems</a><br /> <!-- AEC Software --><a href="http://www.aecsoft.com/"> AEC Software</a><br /> <!-- AJA Video Systems --><a href="http://www.aja.com/"> AJA Video Systems</a><br /> <!-- Aladdin Systems --><a href="http://www.aladdinsys.com/"> Aladdin Systems</a><br /> <!-- Amazon --><a href="http://www.amazon.com/"> Amazon</a><br /> etc etcOne last snippet. You can easily figure out how many bookmarks you have with this script too: $ get-safari-bookmarks.sh | wc -l 355Hope that's what you want. Ai yi yi. I'd rather just use the Safari Debug "Export Bookmarks..." feature, personally.
Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Simpy.
Categorized:
Mac OS X Help
, Shell Script Programming
(Article 3818)
Tagged: Previous: How can I tell if my Paypal and eBay Alerts are legit or phishing? Next: Can I crack a password protected Microsoft Word file? Subscribe!
Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader. good and thanx Posted by: vel at August 29, 2006 11:47 PMI have a lot to say, but ...
I do have a comment, now that you mention it!
|
Search
Find just the answers you seek from among our 1700+ free tech support articles by using our Lijit search engine.
Help!
Subscribe to
Ask Dave Taylor!
Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.
Articles and Reviews
Auctions and Online Shopping Blogs and RSS Feeds Building Web site traffic Business and Management Cell Phones and Mobile Phones CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above HTML and CSS Mac OS X Help MySpace, Facebook, Twitter and Social Network Help Pay Per Click (PPC) Search Engine Optimization Shell Script Programming Sony PSP, MP3 Players, Etc. The Writing Business Unix and Linux Help Video Game Tips and Help Windows Help
Recent Entries
Join the List!
Book Links
|