CScript
for Pascal
Kevin
Avila, Cache Computing
Sebastiano
Pilla, Macintosh Developer
Copyright © Cache Computing
1998
Pascal version by Sebastiano Pilla
CScript for Pascal (27k binhexed)
CScript is a small library that allows you to execute AppleScript code from directly inside your C/C++ code. For example, lets say you would like to tell the Finder to restart the computer, instead of setting up frustrating Apple Events, simply add this to your C source code:
CScript("tell application \"Finder\" to beep);
CScript will compile and execute this AppleScript code resulting in the Finder going beep!
CScript consists of 3 API calls you need to be aware of in order to operate CScript. These are:
- InitCScript() - You must call this function before using CScript.
- CScript() - This is the main CScript function. See example above on how to use.
- DisposeCScript() - Call this function after you are finished using CScript to release CScript's memory partition.
Comments / Suggestions welcome at support@cache-computing.com.
CScript is a small library that allows you to execute AppleScript code from directly inside your Pascal/Object Pascal code. CScript saves from the pain of creating and maintaining complex code for setting up Apple Events, while keeping a good amount of flexibility.
This Pascal port was done with THINK Pascal 4.5d4.
Contents of the CScript package
- CScript Demo.p THINK Pascal 4.5d4 project for the demo application
- CScript Demo.p.rsrc Resource file for the demo application
- CScript Library.p THINK Pascal 4.5d4 project for the CScript library
- CScript ReadMe This ReadMe file
- CScript.lib Compiled code (68K only) of the CScript library
- CScript.p Source code to the CScript library
- CScriptDemo Compiled CScript demonstration application
- CScriptDemo.p Source code to the CScript demo application
- CScriptIntf.p Interface to the CScript compiled library
- Listing.CScript Demo.p Listing of all files for needed for compiling the demo application
- MovableModal.p Source code for implementing movable modal dialogs
The following instructions are geared towards the THINK Pascal development environment.
NOTE: To use CScript with the MetroWerks CodeWarrior development environment, you
should probably compile the CScript.p source file along with your application instead
of using the compiled CScript.lib library, and adding CScript to your project files'
USES clauses instead of CScriptIntf.
USES CScriptIntf; VAR err: OSStatus; myCScriptRef: CScriptRef; BEGIN err := InitCScript(myCScriptRef);
If the initialization is successful, then InitCScript returns noErr and myCScriptRef
contains a reference to CScript's private data. You must pass this reference to the
other CScript calls; you must not dispose of the returned CScriptRef. For convenience,
you may want to make the reference to CScript a global variable.
VAR
myScriptText: Handle;
err: OSStatus;
BEGIN
... { load a script into myScriptText }
HLock(myScriptText); { lock the text since we'll }
{ definitely move memory }
err := DoCScript( myCScriptRef,
myScriptText^,
GetHandleSize(myScriptText)
);
HUnlock(myScriptText);
VAR err: OSStatus; BEGIN err := DisposeCScript(myCScriptRef);
TYPE CScriptRef = Ptr;
CScriptRef is an opaque reference to CScript's internal private data. The client application obtains a reference to CScript only by calling InitCScript, and must dispose of this reference only by calling DisposeCScript.
FUNCTION InitCScript (VAR outCScriptRef: CScriptRef): OSStatus;Call InitCScript to open a connection to the CScript library, before using any of the other CScript's routines. If the initialization fails then outCScriptRef is NIL and the function result is a non-zero error code.
Returns: outCScriptRef reference to CScript internal data function result error code
FUNCTION DoCScript (inCScriptRef: CScriptRef; inScriptTextPtr: Ptr; inScriptTextLen: SInt32): OSStatus;Call DoCScript to execute an AppleScript, passing a pointer to the AppleScript text and the length of the text (in bytes). If the execution fails then a non-zero error code is returned.
Fields: inCScriptRef reference to CScript data, as obtained from InitCScript inScriptTextPtr pointer the AppleScript text inScriptTextLen length (in bytes) of the AppleScript text Returns: function result error code
FUNCTION DisposeCScript (VAR ioCScriptRef: CScriptRef): OSStatus;Call DisposeCScript to terminate the connection with the CScript library and deallocate CScript's storage.
Fields: ioCScriptRef reference to CScript data, as obtained from InitCScript Returns: ioCScriptRef NIL function result error code