Search code examples
windowsnsis

Getting a path for each component in NSIS


I'm trying to create an installer with NSIS that installs 3 different components to three different paths. I want the users to select/confirm each of them, but they should only be asked if they've selected the relevant component.

Since pages can't appear in sections, I'm at a loss how to do this

Any suggestions?

Thanks in advance :-)


Solution

  • You can use multiple directory pages:

    !include LogicLib.nsh
    
    InstallDir $ProgramFiles32\Foo\Bar
    
    Var Comp1Path
    Var Comp2Path
    
    Page Components
    PageEx Directory
        DirText "Blah blah 1"
        DirVar $Comp1Path
        PageCallbacks Comp1Pre
    PageExEnd
    PageEx Directory
        DirText "Blah blah 2"
        DirVar $Comp2Path
        PageCallbacks Comp2Pre
    PageExEnd
    Page InstFiles
    
    Section /o Comp1 SID_C1
    DetailPrint "Installing Comp1 to $Comp1Path"
    SectionEnd
    
    Section Comp2 SID_C2
    DetailPrint "Installing Comp2 to $Comp2Path"
    SectionEnd
    
    Function Comp1Pre
    StrCpy $Comp1Path $InstDir\Comp1
    ${IfNot} ${SectionIsSelected} ${SID_C1}
        Abort ; Skipping this page
    ${EndIf}
    FunctionEnd
    
    Function Comp2Pre
    StrCpy $Comp2Path $InstDir\Comp2
    ${IfNot} ${SectionIsSelected} ${SID_C2}
        Abort
    ${EndIf}
    FunctionEnd
    
    ; In this example the next button on the components page might be the last page before InstFiles so we have to update the button text
    !include WinMessages.nsh
    Function .onSelChange
    GetDlgItem $1 $hwndParent 1
    ${If} ${SectionIsSelected} ${SID_C1}
    ${OrIf} ${SectionIsSelected} ${SID_C2}
        SendMessage $1 ${WM_SETTEXT} 0 "STR:$(^NextBtn)"
    ${Else}
        SendMessage $1 ${WM_SETTEXT} 0 "STR:$(^InstallBtn)"
    ${EndIf}
    FunctionEnd
    

    Another alternative would be to create a custom page with nsDialogs and just disable or hide the text fields the user does not need to confirm...