If in case you have an XML file, it is normally advisable to parse it as XML somewhat than as plain textual content. System Occasions affords a set of a instructions that may descend an XML object hierarchy, and extract the attributes related to every ingredient.
The XML snippet you supplied is not truly legitimate XML, because it accommodates an extraneous ingredient. So, to be able to reveal the parsing of your XML snippet, I’ve eliminated the primary of the 2 closing
tags:
inform software id "com.apple.SystemEvents"
set xml to make new XML information with information "
"
inform xml's XML ingredient "backbone" to inform ¬
XML ingredient "asset-clip" to inform ¬
XML ingredient "marker" to get the ¬
worth of XML attribute "begin"
finish inform
This returns the anticipated worth "6912372957/80000s"
.
Since you will be studying the XML from an XML file somewhat than a string as I did above, you’d begin by changing the road of code within the above script that begins:
set xml to ...
(together with the XML string that’s cut up over a number of strains) with:
set xml to XML ingredient 1 of XML file "/path/to/file.xml"
XML ingredient 1
represents the basis ingredient, which is all the time the start line when traversing an XML hierarchy. Within the XML string I assigned to the variable xml
within the above AppleScript code, the basis ingredient was the named XML ingredient "backbone"
, but when that is, itself, a baby of another ascendant XML ingredient in your file, then it will not be the basis ingredient.
You will must assemble the AppleScript reference chain that takes System Occasions‘s XML parser from XML ingredient 1
(the basis ingredient, which is completely wonderful to reference by the index quantity 1
as a substitute of its title) down by means of the XML object mannequin to the XML ingredient "backbone"
, from the place my code takes it the remainder of the best way (until the extraneous tag is not, in reality, extraneous within the context of the file as a complete, which can have to be mirrored within the AppleScript XML ingredient
chain.
My clarification in all probability makes it sound harder than it’s in follow, and when you use my AppleScript code as a information, you possibly can see it is fairly easy, and likewise leads to fewer strains of code—that can be going to do a extra dependable job—in comparison with any try to parse the XML file as textual content.
In the identical method we learn the begin
attribute of the
tag, we are able to additionally assign a brand new worth to the offset
attribute of the
tag.
The place AppleScript was instructed to:
get the worth of XML attribute "begin"
you’d assign this worth to a variable, then:
set the worth of XML attribute "offset" to ...
You’ll, in fact, want to focus on the XML ingredient "asset-clip"
in the identical hierarchical method as was executed for XML ingredient "marker"
. However that does not entail any extra work, and what you find yourself with is a script that actually does what must be executed: that’s to say, it finds the worth of some particular attribute, and makes use of it to interchange the present worth of another particular attribute. It is two operations.
Nevertheless, there is a ultimate, third operation. The XML containing the brand new worth for the offset
attribute can be saved within the xml
variable, by means of which all the things else is referenced. This must be written out to file.
A textual content illustration of the XML will be obtained by means of xml's textual content
.
To keep away from information loss, I would advise writing out the XML textual content to a separate file, then changing the outdated file when you validate the brand new one. In actual fact, your unique strategy was smart, particularly duplicating the XML file earlier than studying from it, which you’ll be able to merely overwrite with the brand new contents with out an excessive amount of fear.
Duplicating needs to be executed by way of Finder:
inform software id "com.apple.finder" to duplicate ¬
the file ("/path/to/file.xml" as POSIX file) ¬
to ("/path/to/newfile.xml" as POSIX file)
Writing out is completed like so:
inform software id ("com.apple.SystemEvents")
.
.
(* all of the code to get and exchange
the attributes's values *)
.
.
set xmlText to the xml's textual content
finish inform
set the eof of "/path/to/newfile.xml" to 0 --> scrubs the file
write the xmlText as "utf8" to "/path/to/newfile.xml"