msd_2
msd_2

Reputation: 1187

Ask user for additional directory location in NSIS

I have a NSIS script which asks user for Installation direcory, but I want to ask user for one more temp directory on a new page. Is there a way by which I can add a new page using nsDialogs which specifies a temp directory for eg

C:\temp

and also lets them choose a different directory and then store the value of chosen directory in a variable

Upvotes: 4

Views: 4483

Answers (3)

bk138
bk138

Reputation: 3088

While Seki's answer is very nice in that there's a lot of code re-use and less boilerplate, I needed something that was not showing the "Space required:","Space needed:" widgets (like MUI_PAGE_DIRECTORY does). Here's the custom MUI2 page I've come up with:

;log dir variable and its default 
Var logDir
!define defaultLogDir "C:\Logs"

;three functions for the custom page
Function pageGetLogDir
   !insertmacro MUI_HEADER_TEXT "Choose Log File Destination" "Choose the folder in which to write the log files."

   nsDialogs::Create 1018
   Pop $0
   ${If} $0 == error
     Abort
   ${EndIf}

   var /global browseButton
   var /global directoryText

   StrCpy $logDir "${defaultLogDir}"

   ${NSD_CreateLabel} 0 0 100% 36u "Log files will be written to the following folder. To have them written to a different folder, click Browse and select another folder. Click Next to continue."
   Pop $0 ; ignore

   ${NSD_CreateText} 0 37u 75% 12u "$logDir"
   pop $directoryText
   ${NSD_OnChange} $directoryText onLogDirTextChange

   ;create button, save handle and connect to function
   ${NSD_CreateBrowseButton} 80% 36u 20% 14u "Browse..."
   pop $browseButton
   ${NSD_OnClick} $browseButton onLogDirButtonClick

   nsDialogs::Show
FunctionEnd

Function onLogDirButtonClick
  nsDialogs::SelectFolderDialog "Select Log File Destination" ""
  Pop $0
  ${If} $0 != error
    StrCpy $logDir $0
    ${NSD_SetText} $directoryText $logDir
  ${EndIf}
FunctionEnd

Function onLogDirTextChange
  ${NSD_GetText} $directoryText $logDir
FunctionEnd

; finally, use that custom page in between your other pages:
Page custom pageGetLogDir

Upvotes: 1

Seki
Seki

Reputation: 11465

If you just want a dialog similar to the installation directory page, you do not need to make a dialog yourself: you can just call the MUI_PAGE_DIRECTORY twice. Example taken from an existing setup:

!insertmacro MUI_PAGE_DIRECTORY ; <= it will store the selected directory into $INSTDIR

;second directory selection
!define MUI_PAGE_HEADER_SUBTEXT "Choose the folder in which to install the database."
!define MUI_DIRECTORYPAGE_TEXT_TOP "The installer will install the database(s) in the following folder. To install in a differenct folder, click Browse and select another folder. Click Next to continue."
!define MUI_DIRECTORYPAGE_VARIABLE $DbInstDir ; <= the other directory will be stored into that variable
!insertmacro MUI_PAGE_DIRECTORY

If you need to show the second directory selection only in some cases, you can add a callback to the second page with

!define MUI_PAGE_CUSTOMFUNCTION_PRE some_custom_function

In that callback, test if you need to show the directory selection or not. If not, calling Abort will skip the page.

Upvotes: 7

idleberg
idleberg

Reputation: 12882

Just in case you don't know about this: NSIS installers can create (or will, if using plugins) a temporary folder. You can initialise it using the InitPluginsDir command

Upvotes: 0

Related Questions