Trying to learn by working through AppleScript 1-2-3–Soghoian and Cheeseman. Get this error with both of these scripts when trying to compile (page 300). OS 10.5.8, Script Editor 2.2.1 (Tried on another machine-same result}
Syntax Error
Expected end of line, etc. but found “if”
What am I doing wrong. Thanks in advance.
Sam
Script #1
repeat
display dialog "Enter a whole number: " default answer ""
try
set this_text to the text returned of the result
if this_text is not "" then set this_number to this_text as number
if this_number mod 1 is equal to 0 then set this_number to this_number div 1
exit repeat
end if
on error
beep
end try
end repeat
Script #2
repeat
display dialog "Enter a number" default answer ""
try
if the text returned of the result is not "" then set the requested_number to the text returned or the result as number
exit repeat
end if
on error
beep
end try
end repeat
Model: MacBook Pro
AppleScript: 2.0.1
Browser: Firefox 3.6.3
Operating System: Mac OS X (10.5)
repeat
display dialog "Enter a number" default answer ""
try
if the text returned of the result is not "" then set the requested_number to the text returned of the result as number
exit repeat
on error
beep
end try
end repeat
… then set the requested_number to the text returned of the result as number …
In your script above it says or which actually doesn’t make sense.
You might find some of these tutorials useful as well as you work your way along. Enjoy!
Thanks Adam. That was a typo on my part in Script #2. However Script #1 was entered correctly from the text book and it doesn’t work correctly with the “end if” removed. It allows the user to enter a fractional number (non integer) and still complete the script. These are just exampIe scripts for the display dialog command. I figured out how to get the desired result. Now I can move on the the next Chapter. At least I got a lesson on “simple if” versus “if, end if”. I have fooled around with Applescript for over 10 years, but this is the first time I have gotten serious about learning it. Only 540 more pages to go!
I think it likely there’s meant to be a line break after the first ‘then’ in each script. Everything between there and the ‘end if’ is dependent on the dialog returning something other than “”:
repeat
display dialog "Enter a whole number: " default answer ""
try
set this_text to the text returned of the result
if this_text is not "" then
set this_number to this_text as number
if this_number mod 1 is equal to 0 then set this_number to this_number div 1
exit repeat
end if
on error
beep
end try
end repeat
repeat
display dialog "Enter a number" default answer ""
try
if the text returned of the result is not "" then
set the requested_number to the text returned of the result as number
exit repeat
end if
on error
beep
end try
end repeat
Or, since the first script is waiting for a whole number, it probably needs two ‘end ifs’. (‘ends if’?)
repeat
display dialog "Enter a whole number: " default answer ""
try
set this_text to the text returned of the result
if this_text is not "" then
set this_number to this_text as number
if this_number mod 1 is equal to 0 then
set this_number to this_number div 1
exit repeat
end if
end if
on error
beep
end try
end repeat
Nigel, thank you, thank you, thank you. You are exactly correct. The text book does have a return after “then” in both scripts. You and the text book are correct and the student user (me) needs more practice in Applescript syntax. My apologies to the text book authors.
on requestnumber()
repeat
set o to text returned of (display dialog "Enter a whole number:" default answer "")
try
if o is not "" then
set thenumb to o as integer
if thenumb mod 1 is equal to 0 then
set thenumb to thenumb div 1
exit repeat
end if
end if
on error
beep
end try
end repeat
return thenumb
end requestnumber
requestnumber()