I’m totally new to AppleScript and I do have a question for my AppleScript in Lion.
Would it be possible to convert a .loc file from Geocaching to a simple comma delimited text (CSV) file?
The structure of the file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<loc version="1.0" src="Groundspeak">
<waypoint>
<name id="GC22ZJ3"><![CDATA[Morde im Wald - Fall No. 02 by Vandergore]]>
</name>
<coord lat="50.793" lon="6.19045"/>
<type>Geocache</type>
<link text="Cache Details">http://www.geocaching.com/seek/cache_details.aspx?wp=GC22ZJ3</link>
<difficulty>1.5</difficulty>
<terrain>1.5</terrain>
<container>2</container>
</waypoint><waypoint>
<name id="GC22XA1"><![CDATA[Morde im Wald - Fall No. 03 by Vandergore]]>
</name>
<coord lat="50.6522" lon="6.073233"/>
<type>Geocache</type>
<link text="Cache Details">http://www.geocaching.com/seek/cache_details.aspx?wp=GC22XA1</link>
<difficulty>1.5</difficulty>
<terrain>1.5</terrain>
<container>2</container>
</waypoint></loc>
The first line should contain a headline in the CSV that would describe the fields.
Output (just an example! The headline can be differrent):
waypoint_id,waypoint_name,coord_lat,coord_lon,type,link,difficulty,terrain,container
GC22ZJ3,Morde im Wald - Fall No. 02 by Vandergore,50.793,6.19045,Geocache,http://www.geocaching.com/seek/cache_details.aspx?wp=GC22ZJ3,1.5,1.5,2
GC22XA1,Morde im Wald - Fall No. 03 by Vandergore,50.6522,6.073233,Geocache,http://www.geocaching.com/seek/cache_details.aspx?wp=GC22XA1,1.5,1.5,2
set xmlFile to (choose file) as text
set nameList to {}
set valueList to {}
tell application "System Events"
tell XML file xmlFile
set rootElement to XML element 1 of its contents
repeat with i from 1 to (count XML elements of rootElement)
set anElement to XML element i of rootElement
set entryList to {}
set allEntries to XML elements of anElement
repeat with j from 1 to (count allEntries)
set anEntry to item j of allEntries
set entryName to name of anEntry
if entryName is not "coord" then
if i = 1 then set end of nameList to entryName
set end of entryList to value of anEntry
end if
if XML attributes of anEntry is not {} then
repeat with anAttribute in XML attributes of anEntry
if i = 1 then set end of nameList to entryName & "_" & name of anAttribute
set end of entryList to value of anAttribute
end repeat
end if
end repeat
set end of valueList to entryList
end repeat
end tell
end tell
set csvList to {}
set {TID, text item delimiters} to {text item delimiters, ","}
set end of csvList to nameList as text
repeat with aList in valueList
set end of csvList to aList as text
end repeat
set text item delimiters to return
set csvText to csvList as text
set text item delimiters to TID
display dialog csvText
set xmlFile to (choose file) as text
set nameList to {}
set valueList to {}
tell application "System Events"
tell XML file xmlFile
set rootElement to XML element 1 of its contents
repeat with i from 1 to (count XML elements of rootElement)
set anElement to XML element i of rootElement
set entryList to {}
set allEntries to XML elements of anElement
repeat with j from 1 to (count allEntries)
set anEntry to item j of allEntries
set entryName to name of anEntry
if entryName is not "coord" then
if i = 1 then set end of nameList to entryName
set end of entryList to value of anEntry
end if
if XML attributes of anEntry is not {} then
repeat with anAttribute in XML attributes of anEntry
if name of anAttribute is not "text" then
if i = 1 then set end of nameList to entryName & "_" & name of anAttribute
set end of entryList to value of anAttribute
end if
end repeat
end if
end repeat
set end of valueList to entryList
end repeat
end tell
end tell
set csvList to {}
set {TID, text item delimiters} to {text item delimiters, ","}
set end of csvList to nameList as text
repeat with aList in valueList
set end of csvList to aList as text
end repeat
set text item delimiters to return
set csvText to csvList as text
set text item delimiters to TID
set fileName to (choose file name) as text
if fileName does not end with ".csv" then set fileName to fileName & ".csv"
try
set fRef to open for access file fileName with write permission
write csvText to fRef as «class utf8»
close access fRef
on error
try
close access file fileName
end try
end try