0

So I recently found out about XML-Parsing in Applescript. I wanted to parse an XML file that had a structure like this:

<example>
    <sample value="hello world"/>
</example>

So I tried something like this:

tell application "System Events"
    tell XML file "/path/to/the/file.xml"
        tell XML element "example"
            return XML attribute "value" of XML element "sample"
        end tell
    end tell
end tell

However, the error says:

System Events got an error: Can’t get XML element "example" of XML file "~/Desktop/xml.xml" number -1728.

According to MacErrors.h number -1728 means errAENoSuchObject, so no such object was found. What am I doing wrong?

8
  • Are you sure you’re reading the right file? The error message suggests xml.xml doesn’t have a root element named “example”. Try get name of every XML element of XML file "~/Desktop/xml.xml" to see. Commented Sep 12, 2021 at 9:26
  • @foo Doesn't work. I have now learned an important thing though: Never work with files in Applescript. Commented Sep 12, 2021 at 9:30
  • FWIW, if you’re working with large or complex XML documents, you can also use Foundation’s NSXML classes via the AppleScript-ObjC bridge. The ObjC API’s a bit more complex, but is faster than using System Events and supports more powerful XPath queries. Commented Sep 12, 2021 at 9:31
  • What doesn’t work? Be sure to enclose that get command in a tell app "System Events" block, e.g. tell app "System Events" to get name of every XML element of XML file "~/Desktop/xml.xml". Referencing files is a mess in AppleScript, but in this case you’re dealing with the System Events app so just give it a valid POSIX path string to your XML file. If can’t find the file it’ll tell you, e.g. “System Events got an error: Can’t get XML file "~/tesst.xml".” number -1728. Your error message indicates it found the file okay, but can’t find the root <example>…</example> element within it. Commented Sep 12, 2021 at 9:44
  • @foo Just pasting the entire thing you provided here results in an error number -1703, which means some data is in the wrong type. Commented Sep 12, 2021 at 9:53

1 Answer 1

1

You almost had it:

return XML attribute "value" of XML element "sample"

Should be:

return the value of XML attribute "value" of XML element "sample"

This works for me:

tell application "System Events"
    tell XML file "/path/to/the/file.xml"
        tell XML element "example"
            return the value of XML attribute "value" of XML element "sample"
        end tell
    end tell
end tell



In Terminal:

% file foobar.xml
foobar.xml: XML 1.0 document text, ASCII text
% cat foobar.xml
<?xml version="1.0" encoding="UTF-8"?>
<example>
    <sample value="hello world"/>
</example>
% 

In Script Editor:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.