Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Applescript: Checking a user entered number.

Applescript: Checking a user entered number.
Thread Tools
Addicted to MacNN
Join Date: Nov 1999
Location: Madison, WI
Status: Offline
Reply With Quote
Mar 29, 2003, 03:28 PM
 
I need to check a user entered number with applescript.

I want to check that it is a number applescript already give the error that it can't make the letter into a number but I want to make my own error dialog. And I want to check that the number it is not negative, even though it acts as 0 when used in my application.

Any help would rock!

-Owl
     
Mac Elite
Join Date: May 1999
Location: San Jose, CA
Status: Offline
Reply With Quote
Mar 29, 2003, 08:02 PM
 
Code:
-- assume we don't have a valid entry set gotNum to false repeat until gotNum is true -- ask the user set theDialog to display dialog "Enter a number" default answer "0" --get the text set theInput to text returned of theDialog try -- can it be coerced to a number? set theNum to theInput as number -- is it bigger than 0? if theNum < 0 then -- warn the user the number is negative display dialog "Oops, that was negative" else -- if we get here, we have a positive number set gotNum to true end if on error -- if we get here, there was a problem -- most likely in converting the input to a number display dialog "Oops, that wasn't a number" end try end repeat
The comments cover most of the issues, but the basic principle is that it uses AppleScript's ability to coerce strings into numbers. If it can't because the string contains non-digits, it fails and the 'on error' statement is run.

The code is enclosed in a repeat loop that keeps running until a valid number is obtained.

Change the line 'set theNum to theInput as number" to "... theInput as integer" if you want to ensure you get whole numbers and not real numbers.
Gods don't kill people - people with Gods kill people.
     
OwlBoy  (op)
Addicted to MacNN
Join Date: Nov 1999
Location: Madison, WI
Status: Offline
Reply With Quote
Mar 30, 2003, 01:39 PM
 
Thanks!

Would anyone be able to give me a little commented source of code that would let me save variables to a pref file / txt file sice ASS applications do not save between launches...

-Owl
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 03:17 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2