APPLESCRIPT Using XML Data to Validate Display Dialog Input

Discussion in 'Mac Programming' started by david.cherrie, Jun 30, 2015.

  1. david.cherrie macrumors newbie

    Joined:
    Jun 17, 2015
    #1
    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

     
  2. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #2
    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.
     
  3. david.cherrie thread starter macrumors newbie

    Joined:
    Jun 17, 2015
    #3
    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.
     

Share This Page