You are not logged in.
Hello Experts,
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:
Applescript:
<?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):
Applescript:
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
Last edited by Yogie (2011-09-13 02:12:42 am)
Offline
Nobody ready to help me out?
I do have already XML tools installed but I'm lost as I'm not a programmer, so I wonder if someone could give me a hint how to start...?
Offline
Hi,
try this
Applescript:
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
Last edited by StefanK (2011-09-13 04:04:18 am)
Offline
Wow,
thank you so much! You rock!
That seems to work perfect! Alas I'm not able to use the output as I was not able to copy & paste it, to save and see the result in detail.
Would it be possible to exclude the link_text from the output and save the output to a file?
Many many thanks!
Offline
no problem
Applescript:
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
Offline
Hi Stefan!
Unbelievable that you can do this in such a short time! You are more than great!
Thank you so much for helping me out you delivered more than I expected, it is a total solution for what I was looking for, you save me a lot of time...
Thank you so much again!
Last edited by Yogie (2011-09-13 05:06:52 am)
Offline