Pick of the Week - Nov 10 [Show all picks]
Path Finder 5 - A feature-laden Finder replacement
Submit Hint Search The Forums LinksStatsPollsFAQHeadlinesRSS
12,000 hints and counting!

Sort Safari bookmarks using XSLT Web Browsers
I really like using Safari, but there is no way to sort the bookmarks. But I know that XSLT can sort XML files, and that Safari uses XML to store its bookmarks. So it should be possible. After learning some XSLT and AppleScript, this is what I came up with.

First, you will need to create an XSLT file and download an XSLT processor. Open TextEdit, and cut and paste this XSLT code and save. I named mine SortSafariBookmarks.xslt.

Next you will have to download Marc Liyanage's TestXSLT and install it -- or you can use a different XSLT processor if you wish.

The Safari Bookmarks.plist file is an XML file (in 10.3) or can be converted into an XML file (in 10.4). If you're running 10.4, use this hint, Convert new plist files between XML and binary, to convert the Safari bookmarks file into XML. Open Terminal and type this command (of course, replace username with your short username):
plutil -convert xml1 /Users/username/Library/Safari/Bookmarks.plist
Once you have the Bookmarks.plist file converted, you can either use TestXSLT to process the Bookmarks.plist file with the XSLT file, or you can use this AppleScript to process it. I am not an AppleScript expert, but the demo AppleScript that was provided with TestXSLT was very helpful.

Paste the script into Script Editor and save it. Then run the AppleScript. Your Bookmarks.plist file is now sorted. You just have to convert it back to a binary plist file if you're running 10.4. To do that, you just need to run this command in the Terminal (again, change username to your short user name):
plutil -convert binary1 /Users/username/Library/Safari/Bookmarks.plist
[robg adds: I haven't tested this one...]
    •    
  • Currently 0.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (0 votes cast)
 
[14,820 views]  

Sort Safari bookmarks using XSLT | 22 comments | Create New Account
Click here to return to the 'Sort Safari bookmarks using XSLT' hint
The following comments are owned by whomever posted them. This site is not responsible for what they say.
Sort Safari bookmarks using XSLT
Authored by: jchapman on Thu, Sep 8 2005 at 10:30AM PDT
I haven't tested this either, but whoever does can change the path in the provided putil comands to make them generic:

plutil -convert xml1 ~/Library/Safari/Bookmarks.plist

[ Reply to This | # ]
Sort Safari bookmarks using XSLT
Authored by: genericuser on Fri, Sep 9 2005 at 12:32PM PDT
This tip is thrilling, if complicated. I've been looking for a free way to do this since SafariSorter became Bookdog & $15. I noticed the hint sorts bookmarks case sensitively. So my Apple links for the iPod, iMac, etc. appear at the end of their folders. Is there a simple way to turn off case sensitivity in the XSLT file?

[ Reply to This | # ]
Sort Safari bookmarks using XSLT
Authored by: pxlated on Thu, Sep 8 2005 at 10:33AM PDT
Wow. Way too much hassle.

[ Reply to This | # ]
Seriously
Authored by: lullabud on Thu, Sep 8 2005 at 2:47PM PDT
The other day I wanted to sort my bookmarks in Safari and was pretty surprised that there was no sort function in the "Show all bookmarks" view. =/

[ Reply to This | # ]
Sort Safari bookmarks using XSLT
Authored by: Severian on Thu, Sep 8 2005 at 10:40AM PDT
This sounds like a useful hint, though I haven't had a chance to check it out yet. However here's a hint for your hint. You can use the shell's filename globbing to specify the user directory like so: ~/Library/Safari/Bookmarks.plist
The shell turns the twiddle (~) into the current user's home directory. This has been a standard part of unix since before Apple was a company.

[ Reply to This | # ]
Sort Safari bookmarks using XSLT
Authored by: fabrizio on Thu, Sep 8 2005 at 10:43AM PDT

There's a couple of utilities that do the same job: Bookdog, that is compatible with 10.2 to 10.4 but it costs 14.95$, and SafariSorter, that is free but is not compatible with Tiger.

About the hint, the two "plutil" commands can be inserted directly into the AppleScript, using:

do shell script "plutil -convert xml1 /Users/username/Library/Safari/Bookmarks.plist"

at the top of the script, and

do shell script "plutil -convert binary1 /Users/username/Library/Safari/Bookmarks.plist"

at the bottom.



[ Reply to This | # ]
Sort Safari bookmarks using XSLT
Authored by: masjones on Thu, Sep 8 2005 at 11:01AM PDT
What am I missing? I have all of my bookmarks sorted and organized into folders...

I even have the bookmarks organized inside the folders.

Mj

[ Reply to This | # ]
Sort Safari bookmarks using XSLT
Authored by: sjk on Sun, Sep 11 2005 at 1:37PM PDT
Because you've manually sorted them?


[ Reply to This | # ]
Translate xbel.xml in Safari bookmarks?
Authored by: MiStch on Thu, Sep 8 2005 at 11:41AM PDT
This hint may be a helpful basis to create a script that allows translating bookmarks in xbel.xml format (e.g. from firefox) into the "proprietary" safari xml format and vice versa.
Has anyone done already something like this or knows a solution? Would be great!

[ Reply to This | # ]
Translate xbel.xml in Safari bookmarks?
Authored by: sluzynsk on Thu, Sep 8 2005 at 6:15PM PDT
Bookit $12, worth it (to me anyway) not to hassle with it. It syncs bookmarks between all the major Mac browsers - Safari, Opera, Firefox, omniweb for sure, probably more that I don't have installed. (No affiliation other than a satisfied user)

[ Reply to This | # ]
xbel.xml in Safari bookmarks
Authored by: MiStch on Fri, Sep 9 2005 at 7:29AM PDT
Bookit is nice, but does not solve my issue.
I want to synchronize my firefox bookmarks on a wintel computer with the safari bookmarks on my mac.
As firefox is able to synchronize with xbel files I need a solution in order to import and export safari bookmarks into xbel.


[ Reply to This | # ]
Sort Safari bookmarks using XSLT
Authored by: mayo2ca on Thu, Sep 8 2005 at 2:51PM PDT
You can use xsltproc from the command line for xslt processor.

[ Reply to This | # ]
Little Snitch
Authored by: jecwobble on Thu, Sep 8 2005 at 7:44PM PDT
Any idea why xsltproc would be calling eg-www.apple.com on port 80?

[ Reply to This | # ]
Little Snitch
Authored by: miggins on Thu, Sep 8 2005 at 10:38PM PDT
There may be a DTD stored at apple.com referenced in the file you're processing.

[ Reply to This | # ]
Sort Safari bookmarks using XSLT
Authored by: jecwobble on Thu, Sep 8 2005 at 7:35PM PDT

Great hint! Thanks. Using mayo2ca's suggestion, I created a shell script that will do whole process for you. It's not fully tested nor foolproof, but it seems to work for me. A few notes and thoughts:

  • The script assumes the XSLT file is saved in ~/Library/Safari/. Perhaps not the best place, but that's where I'm trying it out
  • The script takes one argument as an alternative XSLT file
  • There is an option to make a backup copy of your Bookmarks.plist file
  • This hint's XSLT file sorts folders first, then files, which I like, but it sorts uppercase before lowercase, which I don't
  • I know nothing about XSLT or XML
  • Kick this off with cron/launchd or some keystroke launcher if you like

#! /bin/sh

####### Define constants and Variables #######

ARGS=                  # Number of parameters required besides
                       # options. Set equal to nothing for no requirement
ME=`basename $0`       # Capture this script's name
E_NOARGS=65            # Exit code for wrong number of arguments
E_NOLENGTH=66          # Exit code for zero length arguments
XSLT="$HOME/Library/Safari/SortSafariBookmarks.xslt"
BKMRK="$HOME/Library/Safari/Bookmarks.plist"


# Option defaults
# Mirror any changes here as needed in usage() and getopts
OPT_b=                 # -b option default

####### UI Information #######

usage () {
   echo
   echo "  Usage: $ME [options] [XSLT file]"
   echo "  Sorts Safari's binary Bookmarks.plist file"
   echo "  Options:"
   # Change the following as needed
   # and mirror in the following section
   echo "        -b         To make a backup file of Safari Bookmarks.plist"
   echo "        -h         For this help list"
   echo
}

####### Parse command line #######

# Get options from $@ (parameters/arguments)
# Change "ahf:v" as needed
while getopts bhv option
do
   case $option in
   b)   OPT_b=1;;
   h)   usage
        exit 0;;
   v)   version
        exit 0;;
   *)   exit 1;;
   esac
done

# This strips off options leaving remaining parameters
shift $(($OPTIND - 1))

# Check for required number of parameters if $ARGS is set
if [ $# -ne "${ARGS:-$#}" ]; then
   echo
   echo "$ME error: Requires $ARGS argument(s)" >&2
   usage >&2
   exit $E_NOARGS
fi

# Check for zero length arguments
# Uncomment to activate
for arg in "$@"
do
   if [ -z "$arg" ]; then
      echo
      echo "$ME error: No empty string arguments allowed" >&2
      usage >&2
      exit $E_NOLENGTH
   fi
done


##############################################################
#                     Main script area                       #
#      If we got this far, all command line parameters       #
#                        must be good                        #
##############################################################

# Convenient exit function to clean up after yourself
# Call this function instead of exit X
allDone () {
   # Insert cleanup code here

   exit $1
}

if [ ! -z "$OPT_b" ]; then
   echo "Backing up Bookmarks.plist to Bookmarks.plist.bak"
   cp $BKMRK $HOME/Library/Safari/Bookmarks.plist.bak
fi

for arg in "$@"
do
   echo "Using $@ as XSLT file"
   XSLT="$@"
done

plutil -convert xml1 $BKMRK
xsltproc -o $BKMRK $XSLT $BKMRK
plutil -convert binary1 $BKMRK

######## Exit with a successful code ########
allDone 0

P.S. - Obviously you are free to use or change this script to your liking and I can't be help accountable for any damage it may cause. :-)

[ Reply to This | # ]

Sort Safari bookmarks using XSLT
Authored by: boredzo on Fri, Sep 9 2005 at 1:19AM PDT

Tiger comes with an XSLT processor, xsltproc.



[ Reply to This | # ]
Sort Safari bookmarks using XSLT
Authored by: scottmckaygibson on Tue, Feb 14 2006 at 3:45AM PST
I "created" a little AppleScript Application based on all of the above to do the sorting without too much thought. It can just sit nicely at home in your Applications folder right next to Safari if you want. I've made it available for download here for all who are interested.

[ Reply to This | # ]
Sort Safari bookmarks using XSLT
Authored by: genericuser on Thu, Mar 16 2006 at 7:02AM PST
very nice, works good, I noticed though in my bookmarks most are capitalized but the ones that were lowercase got tossed at the end, does this script only read capitals ?

ie Afolder
Bfolder
afolder

[ Reply to This | # ]
Sort Safari bookmarks using XSLT
Authored by: scottmckaygibson on Tue, Mar 28 2006 at 3:18PM PST
Known Bug.

[ Reply to This | # ]
Sort Safari bookmarks using XSLT
Authored by: thewick on Sun, Apr 23 2006 at 11:11PM PDT
dude, i just wanted to say thanx personally....ive been trying 2 find a way 2 sort my safari bookmarx amd happen to com across your post.

now i dont have 2 buy bookdog....lol

? 4 u....your program will sort everytime i boot up automatically, right? i dont have to double click on....or am i wrong?

much thanx......
thewick

[ Reply to This | # ]
Sort Safari bookmarks using XSLT
Authored by: scottmckaygibson on Sat, Jun 24 2006 at 11:49AM PDT
if you want it to run whenever you boot up, add it to your user's Login Items in System Prefs, otherwise it sorts everytime you launch the program.

[ Reply to This | # ]
Sort Safari bookmarks using XSLT
Authored by: swbuehler on Sat, May 6 2006 at 12:19PM PDT
Great idea; I used the plist and XSLT in Oxygen XML Editor which did everything without need for additional scripts.

[ Reply to This | # ]