0

I am trying to parse data from a networked media player.

I can access the data on the player via the following endpoint:

http://192.168.10.2:8008/GetUserVars

This returns the following data:-

<?xml version="1.0" encoding="UTF-8"?>
<BrightSignUserVariables>
  <BrightSignVar name="CurrentLetter">W</BrightSignVar>
  <BrightSignVar name="CurrentUserName">&amp;Q&amp;W&amp;Q&amp;W&amp;Q&amp;W&amp;Q&amp;W</BrightSignVar>
</BrightSignUserVariables>

From this I need to use AppleScript to get the values from the BrightSignVar XML element node whose name attribute is CurrentLetter and CurrentUserName.

Essentially, I want to obtain these two text nodes:

  • W
  • &amp;Q&amp;W&amp;Q&amp;W&amp;Q&amp;W&amp;Q&amp;W

I tried the following example but got errors, any ideas?

set theXMLFile to ((http://192.168.10.2:8008/GetUserVars) as string)

tell application "System Events"

    set CurrUser to (value of XML element "CurrentUserName")

    set CurrLetter to (value of XML element "CurrentLetter")

end tell

Edit:

I have also tried the following:

set theXMLFile to (do shell script (("curl -X GET 'http://192.168.10.5:8008/GetUserVars'")))

tell application "System Events"

    set CurrUser to XML elements of XML element "BrightSignVar" of XML element "BrightSignUserVariables" of theXMLFile whose name is "CurrentUserName"

end tell

This returns the response as above however I get an error when trying to set CurrUser as variable:

can't make "BrightSignUserVariables" into type integer (-1700)

1
  • 1
    1. Your System Events code is missing a tell XML data of XML file theFilePath … end tell block. 2. System Events only works on local files so use curl (via Standard Additions’ do shell script command) to download the XML data to file first. Commented Mar 4, 2020 at 8:51

1 Answer 1

2

You were very close. Here's your last script with a few appropriate amendments that I believe should work:

set userVars to do shell script "curl -X GET 'http://192.168.10.5:8008/GetUserVars'"

tell application id "com.apple.SystemEvents"
    tell (make new XML data with data userVars)
        tell XML element "BrightSignUserVariables"
            tell every XML element whose name="BrightSignVar" to ¬
                set {letter, username} to its value
        end tell
    end tell
end tell

When I ran it using the XML response text you provided, the return value was:

{"W", "&Q&W&Q&W&Q&W&Q&W"}
Sign up to request clarification or add additional context in comments.

2 Comments

This was the part I missed tell (make new XML data with data userVars) when I first looked at answering this question. Nice! +1
Ditto what @user3439894 said. TIL, so thanks for sharing this CJK. I'd missed this answer previously which shows a slight variant of it.

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.