Sunday, November 23, 2014

#1 2005-06-07 06:13:59 pm

OnlyTheSnake
Member
Registered: 2005-05-26
Posts: 29

how do i do this?

i have a display dialog which has text. i need to know how to go about making this scenario work -->
1. dialog pops up
2. some value is entered into text field
3. script recognises that the value entered is not of correct type
4. dialog pops up to tell of error
5. same dialog from #1 pops up again

i want to know how to make this happen over and over, so if i keep making errors, it keeps taking me back to the original dialog again. thanks for any help.

Model: iBook G4
AppleScript: v2.1 (80)
Browser: Safari v2.0 (412)
Operating System: Mac OS X (10.4)

Offline

 

#2 2005-06-07 11:53:01 pm

sitcom
Member
Registered: 2005-03-14
Posts: 367

Re: how do i do this?

Applescript:


repeat

--1. dialog pops up
--2. some value is entered into text field
   set ThisValue to display dialog "Enter value" default answer "1"
   try
       set ThisValue to text returned of ThisValue as number
   on error
       set ThisValue to text returned of ThisValue as text
   end try

--3. script recognises that the value entered is not of correct type
set ThisType to (class of ThisValue) as string
   
display dialog "Correct type is integer" & return & "Value entered is " & ThisType

set CorrectType to "integer"

   if ThisType is not CorrectType then
       display dialog "Incorrect type of value"--4. dialog pops up to tell of error
else
       if ThisType is CorrectType then display dialog "Correct Type"
   end if
end repeat--5. same dialog from #1 pops up again

SC

Offline

 

#3 2005-06-08 12:11:16 am

Macrajeev
Member
Registered: 2005-06-06
Posts: 228

Re: how do i do this?

Hi
I don't understand which type of value you are entering. Basically error will coame when I perform any checks against the entered value.
Let me know exact what you want see the code below:

Applescript:

repeat
   set input_number to text returned of (display dialog "Please enter the number" default answer "")
   display dialog "You have not enter the valid value. Please enter again"
   
end repeat

Offline

 

#4 2005-06-08 12:56:06 am

sitcom
Member
Registered: 2005-03-14
Posts: 367

Re: how do i do this?

Applescript:


repeat
   set CorrectValue to "Whatever"
   
   --1. dialog pops up
   --2. some value is entered into text field
   set ThisValue to display dialog "Enter value" default answer "Whatever"
   
   set ThisValue to text returned of ThisValue
   
   --3. script recognises that the value entered is not of correct type
   
   if ThisValue is not CorrectValue then
       display dialog "Incorrect Value" --4. dialog pops up to tell of error
   else
       if ThisValue is CorrectValue then display dialog "Correct Value"
   end if
end repeat --5. same dialog from #1 pops up again

I think I was over analyzing what you were looking for.

"script recognises that the value entered is not of correct type"
'value class' is how AppleScript refers to types of data. That's what the first script did.
How's this one?
SC

Offline

 

#5 2005-06-08 04:11:47 am

OnlyTheSnake
Member
Registered: 2005-05-26
Posts: 29

Re: how do i do this?

sitcom, those 2 scripts are almost like what i wanted. the only thing is, even after i have entered the correct value, the script will still repeat. how do i make it such that the script will repeat as long as the value is of the wrong type, but will stop repeating once the value is of the appropriate type?

Macrajeev, basically i have a script which requires an input of 3 digits. i want to make it such that the dialog part of the script will repeat itself until it has an appropriate 3 digit value, then move on with the rest of the script.

thanks for all the help.

Offline

 

#6 2005-06-08 04:18:36 am

jonn8
Member
From: New York
Registered: 2003-05-01
Posts: 1766
Website

Re: how do i do this?

This is how I validate input:

Applescript:

set a_date to my get_validated_input("Enter a date:", "6/8/05", {date}) -->date "Wednesday, June 8, 2005 12:00:00 AM"
set an_integer to my get_validated_input("Enter an integer:", 1, {integer}) -->1
set a_real to my get_validated_input("Enter a real:", 1.0, {real}) -->1.0
set a_string to my get_validated_input("Enter a string:", "a string", {Unicode text, string}) -->"a string"

on get_validated_input(the_prompt, default_answer, valid_types)
   set {the_error, the_icon} to {"", 1}
   repeat
       try
           set the_result to text returned of (display dialog the_error & the_prompt default answer default_answer buttons {"Cancel", "OK"} default button 2 with icon the_icon)
           repeat with i from 1 to (count valid_types)
               try
                   set valid_result to (run script "get \"" & the_result & "\" as " & (item i of valid_types) as Unicode text)
                   return valid_result
               on error
                   try
                       set valid_result to (run script ("get " & (item i of valid_types) as Unicode text) & " \"" & the_result & "\"")
                       return valid_result
                   end try
               end try
           end repeat
           return valid_result
       on error e number n
           if n = -128 then return default_answer --user cancelled
           set {the_error, the_icon} to {("The input “" & the_result & "” is not a valid " & (item 1 of valid_types) as Unicode text) & ". Please try again." & return & return, 2}
       end try
   end repeat
end get_validated_input

Jon

Last edited by jonn8 (2005-06-08 09:52:41 am)

Offline

 

#7 2005-06-08 04:45:32 am

OnlyTheSnake
Member
Registered: 2005-05-26
Posts: 29

Re: how do i do this?

ok jonn8, the script works fine on its own but to use it i need to understand how it works. i'm up very late now so my brain's not functioning properly. would it be possible for you to add more comments in the script so i know what's going on? thanks for the help. big_smile

Offline

 

#8 2005-06-08 10:04:45 am

jonn8
Member
From: New York
Registered: 2003-05-01
Posts: 1766
Website

Re: how do i do this?

Applescript:

set a_date to my get_validated_input("Enter a date:", "6/8/05", {date}) -->date "Wednesday, June 8, 2005 12:00:00 AM"
set an_integer to my get_validated_input("Enter an integer:", 1, {integer}) -->1
set a_real to my get_validated_input("Enter a real:", 1.0, {real}) -->1.0
set a_string to my get_validated_input("Enter a string:", "a string", {Unicode text, string}) -->"a string"

on get_validated_input(the_prompt, default_answer, valid_types)
   --set the error to blank (we don't have an error yet...) and the icon to the standard icon:
   set {the_error, the_icon} to {"", 1}
   repeat
       try
           --get the input from the user using the prompt and default answer supplied
           --in the parameters of the handler:
           set the_result to text returned of (display dialog the_error & the_prompt default answer default_answer buttons {"Cancel", "OK"} default button 2 with icon the_icon)
           --evaluate the user's input; since we accepted a list of valid types
           --we need to iterate through the types:
           repeat with i from 1 to (count valid_types)
               try
                   --since the list of valid_types are classes, you can't
                   --simply use get the_result as (item i of valid_types) because
                   --AppleScript is expecting a class, not a variable
                   --to work around this, I create a new script that allows me to
                   --pass the class as a string and still get the desired result:
                   set valid_result to (run script "get \"" & the_result & "\" as " & (item i of valid_types) as Unicode text)
                   --if this is a valid coercion, then return the result:
                   return valid_result
               on error
                   try
                       --some classes don't allow coercions with a "the_result as «class»" command
                       --(date is one such class) so you have to use "«class» the_result" if
                       --the first (more common) coercion fails:
                       set valid_result to (run script ("get " & (item i of valid_types) as Unicode text) & " \"" & the_result & "\"")
                       --if this is a valid coercion, then return the result:
                       return valid_result
                   end try
               end try
           end repeat
           --if we get to this point, there was no valid coercion and
           --valid_result is not defined so trying to return it
           --will cause an error:
           return valid_result
       on error e number n
           --on the error, do something:
           if n = -128 then return default_answer --user cancelled so just return the default_answer
           --if it wasn't a user cancelled error, and, rather, that there was no valid_result
           --change the error from the default blank to a more descriptive error
           --and use the alert icon to signify a problem and then cycle through
           --the whole process again:
           set {the_error, the_icon} to {("The input “" & the_result & "” is not a valid " & (item 1 of valid_types) as Unicode text) & ". Please try again." & return & return, 2}
       end try
   end repeat
end get_validated_input

Jon

Last edited by jonn8 (2005-06-08 10:05:17 am)

Offline

 

#9 2012-04-25 06:00:50 am

alastor933
Member
From: Utrecht, NL
Registered: 2008-09-12
Posts: 458

Re: how do i do this?

I found Jon's script when looking for ways to validate a user entered date.
I cannot determine whether it was safe when written, but it has a hole in Snow Leopard (AppleScript 2.1.2):   

Applescript:

set validResult to (run script (("get " & typeClass) as text) & " \"" & theResult & "\"")

This will accept any day up to 31, regardless of the month, and overflow will occur with months with less than 31 days.
I added a test to compare the entered month with that of valid_result:

Applescript:

-- default answer reflects expected format
set theDate to validateInput("Enter a date:", "20/8/05", date) -->date "zaterdag 20 augustus 2005 00:00:00"

on validateInput(thePrompt, defaultAnswer, typeClass)
   set {theError, theIcon} to {"", 1}
   repeat
       try
           set theResult to text returned of (display dialog theError & thePrompt default answer defaultAnswer buttons {"Cancel", "OK"} default button 2 with icon theIcon)
           try
               set validResult to (run script "get \"" & theResult & "\" as " & typeClass as text)
               return validResult
           on error
               try
                   -- If here it's a date; must perform test to prevent overflow
                   -- The 'run script' will accept any day up to 31,
                   -- regardless of the month, and overflow may occur
                   set validResult to (run script (("get " & typeClass) as text) & " \"" & theResult & "\"")
                   -- Check if its month is same as entered month
                   -- make it so it works for both dd/mm and mm/dd format
                   -- As the day is always 29, 30 or 31 with this error
                   -- a situation where month & day are the same integer should not occur
                   set text item delimiters to "/"
                   set {firstItem, secondItem} to {text item 1, text item 2} of theResult
                   set text item delimiters to ""
                   set monthResult to month of validResult as integer
                   if (monthResult ≠ (firstItem as integer)) and (monthResult ≠ (secondItem as integer)) then
                       -- overflow occurred, it's not the entered date
                       beep
                       error -- up a level...
                   else -- it's valid, return result
                       return validResult
                   end if
                   error -- ...and up...
               end try
               error "Overflow error!" -- tell 'm
           end try
           
           return validResult -- undefined - raises error
           
       on error e number n
           if n = -128 then return defaultAnswer
           -- implicit coercion of typeClass
           set {theError, theIcon} to {"“" & theResult & "” is not a valid " & typeClass & ". Please try again." & return & return, 2}
           if e = "Overflow error!" then set theError to e & return & theError
       end try
   end repeat
end validateInput

(and eliminated the class list, as AppleScript 2 has only class text)
It seems to work…It is, however, less forgiving as Jon's script. Jon's routine accepts various separators without change (I tried dash, dot, comma) - you can even mix them.

I could not find any info on the year range when entered as yy.
This seems to be the boundary:

Applescript:

   -- 1/1/50 > 1 jan 2050
   -- 2/1/50 > 2 jan 1950

Anything with yy > 50 evaluates as 19yy, and  yy < 50 becomes 20yy.
A leading zero is required with single-digit years, but not month or day; you'll get '0005' when '5' is entered for the year.

Last edited by alastor933 (2012-04-25 06:11:36 am)

Offline

 

#10 2012-04-25 11:52:03 am

Nigel Garvey
Moderator
From: Warwickshire, England
Registered: 2002-11-19
Posts: 3570

Re: how do i do this?

alastor933 wrote:

I found Jon's script when looking for ways to validate a user entered date.
I cannot determine whether it was safe when written, but it has a hole in Snow Leopard (AppleScript 2.1.2):   

Applescript:

set validResult to (run script (("get " & typeClass) as text) & " \"" & theResult & "\"")

This will accept any day up to 31, regardless of the month, and overflow will occur with months with less than 31 days.

Hi, alastor933.

Jon's script would have worked correctly with regard to dates up to and including Leopard. AppleScript dates were seriously compromised in Snow Leopard and I'm told they haven't been fixed in Lion.

Applescript:

"31 february 13"
date result --> date "Sunday 3 March 2013 00:00:00"
-- (Too-high day allowed to overflow into the following month.)

"25 December 1500"
date result --> date "Wednesday 25 December 1500 00:00:00"
tell result to return {its day, its month, its year} --> {4, January, 1501}
-- (AppleScript dates before 15th October 1582 are textually Julian, but their properties are are still proleptic Gregorian.)

The interpretation of two-digits years on my Snow Leopard system switches back to 19xx between 31/12/49 23:59:59 and 1/1/50 00:00:00. On my Leopard system, any two-digit year is interpreted as 20xx. I'd have to reboot my G5 into Tiger to see what happened there, but I know there was yet another balance in the past.


NG

Offline

 

#11 2012-04-25 07:26:15 pm

Adam Bell
Administrator
From: Nova Scotia, Canada
Registered: 2005-10-04
Posts: 4635

Re: how do i do this?

Learned a new word; had to look up proleptic. lol


iMac & MBP both running OS X 10.9.1

Offline

 

#12 2012-04-26 05:08:53 am

alastor933
Member
From: Utrecht, NL
Registered: 2008-09-12
Posts: 458

Re: how do i do this?

Nigel, thanks for the awareness-raising. Not a historian, so not worrying too much about The Black Hole Of Time.

I also looked up 'proleptic'. wink

Offline

 

#13 2012-04-26 05:34:00 am

Nigel Garvey
Moderator
From: Warwickshire, England
Registered: 2002-11-19
Posts: 3570

Re: how do i do this?

Adam Bell wrote:

Learned a new word; had to look up proleptic. lol

alastor933 wrote:

I also looked up 'proleptic'. wink

You can never foresee how a casual word will change people's lives!  lol


NG

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.036 seconds, 8 queries executed ]

RSS (new topics) RSS (active topics)