Important: The information in this document is obsolete and should not be used for new development.
Sending Apple Events Without Executing Them
If an application is fully factored, it carries out almost all the tasks a user can perform by sending itself Apple events in the manner illustrated by the listings in the preceding sections. However, in some cases it may not be practical to send an Apple event that actually executes a task performed by the user.For example, if the user drags a window by its title bar from one position to another, it is inefficient to send a series of Apple events that move the window through a series of positions until the user releases the mouse button. Instead, your application can call the Window Manager routine
DragWindow
to allow the user to drag the window to a new position. Until the user releases the mouse button, it's not possible to send a single Apple event that drags the window to the new position, because the new position is not yet known. WhenDragWindow
returns, the window has already been dragged to its new position, and its window record has been updated.At this point your application can send itself the Set Data event that performs the same action; but to avoid repeating the action that was just performed with
DragWindow
, you should add thekAEDontExecute
constant to thesendMode
parameter of theAESend
function when you send the event. The Apple Event Manager then sends the Set Data event to the recording process, if any, but does not send it to the application.Listing 9-5 shows an application-defined routine,
MyDoDragWindow
, that illustrates this approach. TheMyDoDragWindow
routine callsDragWindow
in the usual way, then uses another application-defined routine,MyCreateAESetWindowPos
, and theAESend
function to create and send a Set Data Apple event that sets the window position to the new location. However, because the window has already been moved, there is no need to execute the Set Data event. To send the event for recording purposes without actually executing it, theMyDoDragWindow
routine adds thekAEDontExecute
constant to thesendMode
parameter of theAESend
function when it sends the Set Data event.Listing 9-5 A routine used by a factored application to handle window movement
PROCEDURE MyDoDragWindow (theWindow: WindowPtr; startPt: Point; boundsRect: Rect); VAR newPos: Point; index: Integer; theAppleEvent: AppleEvent; reply: AppleEvent; myErr: OSErr; BEGIN DragWindow(theWindow, startPt, boundsRect); newPos := WindowPeek(theWindow)^.contRgn^^.rgnBBox.topLeft; index := MyIndexFromWndwPtr(theWindow); MyCreateAESetWindowPos(index, newPos, theAppleEvent); myErr := AESend(theAppleEvent, reply, kAENoReply + kAECanInteract + kAEDontExecute, kAENormalPriority, kAEDefaultTimeout, NIL, NIL); END;If recording has been turned on and the user moves a window, the Apple Event Manager automatically sends the scripting component a copy of the Set Data event sent by theMyDoDragWindow
routine but does not send the event to the application. The scripting component records the event as a statement in a compiled script. When a user executes the recorded script, the scripting component sends the same Set Data event to the application. The application's handler for the Set Data event then changes the position of the window.