Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ask user for additional directory location in NSIS

Tags:

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

like image 916
msd_2 Avatar asked Oct 17 '25 23:10

msd_2


2 Answers

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.

like image 192
Seki Avatar answered Oct 19 '25 12:10

Seki


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
like image 44
bk138 Avatar answered Oct 19 '25 14:10

bk138



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!