This section highlights the principles of CorelSCRIPT structure. Here you will learn what script commands, variables and arrays are, how to link to the server application and finally you will write your first simple script for CorelDRAW. Even if you are an experienced programmer, it is recommended that you review all the material in this tutorial because it can contain script specific information that is not known to you yet.
Note again, that this document does not cover all the aspects of Corel SCRIPT language. Refer to the Corel SCRIPT manual to get more detailed and complete information.
A script is a computer program that performs a certain action and is usually used in conjunction with another program to get control over that program. The simplest scripts just repeat user's keystrokes, menu commands and mouse movements. They can be used to automate some routines that the user usually performs by hand. For example, you can create a script that opens a number of documents, adds your comment to each of them and prints them out.
More complex scripts can interact with the user and run with accordance to the user's input using conditions, loops (repeating some operation several times) and so on. The complexity of a script and tasks it can perform only depends on the programmer creating this script.
Corel SCRIPT can run in conjunction with other Corel applications. Such application can execute some procedures that are directed by Corel SCRIPT language. This means you can write an instruction for CorelDRAW to create an ellipse or for Photo-Paint to resample the current document. The applications that can communicate with Corel SCRIPT are called here 'server applications'.
A script program is a text file that contains a list of instructions. Each line of the text contains one instruction. If you want to place more than one instruction per line you should separate them with the colon (:).
These instructions are all part of Corel SCRIPT programming language and can be divided into two categories. The first is based mainly on Corel applications menu commands. For example CopyToClipboard copies the currently selected objects in CorelDRAW to the Windows clipboard (the same action as if you pressed Ctrl-C in Draw) or Undo reverses the last operation (the same as Ctrl-Z). These instructions are application specific. This means that they are defined in one application and not supported by others. For example, the CorelDRAW's CopyToClipboard doesn't work with Photo-Paint. The latter has its own command for this purpose called EditCopy.
The second category of Corel SCRIPT commands are assisting ones that help control the program execution, interact with the user, perform mathematical calculations, etc. These commands are executed by CorelSCRIPT itself and don't depend on the kind of server program used. In fact, it is not necessary to use CorelSCRIPT with some other Corel applications. It can be used as stand-alone program and serve your own purposes. For example, you can write a specialized calculator or measurement unit converter and use it whenever you like.
Note that not every Corel application supports Corel SCRIPT programming. This feature is supported only by the following applications:
You use variables to store some data while a script is running. The variable itself is a data place holder. Its name tells CorelSCRIPT where in computer's memory the data is stored. You can access the variable's value by its name. The name of a variable is a character string containing 1 up to 255 characters. It can contain letters from 'a' to 'z' and 'A' to 'Z', numbers ('0' to '9') and the underscore character ('_'). The variable name is case insensitive, i.e. Name, NAME and name represent a single variable, and must begin with a letter or underscore character. The first character of a variable name can be followed by any combination of letters, numbers or the underscore character but the name cannot contain spaces. The valid variable names are x, Value, s13 , My_Variable. It is recommended to choose meaningful names for your variables as this will help you read your programs in the future. For example if you are to store the rectangle width you could name your variable as Rectangle_width or RectWidth.
Note that a variable name cannot be the same as a Corel SCRIPT command. Refer to the CorelSCRIPT manual for the list of all reserved words.
A variable can store one value at a time. This value can be changed during the script execution but the variable itself and its value are lost after the script terminates. You should use the following syntax to assign and re-assign a value to a variable:
Here Variable is a variable name and value is its value. For example,
You can re-assign the variable value as many times as you like:
x=100 .... x=25 .... x=10000
A variable can hold data of a specific type. It can be numeric, string, Boolean or date. CorelSCRIPT supports nine data types:
|Data type||Value range||Type char|
|String||0 to 4 billion characters (approximate)||$|
|Boolean||TRUE (-1) or FALSE (0)|
|Integer||-32,768 to 32,767 (whole numbers)||%|
|Long||-2,147,483,648 to 2,147,483,647 (whole numbers)||&|
-3.402823·1038 to -1.401298·10-45
1.401298·10-45 to 3.402823·1038
-1.79763913486232·10308 to -4.94065645841247·10-324
4.94065645841247·10-324 to 1.79763913486232·10308
|Date||1 to 2958465 (as a serial
31/12/1899 00:00:00.0000 to 31/12/9999 23:59:59.9999 (as a date value)
|Currency||-922,337,203,685,477.5808 to 922,337,203,685,477.5807||@|
|Variant (numeric type)|
|Variant (string type)|
In order to define which type of data can be assigned to the given variable, the variable should be declared in CorelSCRIPT. The declaration is necessary for CorelSCRIPT to allocate the necessary room in the computer's memory prior to assigning the value to the variable.
The variable declaration can be either implicit or explicit. You can declare a variable implicitly when you assign it a value for the first time. The variable type is figured out depending on the data type that is assigned to it. If you assign a string to a variable, the variable becomes a string variable. Once declared, the type of a variable cannot be changed in the future. The special case is a variant data type which is a non-constant type able to change during the script execution. Please refer to the CorelSCRIPT manual to get more information on the data types.
To declare the variable explicitly you should use DIM keyword. The declaration syntax is as follows:
DIM variable AS data_type
where '?' stands for a data type character mentioned in a table above. Note, that not all data types have the corresponding type character.
Example of explicit variable declaration:
DIM RectWidth AS integer DIM FileName AS string DIM x&
The last statement declares an X variable being of long type.
You can declare more than one variable in a single DIM command. Just separate declarations with commas:
DIM x1%, MyName AS STRING, Counter AS DOUBLE
An array is a set of variables of the same type addressed by the single variable name. Each variable in an array is accessed by its number an index. If you have an array of 10 variables (elements) you can access them by Name(1), Name(2), ..., Name(10), where Name is the array name and the numbers 1 through 10 are the array index.
Each array must be declared explicitly. You can use the same DIM command to declare arrays. After the name of the array being declared you should specify the number of elements it will contain. This number should be in parentheses:
DIM months(12) as string, days%(31)
The example above declares the array months containing 12 elements of the string type and the array days with 31 elements of the integer type.
You can use arithmetic, logical or string expressions to calculate desired value or to derive it from the existing data. The expressions are comprised of operators and their operands. Most useful operators are:
|+||Plus sign. Arithmetic addition or string concatenation (merging).|
|||Minus sign. Arithmetic subtraction.|
|*||Asterisk. Arithmetic multiplication.|
|/||Slash. Arithmetic division. The result is a floating point numeric value.|
|\||Back slash. Integer division. The result is an integer. The fraction is discarded.|
|&||Ampersand. String concatenation.|
|7/4||1.75||The resulting value is a fractional number|
|7\4||1||The result is an integer number|
|"Corel"+"SCRIPT"||"CorelSCRIPT"||The two strings are concatenated (merged)|
|"Hello "&"world!"||"Hello world!"||Acts the same way as '+' for strings|
You can also use variables in your expressions like in the following examples:
Area&=Width*Height Dist#=Spacing*Count+15.256*Index FullName$=FirstName$+" "+LastName$
Use parentheses to specify the order of evaluation:
Corel SCRIPT allows you to perform certain actions in other application by means of OLE Automation. This means that you use the application's program interface to issue commands to it from your script rather than from keyboard, mouse or any other source. You can control the application's state and perform application specific tasks such as opening or creating documents, editing them, printing, and so on. The actions that normally would take you a lot of time to perform manually can be done much faster from the script.
A server application exports a list of methods to allow CorelSCRIPT to control it. Such methods, as usual, are connected with menu commands or keystrokes. For example, FileOpen command in CorelDRAW opens a file and ObjectDuplicate simply duplicates the currently selected object in Corel PHOTO-PAINT.
This methods are supplied by a server application and Corel SCRIPT simply passes them to the application. For this purpose CorelSCRIPT should know what application will receive the external command. To establish a link between CorelSCRIPT and a server application you should use WITHOBJECT...END WITHOBJECT structure. Its syntax is the following:
WITHOBJECT "AutomationID" A set of application-specific commands END WITHOBJECT
Here AutomationID is a string defining a server application as it was registered with Windows. You can use the following identification string for the applications:
|Corel PHOTO-PAINT 6||CorelPhotoPaint.automation.6|
|Corel PHOTO-PAINT 7||CorelPhotoPaint.automation.7|
|Corel PHOTO-PAINT 8||CorelPhotoPaint.automation.8|
|Corel SCRIPT Editor 7||CorelScript.automation.7|
|Corel SCRIPT Editor 8||CorelScript.automation.8|
|Corel VENTURA 7||CorelVentura.automation.7|
|Corel VENTURA 8||CorelVentura.automation.8|
Note that the string must be preceded and followed with a double quotation marks (").
In CorelSCRIPT application specific commands are preceded with a dot.
Withobject "CorelDraw.Automation.8" .FileOpen "C:\Temp\MyDrawing.cdr" END Withobject
This example opens the file C:\Temp\MyDrawing.cdr in CorelDRAW 8.
You use comments to explain a part of your script code or one specific command. This is necessary to read and understand the program easily. Even if you are not intending to give your script to someone else you will find the comments useful because you might forget in the future some ideas you used when you wrote the script.
To add a comment to your script you should use the REM command. Any text to the right of this word is ignored as far as the end of the current line. Or you can use the command's shortcut the apostrophe character ('). If you want to add a comment to the line containing a CorelSCRIPT command, you should use a colon (:) to separate that instruction from the REM command. Note that an apostrophe can be used without any colon.
If the first two line of the script code are comments, they are used by CorelSCRIPT as a script description. This description is displayed when you select a script from the list in Run script command or is shown as a tool tip if you place the script button on a Corel application's toolbar.
REM This is an example of using comments REM This comment starts from the beginning of line 'so does this A%=0 : REM this comment continues the current line Title$="Hello" ' so does this
Comments are for your purposes only and ignored by CorelSCRIPT while executing the script.
Now you are ready to write your own simple script.
WITHOBJECT "CorelDraw.Automation.7" .CreateRectangle 50000, -50000, -50000, 50000, 0 .ApplyUniformFillColor 2, 0, 100, 100, 0 END WITHOBJECT
Note. In the above example, ApplyUniformFillColor command is used in syntax of CorelDRAW 7. In CorelDRAW 8 it is changed. If you are using CorelDRAW 8 use the following code:
WITHOBJECT "CorelDraw.Automation.8" .CreateRectangle 50000, -50000, -50000, 50000, 0 .StoreColor 2, 0, 100, 100, 0, 0, 0, 0 .ApplyUniformFillColor END WITHOBJECT
This script creates a small rectangle in the center of the page and fills it with red.
WITHOBJECT "CorelDraw.Automation.8" Radius&=50000 ' A circle radius CenterX&=0 ' Horizontal circle center offset CenterY&=0 ' Vertical circle center offset .CreateEllipse CenterY+Radius, CenterX-Radius, CenterY-Radius, CenterX+Radius, 0, 0, FALSE END WITHOBJECT
This script declares three variables Radius, CenterX and CenterY as being of the long type (this is shown by & character at the end of the variable names). Then creates a circle of the given radius and its center coordinates in the current open document of CorelDRAW 8.
On the next page you will learn how to control the script execution, about conditional execution and loops.