Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Reading Safari's bookmarks.plist



I was doing something like this with Groovy and Cocoa. You need to add /System/Library/Java to your CLASSPATH before running this. Here is a link to a Mac OS X 10.3.9 executable you can use instead of downloading the whole distribution of groovy:
http://homepage.mac.com/spullara/groovy-jsr-02-SNAPSHOT-mac


I suggest you put it in /usr/bin/groovy so you can execute scripts easily (remember to chmod +x it).

#!/usr/bin/groovy
import groovy.xml.*;
import groovy.xml.dom.*;
import java.io.*;
import com.apple.cocoa.foundation.*;

class PropertyListCategory {
        static Object get(NSDictionary dictionary, String key) {
                return dictionary.objectForKey(key);
        }
        static Object getAt(NSArray array, int i) {
		return array.objectAtIndex(i);
        }
        static void each(NSArray array, Closure closure) {
                for (i in 0..array.count()-1) {
                        closure.call(array[i]);
                }
        }
}

filename = "${System.getProperty("user.home")}/Library/Safari/Bookmarks.plist";
data = new NSData(new File(filename));
errorString = new String[1];
format = new int[1];
plist = NSPropertyListSerialization.propertyListFromData(data, NSPropertyListSerialization.PropertyListImmutable, format, errorString);


if (errorString[0]) {
        println "Error: ${errorString[0]}";
        System.exit(1);
}

def getURLs(NSArray array, list) {
        array.each {
                getURLs(it, list);
        }
}

def getURLs(NSDictionary dict, list) {
if (dict.Children != null) getURLs(dict.Children, list);
if (dict.URIDictionary != null) {
list.add([title:dict.URIDictionary.title, url:dict.URLString]);
}
}


def getURLs(NSDictionary dict) {
        use (PropertyListCategory.class) {
                def list = [];
                getURLs(dict, list);
        }
        return list;
}

println getURLs(plist);

On Apr 23, 2005, at 7:32 AM, Bill Tschumy wrote:

I did find a (sort of) solution to my problem. First, I didn't make clear that the Safari Bookmarks.plist is in binary format. That's what made it hard to read from Java.

However I found the "plutil" utility in /usr/bin that will convert plists between binary and XML formats. So now I copy the Bookmarks.plist to /tmp, use Runtime's exec() to run plutil on it, and then I can use the SAX classes to easily extract the URLs from the resulting XML.


On Apr 22, 2005, at 3:27 PM, Bill Tschumy wrote:

Does anyone have some code to share for reading Safari's bookmarks file which is in .plist format? I need to extract all the URLs in there.

-- Bill Tschumy Otherwise -- Austin, TX http://www.otherwise.com

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden

_______________________________________________ Do not post admin requests to the list. They will be ignored. Java-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden
References: 
 >Reading Safari's bookmarks.plist (From: Bill Tschumy <email@hidden>)
 >Re: Reading Safari's bookmarks.plist (From: Bill Tschumy <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.