Re: Getting started with XML?
Re: Getting started with XML?
- Subject: Re: Getting started with XML?
- From: kai <email@hidden>
- Date: Sat, 10 Feb 2007 00:35:57 +0000
On 8 Feb 2007, at 00:24, Luther Fuller wrote:
The only reliably safe method of reading a .plist file is to read
the whole file into a record, like this ...
-- plistAlias is an alias to a plist file
tell application "System Events"
plistAlias as text
set plistRec to (value of property list file the result)
end tell
That may be the case when one needs to extract a full or fairly
comprehensive record from the target file.
However, if the aim is to parse a small subset of that data (as was
the case under discussion), I can see little wrong with using the
tools provided for that very purpose.
As it turns out, Michelle decided that directly scripting iTunes
would be more appropriate to her particular needs. But as far as the
general extraction/parsing of a file's data is concerned, even
pulling a pair of values using the filtering techniques suggested is
likely to be considerably faster than iterating through every record.
(Each iteration would necessarily involve some existence tests for
the required property list items, or the concatenation of dummy
'missing value' records - either of which can be time-consuming
operations in a repeat-intensive situation.)
The following routine (which obviously assumes that the sequence, as
extracted, is irrelevant - or will be subsequently sorted anyway),
seems to work perfectly reliably here.
--------------
to merge_items into list_a from list_b around t
script o
property a : list_a
property b : list_b
end script
repeat with i from 1 to count list_a
set o's a's item i to o's a's item i & t & o's b's item i
end repeat
end merge_items
to add_text from t to l given prefixing:prefixing
if (count l) is 0 then return {}
set tid to text item delimiters
if prefixing then
set text item delimiters to return & t
set l to (t as Unicode text) & l
else
set text item delimiters to t & return
set l to (l as Unicode text) & t
end if
set text item delimiters to tid
l's paragraphs
end add_text
set plist_file to (choose file of type "public.xml" default location
(path to ¬
music folder) with prompt "Choose an iTunes xml file:" without
invisibles)
tell application "System Events" to tell property list item ¬
"Tracks" of property list file (plist_file's path)
tell (property list items where "Name" is in name of property list
items ¬
and "Artist" is in name of property list items)
set l to value of property list item "Name"
my (merge_items into l from value of property list item "Artist"
around " - ")
end tell
set l to l & my (add_text from " -" to value of property list item
"Name" of ¬
(property list items where "Name" is in name of property list items
and ¬
"Artist" is not in name of property list items) without prefixing)
set l to l & my (add_text from "- " to value of property list item
"Artist" of ¬
(property list items where "Name" is not in name of property list
items and ¬
"Artist" is in name of property list items) with prefixing)
repeat count (property list items where ¬
"Name" is not in name of property list items and ¬
"Artist" is not in name of property list items) times
set l's end to "-"
end repeat
l
end tell
--------------
---
kai
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/mailman//archives/applescript-users
This email sent to email@hidden