Continuing the discussion from JavaScript in Browser vs ASObjC XPath:
@ShaneStanley has given us some great examples of how to use both XPath in a NSXMLDocument, and in getting ValueForKey in a NSDictionary. I now need to combine the two.
I have a complicated XML document (from a Keyboard Maestro Macro Action) in which I need to find, and change, the text in certain XML keys.
So, my basic question is how do I:
- use a relative XPath to find the key of interest
- get its
<string>
value, - change it (I can do this part)
- update the XML with this change.
Here are some typical XML paths I need to find:
Many differenct Actions, but the most common
<key>Variable</key><string>ABC__Item1</string>
Prompt for User Input
<key>Variables</key><array<dict><key>Default</key><string>|%Variable%ABC__Item1%|Choice 1|Choice 2</string>
Action Name that has been renamed by user to include the Var Name
<key>ActionName</key><string>Variable ABC__Item1 in Action Title</string>
Set Variable to Calculation
<key>Text</key><string>ABC__Item2 + 100</string>
All of these are relative paths. There are usually several XML levels (3–5+) above each of these.
Here’s a example of the full path:
<plist version="1.0">
<dict>
<key>Conditions</key>
<dict>
<key>ConditionList</key>
<array>
<dict>
<key>ConditionType</key>
<string>Variable</string>
<key>Variable</key>
<string>ABC__Item1</string>
where the relative path of interest is
<key>Variable</key>
<string>ABC__Item1</string>
In case you need it, here is an attachment of the full XML:
Example KM Action XML for Variable Prefix.xml.zip (1.5 KB)
The combination of these two ASObjC features/tools is huge, and will enable some processing not otherwise possible.
Many, many thanks.
BTW, I did try to figure this out myself, searching first Shane’s great book Everyday AppleScriptObjC, Third Edition, then this forum, all of my ASObjC script files, and, of course, the 'net.