[Pythonmac-SIG] How do I send appleevents to an external program

Joachim Mårtensson joachimm at mac.se
Sun Jan 30 03:51:05 CET 2005


Bob Ippolito wrote:

>
> On Jan 29, 2005, at 19:40, Joachim Mårtensson wrote:
>
>> Hello everyone.
>> I am trying to figure out how to send appleevents to an external  
>> program. What I want to accomplish is to open a text document in a  
>> editor that supports the external editor protocol as described here  
>> http://www.codingmonkeys.de/techpubs/externaleditor/ 
>> pbxexternaleditor.html.
>> Here is my code so far, What am I doing wrong? _filePath is the path  
>> to the file and _lnnum is the line number where I want to place the  
>> caret. I am using PyObjc but no api in cocoa seems to exist so I use  
>> various other modules.
>
>
> PyObjC can use AppleScript with the NSAppleScript class  
> <http://developer.apple.com/documentation/Cocoa/Reference/Foundation/ 
> ObjC_classic/Classes/NSAppleScript.html>.  However, the best way to  
> send Apple Events from Python is probably appscript  
> <http://freespace.virgin.net/hamish.sanderson/appscript.html>.  The  
> standard library support for Apple Events is very... raw, so I 
> wouldn't  recommend that approach to anyone :)
>
> -bob

Ok thanks, I downloaded the aem module from the page you linked. 
Everything works fine except for the placing of the caret.
I have tried various ways to get this c-structure to work.
struct SelectionRange
{
 short unused1; // 0 (not used)
 short lineNum; // line to select (<0 to specify range)
 long startRange; // start of selection range (if line < 0)
 long endRange; // end of selection range (if line < 0)
 long unused2; // 0 (not used)
 long theDate; // modification date/time
};

This is what I have come up with so far. What is the proper way of 
passing the struct?

def openInExternalEditor(self, _filePath, _lnnum):
   from aem.send import Application
   import struct
   from Carbon.File import FSSpec
   SelectionRange=struct.pack('hhllll', 0, int(_lnnum), 0,0,0,0)
   
Application('/Applications/TextMate.app').event('aevt','odoc',{'----':FSSpec(_filePath),'sopk':`SelectionRange`}).send()



More information about the Pythonmac-SIG mailing list