AppleScript - syntax error
I just started using AppleScript (like 10 minutes ago really), and encountered a weird syntax error. I was just fooling around and came up with this:
display dialog "Press a button!" buttons{"1","2","3"}
开发者_如何学JAVAif the button_pressed is "1" then
display dialog "You pressed the first button!"
else if the button_pressed is "2" then
display dialog "You pressed the second button!"
else
display dialog "You pressed the last button!"
end if
It doesn't even run. It just spits out a button_pressed is not defined
error when clearly the variable is in my program!
You have to set the variable first. Try this:
display dialog "Press a button!" buttons {"1", "2", "3"}
set button_pressed to button returned of the result
Well, "in the program" doesn't necessarily mean "defined". If a variable is defined, the words set
and to
are surrounding it. I don't see that anywhere in your code. This is easily resovled; just add this line before the if
block and you'll be good to go!
set the button_pressed to the button returned of the result
...or even better...
set the button_pressed to the button returned of (display dialog "Press a button!" buttons{"1","2","3"})
Variables must always be defined before they can be used. The three exceptions are property
s, global
variables, and local
variables (you'll learn about these later if you keep on with AppleScript :) ).
精彩评论