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
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
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