Re: [MACTCL] catching quit from Wish menu
by Jon Guyer other posts by this author
Feb 28 2007 9:06AM messages near this date
Re: [MACTCL] catching quit from Wish menu
|
Re: [MACTCL] catching quit from Wish menu
On Feb 28, 2007, at 10:24 AM, David Zolli wrote:
> Le 27 févr. 07 à 23:15, Jeff Hobbs a écrit :
>
> > ... I've tried "open document" and variations, but even then I
> > don't get args passed in.
>
> When I tried to get filename to edit from Yummy ftp I had the same
> problem. I finaly found this tricky way to get something usable:
>
> package require tclAE
>
> proc MyAeHandler {class id} {
> set filename [string range [tclAE::coerceDesc > [string range $class [ex
pr {[string first \[ $class]
> +1}] > [expr {[string first \] $class]-1}]] TEXT] 1 end-1]
> if {[file exists $filename] && $::ready} {
> after idle [list FileOpen $filename]
> }
> return [tclAE::print $class]
> }
ouch! You should *never* need (or want) to do string manipulations on
TclAE's AE descriptors. Yes, they're strings, because Tcl really,
really, really wants strings, and because it makes it easy to simply
look at it and see what you're getting (handy for debugging and code
development), but you should *always* use TclAE's tools for pulling
them apart and putting them together. The string is simply a handle
to / representation of an internal data structure.
> proc MyAeHandler {class id} {
the arguments are (using Apple's notation) {theAppleEvent theReplyAE}
$theAppleEvent is the incoming AppleEvent record. You're almost
always interested in this.
$theReplyAE is an initially empty AppleEvent descriptor for you to
send back a reply to the caller (if they're expecting one)
> set filename [string range [tclAE::coerceDesc > [string range $class [ex
pr {[string first \[ $class]
> +1}] > [expr {[string first \] $class]-1}]] TEXT] 1 end-1]
In addition to fragile string manipulations, this assumes that the
file is being sent in as an AE list (bracketed by [ and ]), but this
is not always true. You might be sent a list of files, you might be
sent a single file, you might be sent one or more files as alias
records, or as file specifiers, or file urls. Some applications
(BBEdit) accept raw posix paths sent as strings, but this is not
universal. A robust solution should be prepared for any of these
possibilities.
> return [tclAE::print $class]
There's no reason to do this. The result gets sent back to the
caller, bundled as well as possible into $theReplyAE, but the caller
already knows what's in $class (or $theAppleEvent), because they sent
it.
In the case of aevt/odoc, the caller is not expecting any reply at
all. However, if you throw an error, the caller will get back an
AppleEvent error and if your return a reply, they'll get that back as
a string. If you want to reply with something that's not a string,
you'll need to stuff appropriate records into $theReplyAE.
Also, tclAE::print has been a no-op for many years.
> It works but I know I have to do something cleaner when I'll
> understand how tclAE really works.
Here's a solution, adapted from what Alpha does, that should be
reasonably robust (most of it is comments):
proc MyAeHandler {theAppleEvent theReplyAE} {
# extract the direct object, which holds the file(s) we're to open
set pathDesc [::tclAE::getKeyDesc $theAppleEvent ----]
# if we didn't get an AE list, make one
if {[tclAE::getDescType $pathDesc] ne "list"} {
set pathDesc [::tclAE::coerceDesc $pathDesc list]
}
set count [::tclAE::countItems $pathDesc]
set paths [list]
# iterate through the AE list of files and build a list of paths
for {set item 0} {$item < $count} {incr item} {
set fileDesc [::tclAE::getNthDesc $pathDesc $item]
# convert whatever we got into an 'alis' AEDesc object
set alisDesc [::tclAE::coerceDesc $fileDesc alis]
# convert the 'alis' to a path
# better to convert to 'utf8', but a coercion handler isn't
# available for that
lappend paths [::tclAE::getData $alisDesc TEXT]
# a more modern solution might be to convert to 'furl'
rather than
# 'alis', but then the URL needs to be parsed
# a robust solution would provide coercion handlers from all
# possible argument types ('fss ', 'alis', 'furl', 'TEXT',
'utf8', etc.)
# and convert them to a single type that we want to deal with.
#
# the right way is with
with ::tclAE::installCoercionHandler, not by
# adding switches here
}
foreach path $paths {
if {[file exists $path] && $::ready} {
after idle [list FileOpen $path]
}
}
}
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Tcl-mac mailing list
tcl-mac@[...].net
https://lists.sourceforge.net/lists/listinfo/tcl-mac
Thread:
Alastair Davies
Philip Aker
Robert Karen
Jon Guyer
Jeff Hobbs
Jon Guyer
Jeff Hobbs
David Zolli
Jon Guyer
Tim Jones
Jon Guyer
David Zolli
Jon Guyer
Jon Guyer
Jeff Hobbs
Robert Karen
Kevan Hashemi
David Zolli
Kevin Walzer
Jeff Hobbs
Robert Karen
Jon Guyer
Robert Karen
Kevin Walzer
Jon Guyer
Robert Karen
Robert Karen
Tim Jones
Robert Karen
Jon Guyer
Kevin Walzer
Jim Ingham
Jasper Taylor
Clarke
|