Re: get handler name [vanilla]
Re: get handler name [vanilla]
- Subject: Re: get handler name [vanilla]
- From: Richard 23 <email@hidden>
- Date: Sat, 24 Mar 2001 10:08:01 -0800
>
Message: 12
>
Date: Thu, 22 Mar 2001 14:02:14 -0800
>
Subject: get handler name
>
From: Brad Giesbrecht <email@hidden>
>
To: AS list <email@hidden>
>
>
Hi,
>
>
Is there a way to get a handler's name?
>
>
<snip>
>
on handlerName()
>
try
>
on error
>
set h to -- code that will return "on handlerName()"
>
end try
>
end handlerName
>
>
>
Thanks,
>
BG
You're in luck, BG. I 've got a vanilla flavoured method for you
and it uses my CoerceToString library (v1.0d17) which has recently
lost quite a bit of weight. I shall be uploading the newest version
to my site later on today.
<
http://homepage.mac.com/richard23/>
But first, here's how to get a handler's name...
-- ---------------------------------------------------------
-- Preprocessed by Convert Script 1.0d5
-- ---------------------------------------------------------
-- author: Richard 23, date: Saturday, March 24, 2001
-- ---------------------------------------------------------
script StigNasty
on ReallyAnnoyingHandlerName()
-- do something really annoying
end ReallyAnnoyingHandlerName
property handler : result
end script
property SomeObj: result
-- ----------------------------------------
-- try to display these in a dialog...
-- ----------------------------------------
display dialog SomeObj
--> Can't make current application into a string.
display dialog SomeObj's handler
--> Can't make +handler ReallyAnnoyingHandlerName; into a string.
-- ----------------------------------------
-- you can when you use CoerceToString...
-- ----------------------------------------
set theList to {SomeObj, SomeObj's handler}
display dialog CoerceToString(theList)
[ {+script StigNasty;, +handler ReallyAnnoyingHandlerName;} ]
get {PlainOsaName(theObj), StyledOsaName(theObj's handler)}
display dialog CoerceToString(result)
[ {"StigNasty", "ReallyAnnoyingHandlerName"} ]
-- ----------------------------------------
-- styled text and plain text varieties
-- ----------------------------------------
on StyledOsaName(theObj)
return word 2 of CoerceToString(theObj)
end StyledOsaName
-- ----------------------------------------
-- handler or script name as plain text
-- ----------------------------------------
on PlainOsaName(theObj)
return last word of CoerceToText(theObj)
end PlainOsaName
--
---------------------------------------------------------------------------
--
-- string coercion made simple! CoerceToString 1.0d17 [r1] (c) 2000-2001
by R23.
--
---------------------------------------------------------------------------
--
on CoerceToString(theValue)
--if my reference /= true then set theValue to DerefValue(theValue)
try
error number -1700 from {return, theValue, return}
on error errTxt number -1700
text 4 thru -4 of errTxt's text from paragraph 2 to paragraph -2
end try
end CoerceToString
--
---------------------------------------------------------------------------
--
-- coercion to unstyled text CoerceToString [r1] 1.0d17 (c) 2000-2001
by R23.
--
---------------------------------------------------------------------------
--
-- NOTE: my system displays kText (custom osax), yours prefers <<class
ktxt>>
--
---------------------------------------------------------------------------
--
on CoerceToText(theValue)
kText of (CoerceToString(theValue) as record)
end CoerceToText
-- ---------------------------------------------------------
I tried to make that semi-practical... Some people probably saw your
question
and said "Look at the script...the handler's name is right there!"
So one more slightly more practical use of the 'handler' class.
How about an error trapping routine that is able to fix the cause of an
error
and then call the handler again...without hardcoding the handler in the
error
trap. Sounds unlikely? I thought so...
-- ---------------------------------------------------------
-- Preprocessed by Convert Script 1.0d5
-- ---------------------------------------------------------
-- author: Richard 23, date: Saturday, March 24, 2001
-- ---------------------------------------------------------
on run
try
foo()
on error errTxt number errNum from errObj
global gRef
if class of errObj = list then
set {gRef, errObj} to errObj
display dialog "Problem in " & StyledOsaName(gRef) & ==>
" with " & errObj with icon caution
set theReply to gRef()
display dialog "Problem solved!" & return & return & ==>
"Result: " & CoerceToString(result) with icon note
theReply
end if
end try
return result
end run
-- ----------------------------------------
-- initially not 'ready'...what to do?
-- ----------------------------------------
on foo()
global ready
try
if ready then
return {"done!", ready}
end if
on error errTxt number errNum from errObj
error errTxt number errNum from {foo, errObj}
end try
end foo
-- ----------------------------------------
-- create and set global or set property
-- ----------------------------------------
on SetGlobal(theLabel, theValue)
tell (run script "me
on f(theObj, theValue)
global " & errObj & "
set " & errObj & " to theValue
end f") to f(me, true)
end SetGlobal
-- ---------------------------------------------------------
NOTE: Unfortunately while it is possible to get at the current
script object regardless of whose handler its executing (using
me or it, not always the same thing) there doesn't seem to be
a way to get at the current handler... That's why I used the
offending object parameter as a temporary stack. I'd like it
better if I didn't have to trap the error in the handler at all.
One error trap would be preferable.
R23