Thursday, October 17, 2019

#1 2019-08-18 06:48:19 am

sticks1977
Member
Registered: 2018-12-05
Posts: 9

multiply a distance field within a text file?

firstly - thank you for clicking on this topic and I’m hopeful that you can assist with a file I’m attempting to alter and I figure an AppleScript might be able to help?

To try and condense my request - I went for a run on a treadmill today and the data from my smart watch doesn’t reflect the actual distance I covered correctly, so I’m attempting to multiply the <DistanceMeters> field by 1.152 – so the new figure in this field would be 1240.31117

The text in the file below is as follows, I have several hundred of these distance fields to change so I’m hopeful a script can speed up the process?

<Trackpoint>
<Time>2019-08-17T23:24:36.000Z</Time>
<DistanceMeters>1076.659</DistanceMeters>
<HeartRateBpm>
<Value>151</Value>
</HeartRateBpm>
<Extensions>
<ns3:TPX>
<ns3:Speed>2.6689999103546143</ns3:Speed>
<ns3:RunCadence>90</ns3:RunCadence>
</ns3:TPX>
</Extensions>
</Trackpoint>

I appreciate if you’re able to help as I’m not that familiar with programming language or how to create a script but would love to learn some day!

Regards, Shaun (sticks1977)

Model: iMac
Browser: Safari 605.1
Operating System: macOS 10.13

Offline

 

#2 2019-08-18 10:45:49 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 618

Re: multiply a distance field within a text file?

I assume that your files indeed are not text files. And the text you provided above is part of a dictionary artificially cut by you. It does not help. Therefore, if you want to be helped, the question is: what is the extension of the name of this file?


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#3 2019-08-18 01:40:14 pm

sticks1977
Member
Registered: 2018-12-05
Posts: 9

Re: multiply a distance field within a text file?

KniazidisR wrote:

what is the extension of the name of this file?

the extension of the file is .TCX - I’m able to export from Garmin Connect and open the file in TextEdit to modify the data, hence why I first mentioned it was a text file. Appreciate your reply KniazidisR :-)

Offline

 

#4 2019-08-18 10:15:34 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 618

Re: multiply a distance field within a text file?

OK, your file contains XML data. Something like that:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<TrainingCenterDatabase xmlns="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2 http://www.garmin.com/xmlschemas/TrainingCenterDatabasev2.xsd">
   
        <Activities>
              <Activity Sport="Running">
                  <Id>2012-04-18T17:39:02Z</Id>
                      <Lap StartTime="2012-04-18T17:39:02Z">
                          <TotalTimeSeconds>276.03000</TotalTimeSeconds>
                          <DistanceMeters>804.61000</DistanceMeters>
                          <MaximumSpeed>12.00000</MaximumSpeed>
                          <Calories>65.6582</Calories>
                          <Intensity>Active</Intensity>
                          <TriggerMethod>Location</TriggerMethod>
                          <Track>
                              <Trackpoint>
                                      <Time>2012-04-18T17:39:03Z</Time>
                                      <Position>
                                          <LatitudeDegrees>46.850575</LatitudeDegrees>
                                          <LongitudeDegrees>-96.77945</LongitudeDegrees>
                                      </Position>
                                      <AltitudeMeters>0</AltitudeMeters>
                                      <DistanceMeters>0.00000</DistanceMeters>
                                      <SensorState>Absent</SensorState>
                              </Trackpoint>
                              <Trackpoint>
                                      <Time>2012-04-18T17:39:06Z</Time>
                                      <Position>
                                          <LatitudeDegrees>46.850555</LatitudeDegrees>
                                          <LongitudeDegrees>-96.779522</LongitudeDegrees>
                                      </Position>
                                      <AltitudeMeters>0</AltitudeMeters>
                                      <DistanceMeters>5.84000</DistanceMeters>
                                      <SensorState>600</SensorState>
                              </Trackpoint>
                              <Trackpoint>
                                      <Time>2012-04-18T17:39:09Z</Time>
                                      <Position>
                                          <LatitudeDegrees>46.850523</LatitudeDegrees>
                                          <LongitudeDegrees>-96.77959</LongitudeDegrees>
                                      </Position>
                                      <AltitudeMeters>0</AltitudeMeters>
                                      <DistanceMeters>12.06000</DistanceMeters>
                                      <SensorState>600</SensorState>
                              </Trackpoint>

To get and set value of certain key (as key "DistanceMeters") I should know the full structure of your XML. So provide here your full XML, beginning from <?xml version="1.0" encoding="UTF-8" standalone="no" ?>

Last edited by KniazidisR (2019-08-18 10:22:35 pm)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#5 2019-08-18 11:00:32 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5958

Re: multiply a distance field within a text file?

If the format is as KniazidisR says, this should work:

Applescript:

use framework "Foundation"
use scripting additions

-- classes, constants, and enums used
property NSXMLDocumentTidyXML : a reference to 1024
property NSXMLNodePreserveAll : a reference to 4.29391875E+9

set theFile to POSIX path of (choose file)
set theURL to current application's NSURL's fileURLWithPath:theFile
set outPath to theURL's |path|()'s stringByDeletingPathExtension()'s stringByAppendingString:"-2.tcx"
set {theDoc, theError} to current application's NSXMLDocument's alloc()'s initWithContentsOfURL:theURL options:NSXMLNodePreserveAll |error|:(reference)
if theDoc is missing value then error theError's localizedDescription() as text
set {theNodes, theError} to theDoc's nodesForXPath:"//DistanceMeters" |error|:(reference)
if theNodes is missing value then error theError's localizedDescription() as text
set nf to current application's NSNumberFormatter's new()
nf's setFormat:"###0.00000#"
repeat with aNode in theNodes
   set oldValue to aNode's stringValue()
   set newValue to (1.152 * (oldValue as real))
   (aNode's setStringValue:(nf's stringFromNumber:newValue))
end repeat
theDoc's XMLString()
set theData to theDoc's XMLData()
theData's writeToFile:outPath atomically:true

That's also going to re-calculate the Lap distance, as well as the Trackpoint distances. if you don't want that, change "//DistanceMeters" to "//Trackpoint/DistanceMeters".

Last edited by Shane Stanley (2019-08-19 05:10:33 pm)


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Online

 

#6 2019-08-18 11:32:31 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 618

Re: multiply a distance field within a text file?

Wow. I was going to parse XML with pure AppleScript. But, probably, pure AppleScript is not able to work with nodes? smile


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#7 2019-08-18 11:37:21 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5958

Re: multiply a distance field within a text file?

KniazidisR wrote:

But, probably, pure AppleScript is not able to work with nodes? smile



If you mean System Events, it can probably be done — but it will be achingly slow for anything but the smallest of files. XPath simplifies things enormously.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Online

 

#8 2019-08-19 02:45:53 am

sticks1977
Member
Registered: 2018-12-05
Posts: 9

Re: multiply a distance field within a text file?

KniazidisR wrote:

So provide here your full XML, beginning from <?xml version="1.0" encoding="UTF-8" standalone="no" ?>


I figure it would be easier if you are able to see the whole file so I have uploaded the TCX file to WeTransfer for you to view.

I saved the script that Shane Stanley kindly created and when I attempted to run the script and click on the TCX file this error appeared:

ZNH40ZX.png

Appreciate the replies so far and helping me to try and save several hours of work to fix the data.

Regards, Shaun (sticks1977) smile

Offline

 

#9 2019-08-19 03:38:33 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5958

Re: multiply a distance field within a text file?

Sorry, I left two vital lines needed at the top of the script:

Applescript:

use framework "Foundation"
use scripting additions


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Online

 

#10 2019-08-19 04:22:16 am

sticks1977
Member
Registered: 2018-12-05
Posts: 9

Re: multiply a distance field within a text file?

Thanks Shane for your reply - I added the two lines to the top of the script, it started running and all was looking good... until...

Hrbv0iv.png

Model: iMac
AppleScript: v2.10
Browser: Chrome
Operating System: macOS 10.13

Offline

 

#11 2019-08-19 07:00:36 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5958

Re: multiply a distance field within a text file?

Make that four lines I omitted — I've modified the version above to include them,


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Online

 

#12 2019-08-19 12:34:40 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 618

Re: multiply a distance field within a text file?

I wrote pure AppleScript code. But how can I save changes in XML?  Need help.

Applescript:


set theXMLFile to POSIX path of (choose file)

tell application "System Events"
   
   set theXMLFile to XML file theXMLFile
   set Activity to XML element 1 of XML element "TrainingCenterDatabase" of theXMLFile
   set ActivitySportRunning to XML element of Activity
   set Laps to XML elements of item 1 of ActivitySportRunning whose name is "Lap"
   
   repeat with i from 1 to (count of Laps)
       set DistanceMeters to (XML elements of item i of Laps whose name is "DistanceMeters")
       repeat with j from 1 to (count of DistanceMeters)
           set DistanceMeter to item j of DistanceMeters
           set oldValue to (value of DistanceMeter) as real
           set newValue to (1.152 * oldValue) as string
           set DistanceMeter's value to newValue
       end repeat
   end repeat
   
end tell

Last edited by KniazidisR (2019-08-19 12:42:33 pm)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#13 2019-08-19 02:26:48 pm

sticks1977
Member
Registered: 2018-12-05
Posts: 9

Re: multiply a distance field within a text file?

Shane Stanley wrote:

Make that four lines I omitted — I've modified the version above to include them,

Thanks again Shane - I have ran the script this morning and there is an error with the last line... I am absolutely blown away that people have knowledge like yourself to be able to automate tasks such as these and willing to help and share your time.

The error that appears is as per the screenshot below:

htWT6MV.png

Model: iMac
Browser: Chrome
Operating System: macOS 10.13

Offline

 

#14 2019-08-19 05:11:01 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5958

Re: multiply a distance field within a text file?

My apologies — please try the updated code.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Online

 

#15 2019-08-19 06:15:05 pm

sticks1977
Member
Registered: 2018-12-05
Posts: 9

Re: multiply a distance field within a text file?

Shane Stanley wrote:

My apologies — please try the updated code.

You do not have to apologise!! you have been a great help!

I'm happy to report that it WORKED!!! the script runs perfectly and all the data has been updated to reflect the new distance - thank you so very much Shane.

Regards, Shaun (sticks1977) big_smile

Offline

 

#16 2019-08-19 06:22:49 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5958

Re: multiply a distance field within a text file?

KniazidisR wrote:

But how can I save changes in XML?



From memory, the key is something like:

Applescript:

make new property list file with properties {contents:whatEver, name:pathForFile}


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Online

 

#17 2019-08-25 09:28:37 pm

sticks1977
Member
Registered: 2018-12-05
Posts: 9

Re: multiply a distance field within a text file?

Shane Stanley wrote:

My apologies — please try the updated code.

Is it possible to add other variables in the same script? For example if I am wanting to multiply the Speed field by the same factor of 1.152?

Offline

 

#18 2019-08-26 02:00:34 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5958

Re: multiply a distance field within a text file?

sticks1977 wrote:

For example if I am wanting to multiply the Speed field by the same factor of 1.152?



Change this line:

Applescript:

set {theNodes, theError} to theDoc's nodesForXPath:"//DistanceMeters" |error|:(reference)

to:

Applescript:

set {theNodes, theError} to theDoc's nodesForXPath:"//DistanceMeters|//ns3:Speed" |error|:(reference)


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Online

 

#19 2019-08-26 02:19:32 am

sticks1977
Member
Registered: 2018-12-05
Posts: 9

Re: multiply a distance field within a text file?

Shane Stanley wrote:

Change this line:

Applescript:

set {theNodes, theError} to theDoc's nodesForXPath:"//DistanceMeters" |error|:(reference)

to:

Applescript:

set {theNodes, theError} to theDoc's nodesForXPath:"//DistanceMeters|//ns3:Speed" |error|:(reference)

Thanks again Shane for your prompt reply :-)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)