This code evinces several misunderstandings.
-
display dialog returns a record that contains properties with information about the button that was used to exit the dialog, the text entered (if a default answer is supplied), and whether the dialog timed out (if giving up after is specified).
text returned of startNumber would yield the string value that the user entered.
- The is operator is comparing a record object (the result from display dialog) to the integer type object. This will always be false. Even if the object being compared was an integer, the expression would still always yield a false value. 5 is an integer (a member of the type integer), but it is not the same as the abstract idea of integers (which the integer type object represents).
The expression class of startNumber is integer would work as expected, if startNumber had any chance of being an integer (it is always a record in the original script).
- You have to specify that you want to convert to an integer the string value that is returned in the record result of display dialog.
(text returned of startNumber) as integer invokes AppleScript’s built-in string to integer coercion. It throws an error if it is unable to convert it to an integer (could not be parsed as an integer, too big to fit in an AppleScript integer object, maybe other reasons).
With probably more error checking than is necessary, here is some code that I think will do what you want:
repeat
set dialogResult to (display dialog "Enter a starting number:" default answer "")
try
set startNumber to (text returned of dialogResult) as integer
-- This test should be unnecessary, just exiting the loop should be fine.
if class of startNumber is integer then ¬
exit repeat
on error m number n from o partial result r to t
set unableToConvertToInteger to n is -1700 and o is text returned of dialogResult and t is integer -- e.g. "asdf"
set numericResultTooLarge to n is -2702 and t is integer -- e.g. anything larger than 2^29-1
if not (unableToConvertToInteger or numericResultTooLarge) then error m number n from o partial result r to t -- return {m, n, o, r, t} -- to see the error values, use the return instead
end try
display dialog "The starting number needs to be a valid integer!" buttons {"Enter again", "Cancel"} default button 1
end repeat
startNumber
Without all the error checking, it could look something like this:
repeat
set dialogResult to (display dialog "Enter a starting number:" default answer "")
try
set startNumber to (text returned of dialogResult) as integer
exit repeat
end try
display dialog "The starting number needs to be a valid integer!" buttons {"Enter again", "Cancel"} default button 1
end repeat
startNumber
Note that this as integer method for converting a string into an integer means that (as examples) “2.5” yields 2, “3.5” yields 4 (rounds “real” values to the nearest even integer), and “2.3e5” yields 230000 (scientific notation). If these results are undesirable you will have check that the string contains only digit characters before trying to coerce it (Martin Michel’s code does this).
Edit: Swapped return and error (previously commented out) in the error handling script.