0

I am trying to read this xml-file with applescript.

<?xml version="1.0" encoding="UTF-8"?>
    <user>
        <username>benutzer</username>
        <password>passwort</password>
        <ip>127.0.0.1</ip>
    </user>

    <vars>
        <display>15</display>
        <sleep>60</sleep>
        <volume>22</volume>
        <app1>"Plex Media Server"</app1>
    </vars>

The original applescript looks like this:

do shell script "pmset displaysleep 15" password "mypassword" with administrator privileges
do shell script "pmset sleep 60" password "mypassword" with administrator privileges
set volume output 20
tell application "Plex Media Server" 
    quit
end tell
say "Media off"

How can I insert the value of "display" tp "displayslep", the value of "app1" as the application to quit etc.?

I am using several applescripts and this would make it so much easier changing certain values without having to change each script on its own. Also, I am a beginner with not too much coding knowledge.

Thanks in advance :)

2
  • Note that's not a valid XML document: there should be a single root element (perhaps you missed it?). Otherwise, the dictionary for AppleScript's System Events.app provided an "XML Suite" - it's not great, but is probably good enough for your needs as long as the actual XML is valid. Commented Feb 15, 2015 at 13:18
  • I tried something different using [this] (w3schools.com/xml/note.xml) XML filme. Applescript set Config to (("readme2.xml") as string) set name1 to value of "to" in Config this is as far as I get, then there is a syntax error (user not allowed). Commented Feb 15, 2015 at 14:37

1 Answer 1

1

In response to above comment, here's some example code:

tell application "System Events"
    set xmlDoc to make new XML data with properties {name:"note", text:"
    <note>
        <to>Tove</to>
        <from>Jani</from>
        <heading>Reminder</heading>
        <body>Don't forget me this weekend!</body>
    </note>"}
    get value of XML element "to" of XML element "note" of xmlDoc
end tell
--> "Tove"

Recommend investing in an AppleScript book - it's a huge pain to figure out this stuff without a guide.

(Obligatory: I co-wrote the Apress book.)

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

1 Comment

Thanks for the input. Is it possible to automatically read the xml-file instead of pasting it's entire content in the file like you did? The reason I am trying this is that I would like to only change info in one xml file and have all the applescripts automatically work with that info. Then set variables for the values, i.e. set sender to value of "to" in xml element", then use this variable in a command, i.e. say "Sender is (sender)". Or, in context of the original script: set sleeptime to value of "sleep" in xml element", then `do shell script "pmset displaysleep (sleeptime)"?

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.