Re: Capturing current handler name in a string variable
Re: Capturing current handler name in a string variable
- Subject: Re: Capturing current handler name in a string variable
- From: Nigel Garvey <email@hidden>
- Date: Wed, 26 Dec 2001 15:32:30 +0000
billp wrote on Mon, 24 Dec 2001 19:02:05 -0600:
>
Hello.
>
>
I have a lengthy script that does a number of searches and replaces in
>
Word (calling VB). The script relies on probably 60 or more handlers,
>
and the actual order of execution is changeable, as is the use or non-use
>
of a number of handlers, due to a decision tree early in the script.
>
>
I would like to create a new handler that reads a particular paragraph
>
from the document after each major step and writes it to a text file.
>
Let's just say this is paragraph number 355. In addition, above each
>
writing of paragraph 355, I would like to write the name of the handler
>
that was being called at the time. This leads to the question: is it
>
possible in AppleScript to capture the name of a particular handler or
>
object being referenced at a given time and set it as a string to a
>
variable?
It's possible to get a handler name as string from the outside, by
parsing the error message that's generated when you try to coerce the
handler itself. I don't know how this would fit into the structure of
your existing script, but hopefully your new handler could slot into the
end of the process - getting the name of the other handler *and* calling
it. You could pass it the handler to be called (as a handler variable)
and a parameter. (This depends on the possible handlers all having the
same number of parameters and types of return value.) Here's an
illustration of the principle:
on lastStage(h, p)
global theHandler -- must be global to access the handlers
set theHandler to h
try -- attempt to coerce the passed handler to string
theHandler as string
on error msg -- extract the handler name from the error message
set handlerName to text 21 thru -17 of msg
end try
-- Build the file text from the handler's name
-- and the return from the handler itself
set fileText to "Using handler " & handlerName & "(" & p & "): " &
return & return & theHandler(p)
display dialog fileText
-- Create, write to, and close the file
end lastStage
-- Some handlers that might be used
on getPara(n)
"This is paragraph " & n & " of the text."
end getPara
on fred(n)
"Don't call me."
end fred
on marmeduke(n)
"Nor me."
end marmeduke
set handlerList to {fred, getPara, marmeduke}
-- Call the last-stage handler, passing
-- the handler to use and the parameter
lastStage(item 2 of handlerList, 355)
NG