Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,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 1.67 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[20,295 views]  

Sort Safari bookmarks using XSLT | 24 comments | Create New Account
Click here to return to the 'Sort Safari bookmarks using XSLT' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Sort Safari bookmarks using XSLT
Authored by: jchapman on Sep 08, '05 10:30:49AM

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 Sep 09, '05 12:32:32PM

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 Sep 08, '05 10:33:21AM

Wow. Way too much hassle.



[ Reply to This | # ]
Seriously
Authored by: lullabud on Sep 08, '05 02:47:50PM

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 Sep 08, '05 10:40:41AM
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 Sep 08, '05 10:43:58AM

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 Sep 08, '05 11:01:28AM

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 Sep 11, '05 01:37:46PM

Because you've manually sorted them?



[ Reply to This | # ]
Translate xbel.xml in Safari bookmarks?
Authored by: MiStch on Sep 08, '05 11:41:18AM

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 Sep 08, '05 06:15:59PM
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 Sep 09, '05 07:29:02AM

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 Sep 08, '05 02:51:00PM

You can use xsltproc from the command line for xslt processor.



[ Reply to This | # ]
Little Snitch
Authored by: jecwobble on Sep 08, '05 07:44:03PM

Any idea why xsltproc would be calling eg-www.apple.com on port 80?



[ Reply to This | # ]
Little Snitch
Authored by: miggins on Sep 08, '05 10:38:40PM

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 Sep 08, '05 07:35:56PM

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 Sep 09, '05 01:19:29AM

Tiger comes with an XSLT processor, xsltproc.



[ Reply to This | # ]
Sort Safari bookmarks using XSLT
Authored by: scottmckaygibson on Feb 14, '06 03:45:24AM
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 Mar 16, '06 07:02:26AM

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 Mar 28, '06 03:18:28PM

Known Bug.



[ Reply to This | # ]
Sort Safari bookmarks using XSLT
Authored by: thewick on Apr 23, '06 11:11:42PM

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 Jun 24, '06 11:49:02AM

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 May 06, '06 12:19:08PM

Great idea; I used the plist and XSLT in Oxygen XML Editor which did everything without need for additional scripts.



[ Reply to This | # ]
Sort Safari bookmarks using XSLT
Authored by: gsgm on Nov 28, '11 06:23:56AM

I've been using this XSLT method for a few years now....worked great until iCloud/Safari 5.1 came out.

Seems that the file structure changed a bit and now if I use this, I loose all the bookmarks that were stored in the Bookmarks menu or Bookmarks Bar (all the rest seem to work)

Anyway, just touching this thread to see if the someone might be able to post an updated XSLT file.



[ Reply to This | # ]
Sort Safari bookmarks using XSLT
Authored by: gsgm on May 28, '12 07:13:27AM

Found an answer to my question of a couple months ago.

http://hintsforums.macworld.com/showthread.php?t=44797 has a perl script that does a similar function and works with Safari v5+



[ Reply to This | # ]