2.3 Scripting structure

A NSIS script contains Installer Attributes, Pages and Sections/Functions. You can also use Compiler Commands for compile-time operations. The OutFile instruction is required and tells NSIS where to write the installer, you also need at least one section.

2.3.1 Installer Attributes

Installer Attributes determine the behavior and the look and feel of your installer. With these attributes you can change texts that will be shown during the installation, the number of installation types etc. Most of these commands can only be set and are not changeable during runtime.

Other basic instructions are Name and InstallDir.

For more information about installer attributes, have a look at Installer Attributes.

2.3.2 Pages

A non-silent installer has a set of wizard pages to let the user configure the installer. You can set which pages to display using the Page command (or PageEx for more advanced settings). A typical set of pages looks like this:

  1. Page license
  2. Page components
  3. Page directory
  4. Page instfiles
  5. UninstPage uninstConfirm
  6. UninstPage instfiles

For the installer, this typical set of pages will display a license agreement, allow selection of components to install, allow selection of an installation directory, and finally install the selected components in the instfiles page. For the uninstaller, it will display a confirmation page, and uninstall in the instfiles page.

2.3.3 Sections

It's common for installers to have several things the user can install. For example in the NSIS distribution installer you can choose to install additional tools, plug-ins, examples and more. Each of these components has its own piece of code. If the user selects to install this component then the installer will execute that code. In the script, that code is defined in sections. Each section corresponds to one component on the components page. The section's name is the displayed component name and the section code will be executed if that component is selected. It is possible to build your installer with only one section but if you want to use the components page and let the user choose what to install, you'll have to use more than one section.

Uninstallers can also have multiple sections. Uninstaller section names are prefixed with 'un.'. For example:

  1. Section "Installer Section"
  2. SectionEnd
  3.  
  4. Section "un.Uninstaller Section"
  5. SectionEnd

The instructions that can be used in sections are very different from the installer attributes instructions, they are executed at runtime on the user's computer. Those instructions can extract files, read from and write to the registry, INI files or normal files, create directories, create shortcuts and a lot more. You can find out more in Instructions.

The most basic instructions are SetOutPath which tells the installer where to extract files and File which extracts files.

Example:

  1. Section "My Program"
  2. SetOutPath $INSTDIR
  3. File "My Program.exe"
  4. File "Readme.txt"
  5. SectionEnd

For more information about sections see Sections.

2.3.4 Functions

Functions can contain script code, just like sections. The difference between sections and functions is the way they are called. There are two types of functions, user functions and callback functions.

User functions are called by the user from within sections or other functions using the Call instruction. User functions will not execute unless you call them. After the code in the function has executed the installer will continue executing the instructions that came after the Call instruction, unless you have aborted the installation inside the function. User functions are very useful if you have a set of instructions that need to be executed at several locations in the installers. If you put the code into a function you can save the copying time and you can maintain the code more easily.

Callback functions are called by the installer upon certain defined events such as when the installer starts. Callbacks are optional. If for example you want to welcome the user to your installer you can define a function called .onInit. The NSIS compiler will recognize this function as a callback function by the name and will call it when the installer starts.

  1. Function .onInit
  2. MessageBox MB_YESNO "This will install My Program. Do you wish to continue?" IDYES gogogo
  3. Abort
  4. gogogo:
  5. FunctionEnd

Abort has a special meaning in callback functions. Each callback function has its own meaning for it, have a look at Callback Functions for more information. In the above example Abort tells the installer to stop initializing the installer and quit immediately.

For more information about functions see Functions.

2.3.5 Working with Scripts

2.3.5.1 Logical Code Structures

Conditionally executing code, or executing code in a loop can be done using StrCmp, IntCmp, IfErrors, Goto and more. However, there's a much easier way do this. The LogicLib provides some very simple macros that allow easy construction of complex logical structures. Its syntax, explained in LogicLib.nsh, is similar to other programming languages and can prove to be simpler for beginners and advanced users alike.

For example, checking a value of a variable without the LogicLib can be done as follows.

  1. StrCmp $0 'some value' 0 +3
  2. MessageBox MB_OK '$$0 is some value'
  3. Goto done
  4. StrCmp $0 'some other value' 0 +3
  5. MessageBox MB_OK '$$0 is some other value'
  6. Goto done
  7. # else
  8. MessageBox MB_OK '$$0 is "$0"'
  9. done:

However, with the LogicLib the code is much more readable and easy to understand, as can be seen in the following example.

  1. ${If} $0 == 'some value'
  2. MessageBox MB_OK '$$0 is some value'
  3. ${ElseIf} $0 == 'some other value'
  4. MessageBox MB_OK '$$0 is some other value'
  5. ${Else}
  6. MessageBox MB_OK '$$0 is "$0"'
  7. ${EndIf}

The same can also be done using a switch, as shown in the following example.

  1. ${Switch} $0
  2. ${Case} 'some value'
  3. MessageBox MB_OK '$$0 is some value'
  4. ${Break}
  5. ${Case} 'some other value'
  6. MessageBox MB_OK '$$0 is some other value'
  7. ${Break}
  8. ${Default}
  9. MessageBox MB_OK '$$0 is "$0"'
  10. ${Break}
  11. ${EndSwitch}

Multiple conditions are also supported. The following example will notify the user, if both $0 and $1 are empty.

  1. ${If} $0 == ''
  2. ${AndIf} $1 == ''
  3. MessageBox MB_OK|MB_ICONSTOP 'both are empty!'
  4. ${EndIf}

The LogicLib removes the need for labels and relative jumps, thus prevents label name conflicts, and removes the need to manually adjust relative jump offsets every time the script is changed.

It also simplifies looping by supporting the common while, do and for loops. All of the following examples count to five using the LogicLib.

  1. StrCpy $R1 0
  2. ${While} $R1 < 5
  3. IntOp $R1 $R1 + 1
  4. DetailPrint $R1
  5. ${EndWhile}
  1. ${For} $R1 1 5
  2. DetailPrint $R1
  3. ${Next}
  1. StrCpy $R1 0
  2. ${Do}
  3. IntOp $R1 $R1 + 1
  4. DetailPrint $R1
  5. ${LoopUntil} $R1 >= 5

To use the LogicLib the following line needs to be added near the top of the script.

  1. !include LogicLib.nsh

More examples can be found in LogicLib.nsi.

2.3.5.2 Variables

You can declare your own variables ($VARNAME) with the Var command. Variables are global and can be used in any Section or Function.

Declaring and using a user variable:

  1. Var BLA ;Declare the variable
  2.  
  3. Section bla
  4.  
  5. StrCpy $BLA "123" ;Now you can use the variable $BLA
  6.  
  7. SectionEnd

In addition there is a stack, which can also be used for temporary storage. To access the stack use the commands Push and Pop. Push adds a value to the stack, Pop removes one and sets the variable.

For shared code, there are 20 registers available (like $0 and $R0). These static variables don't have to be declared and you won't get any name conflicts. If you want to use these variables in shared code, store the original values on the stack and restore the original values afterwards.

After calling the function, the variables contain the same value as before. Note the order when using multiple variables (last-in first-out):

  1. Function bla
  2.  
  3. Push $R0
  4. Push $R1
  5.  
  6. ...code...
  7.  
  8. Pop $R1
  9. Pop $R0
  10.  
  11. FunctionEnd

2.3.5.3 Debugging Scripts

The more you work with NSIS the more complex the scripts will become. This will increase the potential of mistakes, especially when dealing with lots of variables. There are a few possibilities to help you debugging the code. To display the contents of variables you should use MessageBoxes or DetailPrint. To get a brief overview about all variables you should use the plug-in DumpState. By default all actions of the Installer are printed out in the Log Window. You can access the log if you right-click in the Log Window and select "Copy Details To Clipboard". There is also a way to write it directly to a file, see here.

2.3.6 Script Execution

When a user runs an installer or uninstaller, pages are displayed in the order they were defined in the script. When the instfiles page is reached, sections, corresponding to the selected components, are executed in the order they were defined in the script. If the components page is not displayed, all sections are executed, assuming they were not unselected or somehow disabled by the script.

Beside code in sections, there's also code in callback functions. If defined, they might be executed before the sections code. For example, the .onInit callback function is executed before anything else in the script. There are also page callback functions which are executed at certain points of the page display process.

2.3.7 Compiler Commands

Compiler commands will be executed at compile time on your computer. They can be used for conditional compilation, to include header files, to execute applications, to change the working directory and more. The most common usage is defines. Defines are compile time constants. You can define your product's version number and use it in your script. For example:

  1. !define VERSION "1.0.3"
  2. Name "My Program ${VERSION}"
  3. OutFile "My Program Installer - ${VERSION}.exe"

For more information about defines see Conditional Compilation.

Another common use is macros. Macros are used to insert code at compile time, depending on defines and using the values of the defines. The macro's commands are inserted at compile time. This allows you to write a general code only once and use it a lot of times but with a few changes. For example:

  1. !macro MyFunc UN
  2. Function ${UN}MyFunc
  3. Call ${UN}DoRegStuff
  4. ReadRegStr $0 HKLM Software\MyProgram key
  5. DetailPrint $0
  6. FunctionEnd
  7. !macroend
  8.  
  9. !insertmacro MyFunc ""
  10. !insertmacro MyFunc "un."

This macro helps you avoid writing the same code for both the installer and the uninstaller. The two !insertmacros insert two functions, one for the installer called MyFunc and one for the uninstaller called un.MyFunc and both do exactly the same thing.

For more information see Compile Time Commands.