#!/usr/bin/python # # $Id: export_bookmarks 85 2004-09-29 18:07:32Z jay $ # import sys, os, types, codecs import plistlib def read_bookmarks(): bookmarks_file = os.path.join( os.getenv("HOME"), "Library/Safari/Bookmarks.plist" ) return plistlib.Plist.fromFile(bookmarks_file) def filter_bookmarks(node, wanted, found = 0, depth = 0): node_type = node.get("WebBookmarkType") node_title = node.get("Title") if node_title == "BookmarksBar": node_title ="Bookmarks Bar" indent = "\t" * depth if node_type == "WebBookmarkTypeList": found = found or (node_title in wanted) if found and node_title: print "%s

%s

" % (indent, node_title) print "%s

" % indent for child in node.get("Children", []): filter_bookmarks(child, wanted, found, found*(depth+1)) if found and node_title: print "%s

" % indent elif node_type == "WebBookmarkTypeLeaf": if found: print '%s

%s' % ( indent, node["URLString"], int(float(node["URIDictionary"].get("lastVisitedDate", 0))), node["URIDictionary"]["title"] ) def header(): print """ Bookmarks

Bookmarks

""" def footer(): print """""" def main(args): sys.stdout = codecs.getwriter("utf-8")(sys.__stdout__) header() if args: filter_bookmarks(read_bookmarks(), args) else: filter_bookmarks(read_bookmarks(), [], found=1) footer() if __name__ == "__main__": main(sys.argv[1:])