Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


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: 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 | # ]