bubustoober
28th July 2008 22:04 UTC
Modify InstallDir within a Section/Function
Note: I am new to NSIS and still getting used to the scripting language.
Goal: I want to allow the user of my installer to enter the name of their project (e.g. "ProjectX") on a custom page (I have successfully implemented this) and then append that to a default InstallDir (e.g. "C:\Program Files\Company Name\Product Title\ProjectX").
Problem: I can't figure out how to modify the InstallDir from within a Section or Function. Thanks in advance for any assistance!
Comperio
29th July 2008 18:53 UTC
Just use StrCpy to modify the $INSTDIR variable before showing the directory page.
KrisAster
29th July 2008 19:43 UTC
Right, though in practice if I were you I'd generally try to stay away from doing that. Something less prone to error might be something along the lines of:
Var PROJECTDIR ; PROJECTDIR is set in your custom page
Section "Make Custom Project X"
SetOutPath "$INSTDIR\$PROJECTDIR"
File "Blah"
SectionEnd
(Just my thoughts anyway.)
bubustoober
29th July 2008 22:40 UTC
Thanks for the help guys. I used a combination of your suggestions. I used StrCpy to set $INSTDIR with the default install directory plus the user-defined project name. See working code below!
;This function drives the custom page where the user enters the name of their project
Function enterProjectNamePage
nsDialogs::Create /NOUNLOAD 1018
Pop $Dialog
;Simple error handling
${If} $Dialog == error
Abort
${EndIf}
;Label above the project name text field
${NSD_CreateLabel} 0 0 100% 12u "Please enter the project name below:"
Pop $Label
;If the user has previously entered a project name, it was stored and will be recalled if they return to this page
;If Project_name is empty, the user has not previously entered a project name and so the field should be blank
${If} $Project_name == ""
; Disable next button until the user enters text
GetDlgItem $0 $HWNDPARENT 1
EnableWindow $0 0
;Create the text field
${NSD_CreateText} 0 13u 100% 12u ""
Pop $Text
${NSD_OnChange} $Text enableNextButton
${Else}
;Create the text field, prepopulated with user's previously entered text
${NSD_CreateText} 0 13u 100% 12u "$Project_name"
Pop $Text
${NSD_OnChange} $Text enableNextButton
${EndIf}
nsDialogs::Show
FunctionEnd
;This function is called in the previous function, enables the "next" button on the custom page only after the user has entered text in the project name field
Function enableNextButton
; Enable next button since the user has entered text
GetDlgItem $0 $HWNDPARENT 1
EnableWindow $0 1
FunctionEnd
;This function is executed upon leaving the custom page
Function enterProjectNamePageLeave
;Stores the text the user entered for project name
${NSD_GetText} $Text $Project_name
;Sets the default install directory to C:\Program Files\<product name>\<user-defined project name>
;StrCpy $INSTDIR "$PROGRAMFILES\${prodname}\$Project_name"
FunctionEnd
;Sets the default installation directory
InstallDir $INSTDIR