Tip:

Visual Installer's Scripting Language


 
With Visual Installer you can create quite complex installations without programming, but there may be occasions when a scripting language and script commands can help solve a situation. In the Professional version of Visual Installer there is a flexible scripting language available, and in this tip we will show how to use a few commands in the scripting language. We will show how to copy a file, rename a file and how to use conditional executions of lines with code.
 
 
Where do I enter script commands?
 
There is a script editor available in Visual Installer / Professional. You can open the script editor by choosing the Execute script commands menu item in the Special menu:
 
Special - Execute script commands
 
The script editor look like this:
 
The script editor in Visual Installer
 
'Before installation' and 'After installation'
 
There are two tabs in the script editor: Before installation and After installation. You can enter script lines in both tabs. The difference between them is that the script lines that are entered in the Before installation tab are executed before the actual installation process (before file copying etc.) starts, and the script lines that are entered in the After installation tab are executed after that the actual installation process has finished (all files have been copied etc.). The infograph below shows the process:
 
Infograph: Before installation - Installation process - After installation
 
All script commands in Visual Installer can be used in the both tabs, however, same logic may not always work in both tabs. For example; if you want to copy a file that is included in the setup package you must execute the script in the After installation tab, otherwise there may be no file to copy. Another example; if you want to backup a file before it is overwritten by the installer you should execute your backup script lines in the Before installation tab.
 
Script code example - How to copy a file
 
We will start with something very basic and common: the
COPY command. The COPY command is used to copy a file and this command takes two parameters: a source file path and a destination file path. Below we show how this command can be used to copy a file from C:\folder1 to C:\folder2:
 
COPY C:\folder1\info.txt, C:\folder2\info.txt

In the example above the info.txt file is copied from C:\folder1 to C:\folder2. However, hardcoding folders in this way is normally not so useful. Instead we can use variables for the folder paths. In the next example we will use the %DESTDIR variable. The %DESTDIR variable contains the path to the installation's main destination folder. We change the example to this:
 
COPY %DESTDIR\info.txt, %DESTDIR\backup\info.txt
 
In this example the info.txt file is copied from the main destination folder (%DESTDIR) to a sub folder beneath the main destination folder with the name backup (%DESTDIR\backup). This is an easy way to make a backup of a file.
 
Another example - How to rename a file
 
In the next example we will show how to rename a file by using the
RENAME command. This command takes also two parameters: the old filename and the new filename. The filenames must contain a full file path. The example below shows how to rename a file from newinfo.txt to oldinfo.txt:
 
RENAME %DESTDIR\newinfo.txt, %DESTDIR\oldinfo.txt
 
The file that will be renamed (in our example) must be located in the main destination folder, specified by the
%DESTDIR variable.
 
Conditional execution of script lines
 
Sometimes a specific line or lines with script code should only be executed if a specific condition is met, for example if the setup program is run in a specific version of Windows. In Visual Installer's scripting language you can use conditional execution of script lines to achieve this. For example, if a program only can be run in Windows 7 you can enter a condition like this:
 
IF OS=WIN7
  RUN %DESTDIR\MyWin7App.exe
END IF
 
In the example above the MyWin7App.exe application will only be run in Windows 7. If the operating system, where the installation program currently is running, is something else, for example Windows XP, the MyWin7App.exe application will not be launched. All lines between
IF OS=WIN7 and END IF will be ignored if the operating system is not Windows 7.
 
The RUN command (in the example above) is used to launch a program.
 
More examples
 
In the picture below you can see the script editor with some more command examples.
 
The script editor in Visual Installer - With command examples
 
The following commands are used in the script example above:
 
COPY Copies a file.
XDELETE Deletes a file, also if it is write-protected.
RENAME Renames a file.
UNINSTALL_DELETE Marks a file for deletion at uninstallation.
RUN Runs a program.
   
 
More information
 
More detailed information about the scripting language in Visual Installer, and a full reference with all commands, is available in Visual Installer's User's Guide. See the Script commands - Overview section in the book.
 

< Tips Index Page
   

Go to Visual Installer product page Visual Installer Tip
 
Only Professional Version
SamLogic