Re: Script Debugger: select contents of a handler by name
Re: Script Debugger: select contents of a handler by name
- Subject: Re: Script Debugger: select contents of a handler by name
- From: Paul Berkowitz <email@hidden>
- Date: Sat, 29 Dec 2001 15:28:02 -0800
On 12/29/01 12:33 PM, "Jason Bourque" <email@hidden> wrote:
>
Hello,
>
>
This is what I have but it doen't work.
>
>
I want to select contents of a handler by name.
>
>
>
>
Any ideas.
>
>
tell application "Script Debugger"
>
tell document 2
>
set selection to text of contents of script handler "Handler2Test"
>
end tell
>
End tell
>
>
Yes. You mustn't confuse properties of scripts (e.g. 'script handler') with
properties of documents.
1) The 'contents of script handler' is
<<script handler>>, not the actual text in the window.
2) If you ask for 'name of every script handler of document 2', you'll see
that SD seems to give the name all in lowercase. and is case sensitive:
"handler2test"
and you have to refer to it that way:
contents of script handler "handler2test" of document 2
the result, however , looks like this:
<<script handler "Handler2Test">>
with the upper-case letters back. (Throughout << >> represent chevrons.)
3) The way to select existing text in any app implementing the text suite is
with the command
select
not 'set selection to'. 'set selection to' inserts new text at the insertion
point. You'll find 'select' in Miscellaneous in the SD dictionary.
4) The missing link, for me, is that I don't know how to get the source text
of a script object. So even though I can get <<script handler
"Handler2Test">>, I can't get its text. Even if I could, it would not
include the "on" or "to" line and end line. Maybe someone else can help
here. I'm pretty sure if you wrote to the SD mailing list, Mark would give
the answer: I'm pretty sure there must be one. His own routines select the
"on " line when you click the blue popup to select a handler.
5) I found a workaround, when I discovered that
set selection to <<script handler "Handler2Test">>
actually inserted the text of the handler! But without the opening and
ending lines, and therefore missing one level of indentation throughout
every line. So you'd never find an accurate offset this way. For one thing,
you won't necessarily know whether "on" or "to" is used, nor every parameter
in the "on" line. Fortunately (here), AppleScript does not refuse to compile
undefined variables (the parameters), so you can temporarily open another
window, insert the handler, re-paste it with an "on handler2test()" line (no
parameters) and an "end" line , compile it, do some tricky text
manipulation, close the window without saving, get the offset in the main
script (lower-case name doesn't matter), and select. (Non-indented lines
here are line continuations):
set handlerName to "handler2test"
set theScript to document 2
set theHandler to contents of script handler handlerName of theScript
set newDoc to make new script document
set selection of newDoc to theHandler
set handlerText to (contents of newDoc) as string
set handlerText to "on " & handlerName & "()" & return & handlerText &
return & "end"
set contents of newDoc to handlerText
compile newDoc
set compHandlerText to contents of newDoc as string -- compiled text
with indentations
close newDoc saving no
set compHandlerText to text (paragraph 2 of compHandlerText) thru
(paragraph -2 of compHandlerText) of compHandlerText -- omit "on" or "to"
line, wrong
set handlerLength to count compHandlerText
set scriptText to (contents of theScript) as string
set theStart to offset of compHandlerText in scriptText -- from indent
set theEnd to theStart + handlerLength
set preHandler to text 1 thru (theStart - 2) of scriptText -- omit
return
set prelimLine to (paragraph -1 of preHandler)
set prelimLength to (count prelimLine)
set theStart to theStart - 1 - prelimLength -- -1 for return
set postHandler to text (theEnd + 1) thru -1 of scriptText
set postLine to paragraph 1 of postHandler
set postLineLength to (count postLine)
set theEnd to theEnd + 1 + postLineLength
select text theStart thru theEnd of theScript
This works, although you see that new window pop up briefly. it would be
nice if someone here knows how to get a 'source text'-type property of a
script object. I bet you there's a raw code that will do it.
--
Paul Berkowitz