Simple script:section with option

From NSIS Wiki
Jump to navigationJump to search
Author: gurzixo (talk, contrib)


Description

My install script needed an user-selectable section, with an user-selectable option. By default, this option is checked or not depending on the value of a registry key. Obviously, if the section is not selected, the option should be grayed out. The action of this section (if selected) is something like: "execwait myprog.exe -myoption" or "execwait myprog" depending if the option is checked or not.

I tried using sections inside sections (does not work), then SectionGroups. But I was not able to to get a consistent checking logic. Many thanks to Red Wine for giving me the solution (cf this post).

As this seems a fairly common need, here is the complete working script:

The Script

!Include "MUI.nsh"
 
!include "Sections.nsh"
!include "logiclib.nsh"
 
!insertmacro MUI_PAGE_COMPONENTS
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_LANGUAGE "English"
 
Name "Section with options"
OutFile "test.exe"
ComponentText "Select the components you want to install."
 
var /GLOBAL WithOption
 
SectionGroup /e "MyProg"
 
Section "" sec1
    ${If} $WithOption == ${SF_SELECTED}
	MessageBox MB_OK "MyProg -MyOption"
    ${Else}
	MessageBox MB_OK "MyProg"
    ${endif}
SectionEnd
 
Section /o "MyOption" sec2
#
SectionEnd
 
SectionGroupEnd
 
Function .onSelChange
SectionGetFlags ${sec1} $R0
IntOp $R0 $R0 & ${SF_SELECTED}
 
${If} $R0 == ${SF_SELECTED}
    !insertmacro ClearSectionFlag ${sec2} ${SF_RO}
${ElseIf} $R0 != ${SF_SELECTED}
    !insertmacro UnSelectSection ${sec2}
    !insertmacro SetSectionFlag ${sec2} ${SF_RO}
${EndIf}
SectionGetFlags ${Sec2} $WithOption
FunctionEnd

Credits

This is not all my code. Most of comes from Red Wine.