AppleScript Handlers/Subroutines

Can the AppleScript statement and/or function contain code with a handler? I’ve tried this with the following, which works in the AppleScript Editor or Script Debugger, but doesn’t work in Pano X.

set getSource to “document.body.innerText”

tell application “Safari”
tell the current tab of its front window
set theSource to do JavaScript (getSource)
my PauseMe()
end tell
end tell

set the clipboard to theSource

on PauseMe()
tell application “Safari”
tell current tab of its front window
repeat while true
do JavaScript “document.readyState”
if the result = “complete” then exit repeat
delay 0.05
end repeat
end tell
end tell
end PauseMe

This example is a bit contrived, but it appears that handlers don’t work in the applescript statement/function. Could this be fixed?

I haven’t tried this with Panorama X, but as I recall from earlier versions of Panorama, it was necessary to put the handler first, before the script that calls it. Try that, and see if it works here.

Unfortunately, that doesn’t work. I also tried enclosing the main code in a “on run … end run” wrapper, but that failed as well.