plists and xml

I’m sure the answer is around here somewhere and if so, a URL to an explanation will shut me up :slight_smile: I’m trying to get more information on how to import XML (in this particular case, plists) and have them appear as something readable (I guess via XSLT or CSS)? I’m not particularly excited about any of the import scripts as I don’t seem to get the tags and the links, so I’m looking for alternatives. The Firefox extension “ScrapBook” helps, but I’m playing around with importing the plist from cocoalicious (which is an amazing app.) A dump of the plist dictionary comes out as (obviously) plain XML which is tough to read. Any suggestions?

Just depends on how you’d want it to look, I suppose. Maybe like transforming

blah blah blah


blah – blah blah

Is that what you’re looking for?

In that case it’s a rather simple matter of doing a find-and-replace applescript. It’d basically be setting AppleScript’s text item delimiters to or whatever and replacing that with a line break, then replacing with a " – ", and so forth. In which case, I think it’d probably be best done by importing all the files into DTP as plain text and then processing them via AppleScript.

If you want them to be transformed some other way, well, it’d be different 8)

What do you want the end result to look like? Let me know and I can try to write a script to do it.

Heck, that would do it for me. I seem to be barely literate in English. I have no chance with XML :slight_smile: I don’t need a fancy look-and-feel. Something like the actual site:

Tag, Tag, Tag, Tag

(Obviously with the actual information in it :slight_smile:)

That would be great!

Okay, I think this might be along the lines of what you need.

Dig this: What you do is import a plist file into DEVONthink Pro, select it (or select a bunch), and run this script. It’ll take the standard plist format and spit out a decently-formatted HTML document.

A couple things to note: look at the theReplacements variable. It’s an array of arrays. Each array ({"","

    "}, for instance) contains a string to search for and a string to replace it with. If you use a plist or other XML file that doesn’t obey the rules of DEVONthink Pro’s plist file (which I used to write this script), it’s simple to take the XML you want removed and replace it with an HTML tag. All you have to do is be sure to replace the starting tags ("") with starting tags ("
      ") and the closing tags ("") with closing tags ("
    "), or else it’ll end up looking like crap.

    This might be impossible to read right now, but if you paste it into Script Editor it should clean right up.

global theReplacements
set theReplacements to {{"<dict>", "<ul>"}, {"</dict>", "</ul>"}, {"<key>", "<b>"}, {"</key>", "</b> -- "}, {"<array>", "<ul>"}, {"</array>", "</ul>"}, {"<string>", ""}, {"</string>", "<br>"}, {"<true/>", "<i>true</i>"}}
tell application "DEVONthink Pro"
	set theSelection to the selection
	if theSelection is equal to {} then error "Please select a file to convert."
	repeat with thisSelection in theSelection
		set theContents to the plain text of thisSelection -- Grab the original information.
		set theName to the name of thisSelection & ".html" -- Add an HTML file extension, because viewing this in HTML will make it a lot easier to format.
		set theFile to create record with {name:theName, type:html, source:theContents} -- We're working from another file so as not to b0rk the original.
		repeat with thisReplacement in theReplacements -- Repeat the following with each find/replace pair of the array.
			set a to (item 1 of thisReplacement) -- Thing to search for.
			set b to (item 2 of thisReplacement) -- Thing to change into.
			set theContents to my FindAndReplace(theContents, a, b) -- Change stuff.
		end repeat
		set the source of theFile to theContents -- Write out the final HTML file.
	end repeat
end tell

on FindAndReplace(theText, a, b)
	set c to text item delimiters -- Stores the old text delimiters.
	set text item delimiters to a -- The original.
	set theText to theText's text items -- Split the text (minus the original).
	set text item delimiters to b -- The replacement.
	tell theText to set theText to item 1 & ({""} & rest) -- Glue it back together, using the replacement as glue.
	set text item delimiters to c -- Restores the old text delimiters.
	return theText -- All done with this set :)
end FindAndReplace

Hmm. No love. It creates the html file, but it’s blank. I tried saving it in a variety of different formats in Script Editor but none of them do anything more than create the html file.

Think there might be something odd/unique about the plist file?

Hmm. Should be pretty airtight. It creates and fills the HTML file before it does any manipulation of the contents…

If the original plist file is XML (not binary) and imported as a plain text file, I’m not sure what could be going wrong.

You rock :slight_smile: I think I imported the binary version of the plist. I did a dump, copy/pasted it into a text file that I imported, then ran the script. Thanks!! :smiley:

Glad to hear it worked 8) You’re not the first person to be frustrated by Apple’s switch to binary plists. I should’ve mentioned that.