Hi Folks,
is it possible to jump in a script file?
f.ex:
something like
if … then goto to …
else if then goto …
end if
Thanks for your reply!
Best regards,
Stefan
Hi Folks,
is it possible to jump in a script file?
f.ex:
something like
if … then goto to …
else if then goto …
end if
Thanks for your reply!
Best regards,
Stefan
Hi Stefan,
no, it isn’t, but there is always a way to work around e.g. with a handler
if x is 1 then
goto1(x)
else if x is 2 then
goto2(x)
end if
but it is no way to go to a certain line of a script (like the goto command in Basic)
Hi Stefan,
thanks - but how can I catch the handler? How can I get it?
Thanks for your help,
Stefan
write it!
Handlers are a great option to extend the language in the way you want.
Another way is to work with flags to skip parts of the code if necessary.
Hi Stefan,
thanks for your hint, but I am not quite sure how the syntax should look like…
get handle_pin()
on handle_pin
end handle_pin
is this correct?
Thanks for your help…
Stefan
Perhaps a better explanation of what you are trying to do would help… but here’s an example
set persons_name to "Jay" -- other value you could set to would be Bob
if persons_name is equal to "Jay" then
welcome_Jay()
else
welcome_Bob()
end if
on welcome_Jay()
display dialog "Hi Jay"
end welcome_Jay
on welcome_Bob()
display dialog "Hi Bob"
end welcome_Bob
But you can pass handlers (think of them as subroutines) other values so a better idea would be to do it like this
set persons_name to "Jay" -- other value you could set to would be Bob
welcome(persons_name)
on welcome(theName)
display dialog "Hi " & theName
end welcome
The nice thing about handlers is they can return values so lets go a tad farther…
set persons_name to "Jay" -- other value you could set to would be Bob
set persons_age to welcome(persons_name)
display dialog "Hi " & persons_name & ", so I hear you are " & persons_age
on welcome(theName)
if theName is "Jay" then
return "28"
else
return "33"
end if
end welcome
Hi James,
thanks a lot for your feedback - it helps a lot…!
Best regards,
Stefan
Nicely done, James.
In addition, MacScripter.net/unScripted has a bunch of articles on Handlers:
By TJ Mahaffey: Script Handlers
and a series of three (parts 1, 2, and 3) by Ben Waldie:
Getting Started with Handlers (Part 1 of a 3-part Tutorial),
Getting Started with Handlers (Part 2 of a 3-part Tutorial),
Getting Started with Handlers (Part 3 of a 3-part Tutorial)
I might add as a later edit, that the use of GoTo in programming languages (particularly the BASIC language) was considered a very bad thing by most computer scientists because it makes the code almost impossible to debug - it is very hard to keep track of where the code has gone. It was, and still is, strongly advised to use functions and subroutines (which in AppleScript are handlers), or to use what were called “Case” statements (in AppleScript, they go as shown below).
repeat
set N to (text returned of (display dialog "Enter a number between 1 and 4" default answer "")) as number
if N > 0 and N < 5 then exit repeat
end repeat
-- Now the cascade of checks (better done with a list, but this shows the structure):
if N = 1 then
set tName to "Adam"
else if N = 2 then
set tName to "James"
else if N = 3 then
set tName to "Stefan"
else
set tName to "Peter"
end if
tName
Thanks Adam =) And have I mentioned how much I despise the actual lack of a case command.