Step 5: Working with Scripts

InstallShield 2015 » InstallScript Project Tutorial

InstallShield uses the InstallScript language to drive an installation. You can modify the project’s script in the InstallScript view.

Tip: Press CTRL+M to maximize the Script Editor; press CTRL+M again to restore it.

InstallScript project scripts use an event-driven model, where a series of predefined functions are called in a specific order. For information about the built-in categories of event handlers and the order in which event handler functions are called, see Event Handlers.

The functions already defined in your script appear in the Functions tree. To view or edit an existing function, click its name in the Functions tree.

Note: Event handler functions are called even if they do not explicitly appear in your script. If an event handler function does not appear in your script, its default code is used.

To add and edit an event handler function, select the desired event category (such as Before Move Data) from the script editor’s left drop-down list, and then select the name of the desired event (such as Begin) from the right drop-down list. Event handler functions that are already explicitly defined in your script appear in boldface text.

For example, the OnBegin event handler is the first function called in an installation script, for both a first-time installation and maintenance mode.

To add your own code to the OnBegin event handler, begin by creating the function:

1. Select Before Move Data from the event category list on the left.
2. Select Begin from the event handler list on the right.

The following code is added to your script:

//////////////////////////////////////////////////////////////////////////////

//

//  FUNCTION: OnBegin

//

//  EVENT: Begin event is always sent as the first event during

//  installation.

//

//////////////////////////////////////////////////////////////////////////////

function OnBegin( )

begin

    // TO DO: you may change default non-UI settings, for example

    //

    // You may also perform your custom initialization steps, check requirements, etc.

end;

You can place any code you want to execute at the beginning of your installation in the OnBegin function.

Using the Function Wizard

This example demonstrates how to use the Function Wizard to add a call to the MessageBox function. This displays a message when the user begins the installation.

To add the MessageBox function to your OnBegin function:

1. Delete the comments (lines beginning with //) between the lines reading begin and end;, and place the text insertion point between the lines.
2. Press CTRL+I to launch the Function Wizard.
3. In the Function Category list, select Built-in dialog.
4. In the Function Name list, select MessageBox.
5. Click Next.
6. In the szMsg field—which contains the message you want to display—type "Welcome to the Tutorial installation!" (including the quotation marks).
7. In the nType drop-down list—which specifies the type of message box to display—select INFORMATION.
8. Click Finish to paste your function call into the script.

Your OnBegin function should now appear as follows:

function OnBegin( )

begin

MessageBox ( "Welcome to the Tutorial installation!" , INFORMATION );

end;

Compiling the Script

After modifying the script, you must compile the script for the changes to take effect. Compile the script by clicking the Compile toolbar button, choosing Compile from the Build menu, or pressing CTRL+F7. Status messages are displayed during compilation in the output window of the IDE.

Tip: If compiling the script results in any errors or warnings, you can double-click the error message to highlight the script line where the error occurred.

When you run the installation (by clicking the Run toolbar button, or by pressing CTRL+F5), the message box appears before the Welcome dialog is displayed.

Displaying the Message only for First-Time Installation

The OnBegin function is called for both a first-time installation and a maintenance-mode installation, which means the message box will be displayed when the user reinstalls or removes the program. To display the message box only for a first-time installation, you can place your MessageBox call inside an if statement that determines if the user is running the installation for the first time.

InstallScript defines a system variable called MAINTENANCE, which is true if the installation is running in maintenance mode and false otherwise. To display your message box only for a first-time installation, change the OnBegin function to read as follows:

function OnBegin( )

begin

if (!MAINTENANCE) then

    MessageBox("Welcome to the Tutorial installation!", INFORMATION);

endif;

end;

After you compile and run the installation, the message box appears only for a first-time installation, and not for maintenance mode.

InstallScript

The InstallScript language contains hundreds of functions for performing installation-related tasks, such as working with the registry and INI files, testing characteristics of the target operating system, and displaying dialogs. For details and examples, see the InstallScript Language Reference.

The next step of the tutorial explains how to modify the dialogs that are displayed by your installation.