Mac Text Manipulation

Waragainstsleep

macrumors 6502
Original poster
Oct 15, 2003
295
54
UK
This isn't really a programming issue necessarily, but the folks here are perhaps more likely to have an answer.

I have a plist file which consists of entires like this one:

<dict>
<key></key>
<string>http://www,url.com</string>
<key>title</key>
<string>9ISyAl7.jpg 720×960 pixels</string>
<key>lastVisitedDate</key>
<string>433770848.7</string>
<key>D</key>
<array>
<integer>1</integer>
</array>
<key>visitCount</key>
<integer>1</integer>
</dict>



There are over 5000 of these in the plist.

I'd like to get them into a spreadsheet with the URL in one column, the title in the next column, the last visited date in the next column and the visit count in the last column.

Does anyone know a quick way to accomplish this please?
 

chown33

Moderator
Staff member
Aug 9, 2009
8,785
5,173
vertical
Show the complete structure of the plist file. For example, is the dict you showed an array member or a member of another dict?

The command-line tool PlistBuddy might be usable, but you need to show the exact plist structure. Best of all would be a complete XML plist file with only a few items, say 3, that you want extracted. In short, provide small but well-formed sample data.

PlistBuddy man page:
https://developer.apple.com/library...win/Reference/ManPages/man8/PlistBuddy.8.html
 

Freez

macrumors newbie
Feb 9, 2011
15
0
use find/replace

Use find/replace in TextWrangler.
1. Copy text you want removed for the find.
2. Use the tab for the replace between columns and return for the replace after last column.

note
tab is \t
return is \n

3. Select all and copy.
4. Paste into excel document.
 
Last edited:

Waragainstsleep

macrumors 6502
Original poster
Oct 15, 2003
295
54
UK
Show the complete structure of the plist file. For example, is the dict you showed an array member or a member of another dict?

The command-line tool PlistBuddy might be usable, but you need to show the exact plist structure. Best of all would be a complete XML plist file with only a few items, say 3, that you want extracted. In short, provide small but well-formed sample data.

PlistBuddy man page:
https://developer.apple.com/library...win/Reference/ManPages/man8/PlistBuddy.8.html
Its inside an array, inside a key, inside another dict. Its from a Safari browser history plist. ~/Library/Safari/History.plist.
Mine has three keys, the middle one has the info I need in it. I'm sure your will look very similar.

I'll look into plist and text wrangler, thanks.
 

chown33

Moderator
Staff member
Aug 9, 2009
8,785
5,173
vertical
Its inside an array, inside a key, inside another dict. Its from a Safari browser history plist. ~/Library/Safari/History.plist.
Mine has three keys, the middle one has the info I need in it. I'm sure your will look very similar.
The thing is, PlistBuddy needs to know the specific key. If you don't know it, or can't provide it, then no one can tell PlistBuddy what to retrieve. "The middle one" isn't something PlistBuddy will understand.

Which version of Safari?
 

cqexbesd

macrumors regular
Jun 4, 2009
159
26
Germany
If its well formed XML you might also be able to use the xpath tool from the command line or even an XSLT.
 

Waragainstsleep

macrumors 6502
Original poster
Oct 15, 2003
295
54
UK
The thing is, PlistBuddy needs to know the specific key. If you don't know it, or can't provide it, then no one can tell PlistBuddy what to retrieve. "The middle one" isn't something PlistBuddy will understand.

Which version of Safari?
It should be the current version or pretty close.

I can delete the other keys easily enough using Xcode if you think that would help?
 

briloronmacrumo

macrumors 6502
Jan 25, 2008
486
297
USA
This isn't really a programming issue necessarily
But it could be solved reasonably easily with standard Foundation( Objective-C ) calls to retrieve standard plist data. In Foundation it is possible to retrieve all the data for a specific key and store in an array. From there the data could be formatted as needed ( maybe a CSV file ). Assuming a URL to the plist file is available:

Code:
NSArray *array = [[NSArray alloc] initWithContentsOfURL:url];
reads the entire plist into an array.

Code:
NSArray *values = [array valueForKey:@"visitCount"];
Retrieves all the values for the key "visitCount"

Possibly not the solution sought but off the shelf tools are sometimes inflexible while writing the code yourself is not.
 
Last edited:

subsonix

macrumors 68040
Feb 2, 2008
3,551
79
It should be the current version or pretty close.

I can delete the other keys easily enough using Xcode if you think that would help?
The point is that the structure of this file may differ between versions of Safari. The entries you are interested in with keys: title, lastVisitedDates and the url should be possible to retrieve with PlistBuddy, however the url has an empty key. Not sure how to deal with that in PlistBuddy, that is if there are special meta characters to deal with that. It's possible to just print the entire dict then use the usual suspects, grep, awk, sed etc to get the url out, but..

An alternative, is to parse the file with a library. Which you can do in Applescript which does have support for reading and writing property lists afaik, or in worst case use a regular xml parsing library for say Python.


I noticed that this history plist has a version number which you can get by:

Code:
PlistBuddy -c Print:WebHistoryFileVersion ~/Library/Safari/History.plist
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.