How to use conditional statements
Installation scripts often require different actions depending on the configuration of the user’s computer or its enabled options. In this example we will show how you can execute or skip certain commands using the values of several variables. For simplicity, we will create a window with options, which the user can change, the values of which we will use to accomplish various installation options. You can use any other variables – defined by the user or received from the registry and other sources – in your installations in a similar way. Say we have the following options:
Dialog - Settings
There are three main ways to use conditionals to change the progress of installation:
- Using the parameter If Condition. If this parameter is in a command, which is not empty the command will be executed only if the value of the specified expression is true.
- Using the command If Statement. You can define a combination of conditions for this command, and if they are executed the inner commands will also be executed.
- Using the operator if in the command Source Code.
The If Condition parameter
If this parameter is in a command which is not empty, the command will be executed only if the value of the specified expression is true. You can read about acceptable conditional formats for this parameter in the article How to use If Condition. We have the option Show Welcome Dialog with the variable showwelcome. To show the dialog only when the box is checked it is enough to define If Condition as showwelcome. But the command Dialog – Setup Path has a more complicated condition
!( macrox_getint("usernum") % 2 ) || macrox_get("userstr") %== "setup"
This expression means that the choose path dialog will be shown only if the user entered 0 or an even number, or entered setup into the string field. Or, let’s assume we have two versions – Full and Light. We always need to copy one group of files, but need to copy another group only if the user chooses the Full version. We have the variable userver equal to 1 for the Full version, so it is enough to enter one of the following options in the If Condition field of the additional command Installation Files.
Nuserver == 1 macrox_getint("userver") == 1
You may also notice that in all of the commands Message the parameter If Condition is equal to !skipmsg.
If Statement Command
Not all commands have the If Condition parameter, and it is also sometimes necessary to associate more than one command with a conditional. In this case you can use the command If Statement and enter other commands within it. You can define one or several conditions the fulfillment of which is necessary for the execution of inner commands.
In our project, we develop the checkbox Create uninstall.exe & uninstall.log in the this manner. The create uninstaller command is placed within If Statement and will be executed if the variable isum is not equal to zero (if the box is checked). On the other hand, if the box is not checked it is necessary to disable recording activity into uninstall.log. To do this, we defined the parameter Else, where the name of the command-function to be executed in other cases. The function is located at the beginning of the script. We add the disable recording into uninstall.log command and the display of one message.
Source Code command
More advanced users can use the Gentee programming language if operator in Source Code commands and plug-in scripts. Let’s look at what we have entered into this command in our project.
if *macrox_get("userstr") { $body$ }
As you remember, we have one string parameter in the settings window. It writes the entered value into the variableuserstr. Here, we receive the value of the variable userstr and if it is not empty execute inner commands. As an inner command, we just display a message with the value of the given variable.
As you see, CreateInstall provides enough capabilities for the creation of installations with complicated scripts. You can use the best option for each individual situation.