Parse XML To Set Variables

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

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

This Returns The Following Data:-

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
&Q&W&Q&W&Q&W&Q&W

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

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 is an alternative method, using XPath via AppleScriptObjC:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set theURL to current application's |NSURL|'s URLWithString:"http://192.168.10.5:8008/GetUserVars"
set {theData, theError} to current application's NSData's dataWithContentsOfURL:theURL options:0 |error|:(reference)
if theData = missing value then error (theError's localizedDescription() as text)
set {theXMLDoc, theError} to current application's NSXMLDocument's alloc()'s initWithData:theData options:(current application's NSXMLDocumentTidyHTML) |error|:(reference)
if theXMLDoc = missing value then error (theError's localizedDescription() as text)
set {theMatches, theError} to (theXMLDoc's nodesForXPath:"//BrightSignVar[@name = 'CurrentLetter'] | //BrightSignVar[@name = 'CurrentUserName']" |error|:(reference))
if theMatches = missing value then error (theError's localizedDescription() as text)
set theStrings to (theMatches's valueForKey:"stringValue") as list

Thanks Shane

Im Not Familiar With ApplescriptObjC

When I Run There Script I Get The Response…

Ideally What I Want To Be Able To Do Is Set The Two Values As Variables

eg

How Do I Implement That??

Try this:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set theXML to (do shell script (("curl -X GET 'http://192.168.10.5:8008/GetUserVars'")))
set {theXMLDoc, theError} to current application's NSXMLDocument's alloc()'s initWithXMLString:theXML options:(current application's NSXMLDocumentTidyHTML) |error|:(reference)
if theXMLDoc = missing value then error (theError's localizedDescription() as text)
set {theMatches, theError} to (theXMLDoc's nodesForXPath:"//BrightSignVar[@name = 'CurrentLetter']" |error|:(reference))
if theMatches = missing value then error (theError's localizedDescription() as text)
set CurrUser to (theMatches's firstObject()'s valueForKey:"stringValue") as text
set {theMatches, theError} to (theXMLDoc's nodesForXPath:"//BrightSignVar[@name = 'CurrentUserName']" |error|:(reference))
if theMatches = missing value then error (theError's localizedDescription() as text)
set CurrLetter to (theMatches's firstObject()'s valueForKey:"stringValue") as text

Thanks Shane
Works A Treat.
One Odd Issue - I’m Running This In Script Editor.
When I try to save I get the error.

Any Ideas How To Get Around This??

Force recompile and it will save.

Hi Shane
Thanks For Your Help With This Topic.
Your Solution Works Great.

As Mentioned, I’m Not Really Familiar With ObjC.

Quick Question…
How Can I Send Data The Other Way.

Eg On The Same XML File (http://192.168.10.5) I want to set the value of “CurrentUserName” to “Dan White”

Something like this:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set theXML to (do shell script (("curl -X GET 'http://192.168.10.5:8008/GetUserVars'")))
set {theXMLDoc, theError} to current application's NSXMLDocument's alloc()'s initWithXMLString:theXML options:(current application's NSXMLDocumentTidyHTML) |error|:(reference)
if theXMLDoc = missing value then error (theError's localizedDescription() as text)
set {theMatches, theError} to (theXMLDoc's nodesForXPath:"//BrightSignVar[@name = 'CurrentUserName']" |error|:(reference))
theMatches's firstObject()'s setStringValue:"Dan White"
set xmlText to (theXMLDoc's XMLStringWithOptions:(current application's NSXMLNodePrettyPrint)) as text
-- use curl to upload here

Thanks Shane

Can you check my mark up for posting with curl - struggling…

curl -X POST theXML "'http://192.168.10.5:8008/GetUserVars'"

Nope. Maybe someone else can…