CScript for Pascal
Kevin Avila, Cache Computing
Sebastiano Pilla, Macintosh Developer



CScript for Pascal

Copyright © Cache Computing 1998
Pascal version by Sebastiano Pilla

Download Package

CScript for Pascal (27k binhexed)


The Original CScript ReadMe

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:


Comments / Suggestions welcome at support@cache-computing.com.


What it is

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


Example of use

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.

  1. Since CScript can be used only from an Apple Events-aware application, set the Apple Event-aware flag in the 'SIZE' resource of your project's resource file.

  2. Add CScript.lib and CScriptIntf.p to your project.

  3. You must initialize CScript before using it. To initialize CScript, call

    			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.

  4. To execute an AppleScript, load its text and pass it to the DoCScript function
    			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);
  5. When finished with CScript, call DisposeCScript, passing it the CScriptRef previously obtained by InitCScript
    			VAR
    				err: OSStatus;
    			
    			BEGIN
    				err := DisposeCScript(myCScriptRef);


CScript API Reference

CScript Data Structures

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.




CScript Routines

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


Copyright © Cache Computing 1998

Web page created and edited by Bill Catambay. Provided with permission from Cache Computing and Sebastiano Pilla.