Re: Apple Events in Cocoa
Re: Apple Events in Cocoa
- Subject: Re: Apple Events in Cocoa
- From: Matt Neuburg <email@hidden>
- Date: Thu, 23 May 2002 09:19:32 -0700
On Wed, 22 May 2002 16:31:37 +0100, Duncan Bushell
<email@hidden> said:
>
I want to talk to Microsoft Excel from my own Cocoa app.
>
I can get Excel9s AppleScript interface, classes and events, which I believe
>
tells me the Apple Events Excel supports. How do I convert these classes and
>
events into things I can send programmatically from my own app?
>
So far I9ve resorted to having my code write an AppleScript and run it, but
>
I9m sure there must be a nicer way
The most efficient way to send an Apple event is to send an Apple event (as
opposed to creating an AppleScript and having the system compile this into
its Apple event equivalents and send them). To do so, you would form the
Apple event and send it, yourself.
The easiest way to do this is using AEBuild, which allows you to write an
Apple event textually. Read Technote 2045 for the details.
If you're doing anything of any complexity with AppleScript, you should buy
Script Debugger, from Late Night Software. This program, among other
things, allows you to capture an Apple event in AEBuild format when it is
sent from an AppleScript. Thus, you have a simple way, if you know how to
say something in AppleScript, to learn how to say it in AEBuild format and
hence as a raw Apple event.
I will illustrate by telling the Finder to reveal a file. This is sort of a
silly example, because this is not a complicated Apple event, so the
advantages of using AEBuild will not be evident! But I assure you that
Apple events can quickly become complicated, and that as soon as they do,
AEBuild is a huge time-saver in writing your routines. Plus, since AEBuild
takes an ordinary string, you can form Apple events in a highly dynamic way
(because you can make the string at runtime).
I will use an alias to designate the file because this is the best way.
(Converting a Cocoa pathname to an alias requires some Carbon calls, but
this is nothing terribly difficult.) In Script Debugger, then, we start
with this AppleScript:
tell application "Finder"
reveal alias "xxx:Users:"
end tell
Using Script Debugger's logging window, we capture this in AEBuild format.
Here is the important part:
misc\mvis{----:alis(...)}
We can now write a routine for making the Finder reveal any file:
- (void) revealFile: (NSString*) filepath {
AppleEvent event, reply;
OSErr err;
OSType adrFinder = 'MACS';
FSRef fileRef;
AliasHandle fileAlias;
err = FSPathMakeRef([filepath fileSystemRepresentation], &fileRef,
NULL);
if (err != noErr) return;
err = FSNewAliasMinimal(&fileRef, &fileAlias);
if (err != noErr) return;
err = AEBuildAppleEvent
('misc', 'mvis', typeApplSignature, &adrFinder,
sizeof(adrFinder),
kAutoGenerateReturnID, kAnyTransactionID, &event, NULL,
"'----':alis(@@)", fileAlias);
if (err != noErr) return;
err = AESend(&event, &reply, kAEWaitReply, kAENormalPriority,
kAEDefaultTimeout, NULL, NULL);
AEDisposeDesc(&event);
AEDisposeDesc(&reply);
return;
}
That looks like a big complicated song and dance, but half of it is the
creating of the alias, which in real life would be in a different method.
Plus, after you've done this a couple of times you will realize that the
general template for using AEBuild is always the same - so you can write a
single wrapper function for it and just call it, handing it the necessary
parameters (here these would be 'MACS', 'misc', 'mvis', the AEBuild string,
and the fileAlias). Once you've done this, creating and sending any Apple
event becomes very simple.
Hope this helps - m.
--
matt neuburg, phd = email@hidden,
http://www.tidbits.com/matt
pantes gar anthropoi tou eidenai oregontai phusei
Subscribe to TidBITS! It's free and smart.
http://www.tidbits.com/
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.