Important: The information in this document is obsolete and should not be used for new development.
Attaching Scripts
MacApp provides true attachability through its Apple event dispatching mechanism and itsMScriptableObject
class--that is, a script can be attached to an object, and any Apple events specifying that object will first be dispatched to the attached script. The attached script may perform an action and pass the event on to the specified object, it may perform an action and return, or it may ignore the event and pass it on to the specified object.To enable attachability in your application, you must build the application with the
qAttachable
flag set toTRUE
. If you never attach scripts, you can omit MacApp's attachability code. "Common Command-Line Options," beginning on page 678, describes how to build your application with and without attachability code when using MPW's MABuild system.Attaching a Script to an Object
To have an attached script, an object's class must mix in the MScriptableObject class. You attach a script to an object that mixes in MScriptableObject by calling theSetObjectProperty
method and passing two values: aCAEDesc
value that describes the script and aDescType
value that specifies thepScript
property. The MScriptableObject class has a fieldfOSAScript
of typeCOSAScriptCntPtr
. TheSetObjectProperty
method calls theSetOSAScript
method, which sets thefOSAScript
field.In setting the
fOSAScript
field, theSetOSAScript
method notifies the globalTOSADispatcher
object that there is an object with a script attached. When the first script is attached, the dispatcher object calls itsInstallPreDispatchHandler
method (page 152) to set up MacApp's mechanism for predispatching Apple events to attached scripts.When you remove an attached script, the MScriptableObject
::FreeOSAScript
method calls the global dispatcher object'sScriptDetached
method, which removes the predispatch handler if there are no more scripts attached (thefAttachedScripts
count is equal to 0)."Recipe--Attaching a Script to Objects in Your Application," beginning on page 365, provides a simple script and shows how you can attach it to every window in your application. MacApp even saves the script with the document, as described in the following section.
Saving an Attached Script With a Document
TheTFileBasedDocument
class contains methods to read and write an attached script. Reading and writing are controlled by a field of theTDocument
class,fSaveAttachedScript
, which specifies whether an attached script should be saved when the document is written to a file (or read in when the document is read from a file). By default,fSaveAttachedScript
is set toTRUE
.If the
fSaveAttachedScript
field has the valueTRUE
when you write a document object based onTFileBasedDocument
, theDoWrite
method calls theDoWriteScript
method. If there is an attached script, theDoWriteScript
method calls theWriteOSAScript
method, a method ofMScriptableObject
, which writes the attached script to the file stream.As a result of this implementation, if you attach a script to a window in your application, the script will automatically be saved with the window's document. When you open the document, the script will be read in and enabled.
- Note
- When you attach a script to a window by setting the
pScript
property, the window defers to its document, so the script is actually attached to the document.