Is there an easy way to return every handler of a script? I know I could add all the handler names to a property and return them that way but that is a lot of manual work. What I am looking for is something like:
--pseudo-code:
set the_script to load script "path:to:script" as alias
tell the_script to return name of every handler
AppleScript seems to know the word handler as an item but I can’t figure out a way to make it return anything useful.
“handler” is an AppleScript keyword, one of its classes, such as “application” or “data”, but has not a clear use.
I think you can’t extract handler names from a script programatically, unless you coerce the script to string and search for lines beginning with "on " or "to ". Eg:
returnHandlers without compilation
{result, returnHandlers with compilation}
on returnHandlers with compilation
if compilation then
set scriptHandlers to my returnMyHandlers()
set compiledHandlers to {}
repeat with i in scriptHandlers
set end of compiledHandlers to (run script "my " & i)
end repeat
return compiledHandlers
else
my returnMyHandlers()
end if
end returnHandlers
to returnMyHandlers()
set myCode to me as string
set allHandlers to {}
--> look for handlers in first line
set firstLine to myCode's paragraph 1
set ignoreFirstChunkOn to true
set ignoreFirstChunkTo to true
if firstLine starts with "on " then
set ignoreFirstChunkOn to false
else if firstLine starts with "to " then
set ignoreFirstChunkTo to false
end if
--> extract "on" handlers
set AppleScript's text item delimiters to return & "on "
set onHandlers to myCode's text items
set AppleScript's text item delimiters to {""}
if ignoreFirstChunkOn then
set onHandlers to rest of onHandlers
else
set item 1 of onHandlers to (text 4 thru -1 of (item 1 of onHandlers))
end if
repeat with i in onHandlers
set i to i's first paragraph
set x to ((offset of "(" in i) - 1)
if x = -1 then set x to ((offset of " " in i) - 1)
set end of allHandlers to text 1 thru x of i
end repeat
--> extract "to" handlers
set AppleScript's text item delimiters to return & "to "
set toHandlers to myCode's text items
set AppleScript's text item delimiters to {""}
if ignoreFirstChunkTo then
set toHandlers to rest of toHandlers
else
set item 1 of toHandlers to (text 4 thru -1 of (item 1 of toHandlers))
end if
repeat with i in toHandlers
set i to i's first paragraph
set x to ((offset of "(" in i) - 1)
if x = -1 then set x to ((offset of " " in i) - 1)
set end of allHandlers to text 1 thru x of i
end repeat
return allHandlers
end returnMyHandlers
Of course, this will work only for open-source scripts…
property scriptEditorName : "Script Debugger"
-- or
-- property scriptEditorName: "Script Editor"
set externalScript to (choose file with prompt "Choose file of type \"scpt\"" default location path to scripts folder from user domain) of type "scpt"
set the_script to load script externalScript
tell application scriptEditorName
set document_2 to open externalScript
set handlerNames to name of every script handler of document_2
close document_2 saving no
end tell
return handlerNames