Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

APPLESCRIPT Using XML Data to Validate Display Dialog Input

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.


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



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

MacBook Pro (Retina, 15-inch, Mid 2014), OS X Yosemite (10.10.4)

Posted on Jun 30, 2015 5:41 PM

Reply
4 replies

Jun 30, 2015 6:38 PM in response to David.Cherrie

David.Cherrie wrote:



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.


Would this approach work for you (i.e. choose from list rather than type in value that you have to validate)?


set fileCodesList to {"FS01", "FS02"}

choose from listfileCodesListwith prompt "Choose a file code:"

set fileCode to (result as text)


SG

Jun 30, 2015 8:25 PM in response to David.Cherrie

You can place the dialog and test in a repeat statement to continually repeat until a valid entry is entered, using a try statement to capture any error:


set xmlFile to ((path to desktop) as text) & "GFXLookup.xml"

repeat -- forever
  set fileCode to text returned of (display dialog "ENTER FILE CODE" with title "DISPLAY GRAPHIC" default answer " ")
  try
    tell application "System Events"
      set xmlData to contents of XML file xmlFile
      tell XML element "GFX" of xmlData to tell XML element fileCode
        set fileDescription to value of XML element "Description"
        set fileName to value of XML element "FileName"
      end tell
    end tell
    exit repeat -- success
  on error errmess
    log errmess -- fail
  end try
end repeat

# continue

APPLESCRIPT Using XML Data to Validate Display Dialog Input

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.