[Pythonmac-SIG] Need help with AppleEvent bug

Jack Jansen Jack.Jansen@oratrix.com
Thu, 12 Sep 2002 22:49:43 +0200


On donderdag, september 12, 2002, at 03:34 , bill fancher wrote:
> CodeWarrior is like TextEdit (and other Cocoa Apps) used to be: 
> a document has text elements. According to the CodeWarrior 
> dictionary, you can only refer to a range of them. AppleScript 
> hacks^H^H^H^H^H works around this somehow (I suspect the same 
> way it did with TextEdit). It appears that you can also refer 
> to the text elements by number. It further appears that the one 
> of interest is the first one. So...
>
> The following works here, assuming CodeWarrior is running and 
> has an open document (Mac OS X 10.2, Python 2.3, CW IDE 4.0.4):
>
> import CodeWarrior
>
> cw = CodeWarrior.CodeWarrior()
> txt = CodeWarrior.editor_document(1).text(1)
> print cw.get(txt)

Bill, many thanks for the explanation! I've close the bug report 
with "Won't fix" status. If anyone happens to know how 
AppleScript does the workaround (and if it isn't too hairy to 
implement in Python): please reopen the bug and annotate it.

That final two lines of your solution did however spark 
something that's been in the back of my mind since Day One of 
the Applescript modules. What you would *really* want to say in 
Python, I think, is
 >>> print cw.editor_document(1).text(1).get()

And: I think that by now we have almost all the machinery in 
place to do so. The "cw" object now knows the name of the module 
it was imported from. So, it could have a __getattr__ method 
(probably inherited from aetools.TalkTo) that does a lookup of 
"editor_document" in it's module and returns an object that, 
when called, would instantiate CodeWarrior.editor_document with 
an added "fr" argument (pointing to the cw instance itself). The 
intermediate steps will then all work fine, the only problem is 
the final "get" lookup. But here the ObjectSpecifier class could 
also have an __getattr__ method, and this would percolate up the 
self.fr chain until it gets at the application object, at which 
point it would have the module name, lookup "get" and return a 
similar callable object.

Anyone willing to give this a try? Leave the patch in 
sourceforge and I'll apply it when I get back from holidays, in 
3 weeks time:-)
--
- Jack Jansen        <Jack.Jansen@oratrix.com>        
http://www.cwi.nl/~jack -
- If I can't dance I don't want to be part of your revolution -- 
Emma Goldman -