Hi,
Is there any way to jump back to an earlier point within a subroutine? Or is it only done by placing the place you want to jump back to into a separate subroutine?
Hi,
Is there any way to jump back to an earlier point within a subroutine? Or is it only done by placing the place you want to jump back to into a separate subroutine?
Hi Patrick,
no, it isn’t. But I think, there is always a way to work around it
You might try making the section you may want repeated into an independant handler all by itself, so you can call it anytime you wish, from anywhere in your script.
Yeah… I ended up doing this… Sometimes it just feels like it would be more efficient if I could just jump back within a subroutine though! Oh well…
Thanks for the replies.
Hi guys.
For a simple goto, a repeat loop can be useful:
say 10
say 20
set goto_30 to true
repeat 2 times
say 30
say 40
end repeat
say 50
say 60
To add conditionality:
say 10
say 20
repeat
say 30
say 40
if button returned of (display dialog "Go to 30 - or continue?" buttons ¬
{"Cancel", "Go to 30", "Continue."} default button 3) is "Continue." then exit repeat
end repeat
say 50
say 60
Adding further control options will, of course, increase the complexity:
say 10
say 20
repeat
say 30
say 40
repeat
say 50
say 60
repeat
say 70
say 80
set goto to choose from list {"goto 30", "goto 50", "goto 70", ¬
"goto 90", "goto 110"} with prompt "Go to where?"
if goto is false then error number -128
set goto to last word of goto's end as integer
if goto is not 70 then exit repeat
end repeat
if goto < 50 or goto > 90 then
exit repeat
else if goto > 50 then
say 90
say 100
exit repeat
end if
end repeat
if goto > 110 then
exit repeat
else if goto > 30 then
say 110
say 120
exit repeat
end if
end repeat
Compare that with the greater logical clarity of cascading handlers:
say 10
say 20
goto_30()
on goto_30()
say 30
say 40
goto_50()
end goto_30
on goto_50()
say 50
say 60
goto_70()
end goto_50
on goto_70()
say 70
say 80
choose_where_to_go()
end goto_70
on goto_90()
say 90
say 100
goto_110()
end goto_90
on goto_110()
say 110
say 120
end goto_110
to choose_where_to_go()
set goto to choose from list {"goto 30", "goto 50", "goto 70", ¬
"goto 90", "goto 110"} with prompt "Go to where?"
if goto is false then error number -128
set goto to last word of goto's end
if goto is "30" then
goto_30()
else if goto is "50" then
goto_50()
else if goto is "70" then
goto_70()
else if goto is "90" then
goto_90()
else -- goto is "110"
goto_110()
end if
end choose_where_to_go