You probably have an XML file, it is often advisable to parse it as XML fairly than as plain textual content. System Occasions provides a set of a instructions that may descend an XML object hierarchy, and extract the attributes related to every aspect.
The XML snippet you supplied is not truly legitimate XML, because it incorporates an extraneous aspect. So, to be able to exhibit 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 aspect "backbone" to inform ¬
XML aspect "asset-clip" to inform ¬
XML aspect "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 fairly than a string as I did above, you’ll begin by changing the road of code within the above script that begins:
set xml to ...
(together with the XML string that’s break up over a number of traces) with:
set xml to XML aspect 1 of XML file "/path/to/file.xml"
XML aspect 1
represents the foundation aspect, which is at all times the start line when traversing an XML hierarchy. Within the XML string I assigned to the variable xml
within the above AppleScript code, the foundation aspect was the named XML aspect "backbone"
, but when that is, itself, a baby of another ascendant XML aspect in your file, then it will not be the foundation aspect.
You will have to assemble the AppleScript reference chain that takes System Occasions‘s XML parser from XML aspect 1
(the foundation aspect, which is completely effective to reference by the index quantity 1
as an alternative of its title) down by means of the XML object mannequin to the XML aspect "backbone"
, from the place my code takes it the remainder of the way in which (until the extraneous tag is not, the truth is, extraneous within the context of the file as an entire, which can should be mirrored within the AppleScript
XML aspect
chain.
My clarification in all probability makes it sound harder than it’s in follow, and in the event you use my AppleScript code as a information, you’ll be able to see it is fairly simple, and likewise leads to fewer traces of code—that can also be going to do a extra dependable job—in comparison with any try and parse the XML file as textual content.
In the identical method we learn the begin
attribute of the
tag, we will 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’ll 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 aspect "asset-clip"
in the identical hierarchical method as was accomplished for XML aspect "marker"
. However that does not entail any extra work, and what you find yourself with is a script that actually does what must be accomplished: 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.
Nonetheless, there is a ultimate, third operation. The XML containing the brand new worth for the offset
attribute will probably 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 might be obtained by the use 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 previous file when you validate the brand new one. Actually, your authentic 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 accomplished through 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 change
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"