Hi,
I own a gps equipped heat rate monitor and want to extract the three fields from each report : time, elevation and heart rate. Here is the GPX file in a zip archive:
suuntoapp-Hiking-2026-01-29T09-57-12Z-track.gpx.zip (41.6 KB)
I have an Applescript which does what I want using multiple changes to the text item delimiters but is is slow taking just over three minutes to run on a 2013 MacbookPro.
I realise that the GPX format is a light weight form of XML and suspect that a dedicated XML parser could be used but I know nothing about them.
Here is my script but note when I say my script the handler that does all the hard work was written by forum user StefanK. See post https://www.macscripter.net/t/using-offset/50568/2
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
set thefile to POSIX path of (choose file with prompt "Please choose a file:" of type {"public.text"})
set fileHandle to open for access thefile
set tFile to read fileHandle
close access fileHandle
set tGPXData to text 1 thru -1 of tFile -- One very long string of <tags> and values with some spaces
######### Parse the Data creating a list of lists #########
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to tab
set {tid, text item delimiters} to {text item delimiters, {"<trkpt"}}
set tDataSet to {}
repeat with i from 2 to (count of text items of tGPXData)
set tRec to text item i of tGPXData
--set tTime to ReadRecTime(tRec)
set tTime to GetSubText(tRec, "<time>", "</time>")
set tTime to GetSubText(tTime, "T", "Z")
set tElevation to GetSubText(tRec, "<ele>", "</ele>")
set tHeartRate to GetSubText(tRec, "<gpxtpx:hr>", "</gpxtpx:hr>")
set DataRec to {PlotTime:tTime, Elevation:tElevation, HeartRate:tHeartRate}
set end of tDataSet to DataRec
end repeat
display dialog "Debug stop"
on GetSubText(SomeText, StartItem, enditem)
copy SomeText to OrigText
set tid to text item delimiters
if StartItem is not 1 then
set text item delimiters to StartItem
try
set SomeText to text items 2 thru -1 of SomeText as text
end try
end if
if enditem is not -1 then
set text item delimiters to enditem
set SomeText to text item 1 of SomeText
end if
set text item delimiters to tid
if SomeText is OrigText then return ""
return SomeText
end GetSubText
Any thoughts including :“don’t do it that way!”
best wishes
Simon