Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

david.cherrie

macrumors newbie
Original poster
Jun 17, 2015
7
0
Hi everyone,

I was wondering how to do a validation using an XML data file. To start off, here is the XML data feed I would like to cross check.

Code:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<GFX xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <FS01>
  <Description>Example Image 1</Description>
  <FileName>FS01.png</FileName>
  </FS01>
  <FS02>
  <Description>Example Image 2</Description>
  <FileName>FS02.png</FileName>
  </FS02>

I am prompting the user to type in the file code which is either FS01 or FS02. Currently I have my AppleScript working fine if you type in either of those values however if the user incorrectly types a code that isn't in the XML feed, I would like the display dialog to repeat. Currently if the FileCode is not found, the AppleScript stops working until you compile the script again inside the application.

Here is part of my current AppleScript.

Code:
set FileCode to text returned of (display dialog "ENTER FILE CODE" with title "DISPLAY GRAPHIC" default answer " ")

tell application "System Events"
  set xmlData to contents of XML file "Users/####/Desktop/GFXLookup.xml"
  tell XML element "GFX" of xmlData
  tell XML element FileCode
  set FileDescription to value of XML element "Description"
  set FileName to value of XML element "FileName"
  end tell
  end tell
end tell

I then use the FileDescription and FileName to display a confirmation dialog to then trigger the graphic.

Cheers,

David

 
Hi,

Maybe using a "Choose from list" would make the need for validation obsolete. For example:

Code:
tell application "System Events"
    set xmlData to contents of XML file "~/Desktop/GFXLookup.xml"
    tell XML element "GFX" of xmlData
        set theElementList to value of XML element "FileName" of every XML element
    end tell
end tell

--trim the file names, assuming they will always have a three letter file extension
set theTrimmedNames to {}
repeat with theElement in theElementList
    set theTrimmedNames to theTrimmedNames & {(characters 1 thru -5 of (theElement as text)) as text}
end repeat

set FileCode to choose from list theTrimmedNames with prompt "DISPLAY GRAPHIC"

Just a thought!
r.
 
Hi,

Maybe using a "Choose from list" would make the need for validation obsolete. For example:

Code:
tell application "System Events"
    set xmlData to contents of XML file "~/Desktop/GFXLookup.xml"
    tell XML element "GFX" of xmlData
        set theElementList to value of XML element "FileName" of every XML element
    end tell
end tell

--trim the file names, assuming they will always have a three letter file extension
set theTrimmedNames to {}
repeat with theElement in theElementList
    set theTrimmedNames to theTrimmedNames & {(characters 1 thru -5 of (theElement as text)) as text}
end repeat

set FileCode to choose from list theTrimmedNames with prompt "DISPLAY GRAPHIC"

Just a thought!
r.

Hey sorry I never replied. Thought I did.

I ended up using a repeat using a try command if the XML works then it exited the repeat, if it got an error message on try then the script would repeat.

Using a Choose from list for hundreds of values isn't the way to go.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.