Re: Apple Events In Cocoa
Re: Apple Events In Cocoa
- Subject: Re: Apple Events In Cocoa
- From: Steve Evangelou <email@hidden>
- Date: Mon, 2 Jul 2001 11:24:09 -0700
John and Brendan,
Unfortunately, the AppleScript documentation is not complete or current,
though work is under way to improve it. Here is some information on the
current state of scripting support and documentation.
For making a Cocoa application scriptable (which is highly recommended),
there is useful, though slightly dated documentation in the Developer
Help Center, in the Cocoa Programming Topics "Application Architecture"
and "Scriptability". This doc is also available online at
<
http://developer.apple.com/techpubs/macosx/Cocoa/CocoaTopics.html>.
Updated documentation generally appears at the website before it is
shipped with Developer Tools.
For examples of scriptable Cocoa applications, see the Sketch and
TextEdit Cocoa sample projects in /Developer/Examples/AppKit.
The following description provides some general overview of how
AppleScript works on Mac OS X, both for Carbon and Cocoa applications:
Scripting on Mac OS X
When a user executes a script, script commands are converted to Apple
events (a form of high-level message) and sent to the appropriate
application. Scripters can write AppleScript scripts to control
scriptable Carbon, Cocoa, or Classic applications. A scriptable
application is one that can respond to Apple events sent by the Mac OS or
by another application. Mac OS X supplies a number of mechanisms that
applications can use to work with Apple events.
The Apple Event Manager provides an API for sending and receiving Apple
events and working with the information they contain. It is implemented
in AE.framework, a subframework of ApplicationServices.framework. The
Open Scripting Architecture provides an API for compiling and executing
scripts, and for creating scripting components, such as AppleScript
itself. It is implemented in OpenScripting.framework, a subframework of
Carbon.framework. While any application can use these frameworks to
support scripting, there are differences in the preferred mechanisms for
creating scriptable Carbon and Cocoa applications.
Carbon applications have traditionally used Apple Event Manager functions
directly to send and respond to events. However, there are several
available aids for simplifying this task:
- MoreOSL is a C library from Apple that provides support for creating
scriptable applications.
- MacApp (from Apple) and PowerPlant (from Metrowerks) are C++ frameworks
that provide powerful object-oriented scripting support.
Though the underlying implementations differ, these same mechanisms are
generally available to applications running in the Classic environment
and in earlier versions of the Mac OS.
Cocoa applications can take advantage of automated scripting support
supplied by the Foundation and AppKit frameworks. This support works with
a scripting definition supplied by the application to convert incoming
Apple events into script command objects that work with application
objects to perform specified operations. This mechanism includes built-in
support for most default AppleScript commands and makes it fairly easy to
provide basic scripting support, such as responding to Apple events by
manipulating objects and data in the application.
Cocoa doesn't currently provide support for directly creating and sending
Apple events, though Cocoa applications are free to use the same C
mechanisms available to Carbon applications.
For tasks such as compiling and executing scripts, Carbon applications
can directly call functions in OpenScripting.framework or use one of the
C++ frameworks mentioned above. Cocoa applications can also use
OpenScripting.framework. There currently is no Cocoa wrapper class to aid
in performing these kinds of tasks.
Hope this helps.
Steve
>
John --
>
>
-[NSAppleEventManager -dispatchRawAppleEvent:...] does not send Apple
>
Events to other applications. Here's an accurate description:
>
>
"Given an event, reply event, and refCon of the sort passed into Apple
>
Event handler functions that can be registered with
>
AEInstallEventHandler(), dispatch the event to a handler that has been
>
registered with -setEventHandler:andSelector:forEventClass:andEventID:.
>
This method is primarily meant for Cocoa's internal use. It does not
>
send events to other applications!"
>
>
Brendan --
>
>
Unfortunately, there's no Cocoa API for sending Apple Events to other
>
applications. The Carbon API for this works fine though.
>
>
-- Mark
>
>
On Thursday, June 21, 2001, at 01:50 AM, cocoa-dev-
>
email@hidden wrote:
>
>
> Brendan,
>
>
>
> I think the method:
>
>
>
> - (OSErr)dispatchRawAppleEvent:(const AppleEvent *)theAppleEvent
>
> withRawReply:(AppleEvent *)theReply handlerRefCon:(UInt32)handlerRefcon
>
>
>
> ...in the NSAppleEventManager class is what you need, but I haven't had
>
> occasion to *send* any AppleEvents, so I don't know for sure. It's not
>
> documented, unfortunately.
>
>
>
> right now, I'm using this class to receive incoming events.
>
>
>
> -jcr
>
>
>
> On Wednesday, June 20, 2001, at 12:57 PM, Brendan Younger wrote:
>
>
>
>> I posted this once before but got no answer.
>
>> Does anyone know how to create apple events in cocoa for
>
>> inter-application communication? I can use NSScriptCommand to create a
>
>> command but I can't find any way to specify the receiver. I know it
>
>> has -setReceiversSpecifier but I don't know how to make a
>
>> NSScriptObjectSpecifier specify another application. Specifically, I
>
>> want to do some of the same things the dock does such as bring an app
>
>> frontmost, bring a specific window frontmost and get a list of an
>
>> application's windows. Please help.
>
>>
>
>> Brendan Younger