I know there is countless posts on quitting a subroutine and handler, but I am not having any luck with this one. I even tried throwing error -128 with no success, but on a brighter note I did learn other things in the process of my failures.
It is the center line that is the issue:
if button returned of userResponse is “Cancel” then return
on myLoopLoop(myElement)
tell application "Adobe InDesign CS2"
tell active document
if (name of markup tag of myElement is "expirationdate") then
set userResponse to display dialog "There are already expiration date tags. Do you want to remove all existing tags and tag with this new date?" buttons {"OK", "Cancel 2"} default button {"OK"} with icon 2
if button returned of userResponse is "OK" then
my ExpDateTags()
else
if button returned of userResponse is "Cancel" then return
end if
end if
try
set moreElement to every XML element of myElement
repeat with x from 1 to (count of moreElement)
tell me to myLoopLoop(item x of moreElement)
end repeat
end try
end tell
end tell
end myLoopLoop
display dialog will throw error number -128 (“User canceled”) when a button named “Cancel” (or a cancel button) is pressed:
try
display dialog ""
display dialog (button returned of result) buttons {"OK"} default button 1
end try
Try something like this:
on myLoopLoop(myElement)
tell application "Adobe InDesign CS2"
tell active document
if (name of markup tag of myElement is "expirationdate") then
try
display dialog "There are already expiration date tags. Do you want to remove all existing tags and tag with this new date?" with icon 2
on error errMsg number errNum
if errNum is -128 then
return -- User canceled
else
error errMsg number errNum
end
end
my ExpDateTags()
end if
try
set moreElement to every XML element of myElement
repeat with x from 1 to (count of moreElement)
tell me to myLoopLoop(item x of moreElement)
end repeat
end try
end tell
end tell
end myLoopLoop
If you want to jump right out of the myLoopLoop() recursion, use Bruce’s script, but put the ‘try’ block round the original call to the handler instead, at the point to which you want to return. The “Cancel” error will then be caught there instead of inside the recursion:
on myLoopLoop(myElement)
tell application "Adobe InDesign CS2"
tell active document
if (name of markup tag of myElement is "expirationdate") then
display dialog "There are already expiration date tags. Do you want to remove all existing tags and tag with this new date?" with icon 2
my ExpDateTags()
end if
try
set moreElement to every XML element of myElement
repeat with x from 1 to (count of moreElement)
tell me to myLoopLoop(item x of moreElement)
end repeat
end try
end tell
end tell
end myLoopLoop
-- Blah blah.
try
myLoopLoop(whatever) -- The top call to the handler.
on error number -128
-- Recover from "User Canceled" but not other errors.
end try
-- Continue from here.
Thank you Bruce and Nigel,
This will work, and it is great to have on record to use again and again.
But what it really boiled down to after all, is my code was a MESS! I had to fix so much more. It’s annoying when I think I have something right then all of sudden I am plugin away and a script error pops up. Oh well, it’s a good way to learn.