How to call directory page inside the function in NSIS? - nsis

I have created Exe file for my app using NSIS script.In my script i have checked free space for selected directory.
1.If selected directory dont have required space then user wants to change the directory.
2.After changing directory in directory page again wants to check free space.so when the required space is available for the selected directory then only proceed to next page.
So it will come under looping statement.I have tried following script
page custom checking
Function checking
Push "\"
push $InstallDir
Call SplitFirstStrPart
pop $R0
${DriveSpace} $R0 "/D=F /S=G" $R0
${While} $R0 <= 2
MessageBox MB_OK "Expected free space is not availble"
call directory
${EndWhile}
Function directory
--Here i want to define directory page--
[page directory] we cant use this here
call checking
FunctionEnd
1.How to create user defined directory page?
2.Is possible to call page directory or MUI_PAGE_DIRECTORY multiple times?
Thanks

You cannot call a page from a function but you can skip a page by calling Abort in the page PRE callback and you can also jump to any page.
You can have multiple pages of all page types:
!include MUI.nsh
Var dir1
Var dir2
Function .onInit
StrCpy $dir1 c:\default1
StrCpy $dir2 c:\default2
FunctionEnd
!define MUI_DIRECTORYPAGE_VARIABLE $dir1
!insertmacro MUI_PAGE_DIRECTORY
!define MUI_DIRECTORYPAGE_VARIABLE $dir2
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_LANGUAGE English
Section
DetailPrint $dir1
DetailPrint $dir2
SectionEnd

Related

RelGotoPage function goes to next page instead of going back

My current defined order of pages in the Installer are:
!insertmacro MUI_PAGE_WELCOME ; Welcome page
!insertmacro MUI_PAGE_DIRECTORY ; Select a directory
!define MUI_PAGE_CUSTOMFUNCTION_PRE CheckInstallDirectory ; Check selected directory
!insertmacro MUI_PAGE_COMPONENTS ; Choose install components
!define MUI_PAGE_CUSTOMFUNCTION_PRE SkipLicense ; Component1's check before license page
!insertmacro MUI_PAGE_LICENSE "license.txt" ; Component1's license
!define MUI_PAGE_CUSTOMFUNCTION_PRE SkipLicenseForComp2 ; Component2's check before license page
!insertmacro MUI_PAGE_LICENSE "License2.txt" ; Component2's license
!insertmacro CUSTOM_PAGES ; Custom install pages
!insertmacro MUI_PAGE_INSTFILES ; Install components' files
!insertmacro MUI_PAGE_FINISH ; Finish page
In the CheckInstallDirectory function, which is defined below, it checks if the end-user selected a directory that already houses an .exe that would otherwise be installed by the installer.
If the directory exists, the user is presented a pop-up message box with the "OK" and "CANCEL" buttons.
"OK" selection should simply skip the next few pages and go to MUI_PAGE_INSTFILES page.
"CANCEL" selection should return the user to the MUI_PAGE_DIRECTORY page.
Function CheckInstallDirectory
IfFileExists "$INSTDIR\Component1.exe" file_found end_of_check
file_found:
Var /GLOBAL ver_
${GetFileVersion} "$INSTDIR\Component1.exe" $ver_
StrCpy $0 $ver_ 1 ; Determine the major version of the installed exe (1st char of the returned string)
StrCpy $1 $ver_1 1 ; Determine the major version of the exe we are installing (1st char of the returned string)
${If} $0 == $1
; The major version between .exe's is the same
MessageBox MB_OKCANCEL|MB_ICONEXCLAMATION "A copy of this software is already installed in the selected directory.$\nSelect $\"OK$\" if you would like to update the software to the latest version.$\nOtherwise select $\"Cancel$\" to go back and select a different installation directory." IDOK OK IDCANCEL CANCEL
OK:
; User selected to update this software - continue with the installation like normal
StrCpy $R9 "7" ; Skip to the installation page
Call RelGotoPage
goto end_of_check
CANCEL:
StrCpy $R9 "-1" ; Go back to directory selection page
Call RelGotoPage
goto end_of_check
${Else}
; The major version between the .exe's is NOT the same - cannot update
MessageBox MB_OK "A copy of this software is already installed in the selected directory.$\nIt is not part of the latest major-version group and cannot be updated. To update this installation, uninstall and use the installer to install the latest version.$\nOtherwise, select$\"OK$\" to go back and select a different installation directory."
StrCpy $R9 "-1" ; Go back to directory selection page
Call RelGotoPage
goto end_of_check
${EndIf}
end_of_check:
FunctionEnd
I have found NSIS's description for how to move across installer pages here.
Function RelGotoPage
IntCmp $R9 0 0 Move Move
StrCmp $R9 "X" 0 Move
StrCpy $R9 "120"
Move:
SendMessage $HWNDPARENT "0x408" "$R9" ""
FunctionEnd
The problem that is occurring is that StrCpy $R9 "7" does successfully skip to the MUI_PAGE_INSTFILES page. Yet, StrCpy $R9 "-1" does not return to the previous page (which should be MUI_PAGE_DIRECTORY). Instead, if "CANCEL" is selected in that pop-up message, the installer goes to the next page, MUI_PAGE_COMPONENTS.
What is incorrect here?
Is there a better way to achieve the desired result?
Any help is sincerely appreciated! Thank you!
In the page pre-callback the page is not fully loaded and I assume that is why -1 does not work correctly. -1 does work if you use MUI_PAGE_CUSTOMFUNCTION_SHOW but the proper way to do this is to block the page change:
!include MUI2.nsh
!insertmacro MUI_PAGE_WELCOME ; Welcome page
!define MUI_PAGE_CUSTOMFUNCTION_LEAVE CheckInstallDirectory
!insertmacro MUI_PAGE_DIRECTORY
...
Function CheckInstallDirectory
;Snipped
MessageBox MB_OKCANCEL|MB_ICONEXCLAMATION "A copy of this software is already installed in the selected directory.$\nSelect $\"OK$\" if you would like to update the software to the latest version.$\nOtherwise select $\"Cancel$\" to go back and select a different installation directory." IDOK OK IDCANCEL CANCEL
OK:
; User selected to update this software - continue with the installation like normal
StrCpy $R9 "7" ; Skip to the installation page
Call RelGotoPage
goto end_of_check
CANCEL:
Abort
;Snipped
end_of_check:
FunctionEnd

How to display install page with the copy process as like in the image [NSIS script]

Outfile Test.exe
name "Test"
!include MUI2.nsh
!include LogicLib.nsh
!insertmacro MUI_PAGE_WELCOME
Page instfiles Installer
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_LANGUAGE "English"
Function Installer
UserInfo::GetAccountType
pop $0
${If} $0 != "admin" ;Require admin rights to install application
MessageBox mb_iconstop "Administrator rights required!"
SetErrorLevel 740 ;ERROR_ELEVATION_REQUIRED
Quit
${Else}
System::Call "kernel32::GetCurrentDirectory(i ${NSIS_MAX_STRLEN}, t .r0)"
CreateDirectory $3\pj
CopyFiles /SILENT \Source\*.* \destination\
${EndIf}
FunctionEnd
You have two InstFiles pages in your script, don't do that. You are also calling a function at the start of the InstFiles page but you should not perform file operations in page functions.
Your file operation itself makes no sense, you should not copy based on the current directory!
The "copy" progress from your screenshot is actually extracting files from the installer and you get that for free in NSIS:
RequestExecutionLevel Admin
InstallDir "$ProgramFiles\MyApp"
!include MUI2.nsh
!include LogicLib.nsh
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_LANGUAGE "English"
Function .onInit
UserInfo::GetAccountType
pop $0
${If} $0 != "admin" ;Require admin rights to install application
MessageBox mb_iconstop "Administrator rights required!"
SetErrorLevel 740 ;ERROR_ELEVATION_REQUIRED
Quit
${EndIf}
FunctionEnd
Section
SetOutPath "$InstDir"
File /r "c:\myfiles" ; This will display the extract progress
SectionEnd
If you actually want to copy files instead of extracting then you can just remove the /SILENT switch to display the normal Windows copy dialog.
If you must copy files with output similar to extraction you must manually walk the source directory with FindFirst+FindNext and use DetailPrint+CopyFiles /SILENT for each file...

!define MUI_PAGE_DIRECTORY_VARIABLE does not create variable - NSIS

I'm trying to create an NSIS script which copies various files to two different locations that can be specified.
I have checked the documentation and other StackOverflow entries but do not come up with a solution.
My Problem is that I want to define a second variable for a directory. In this directory sample files and projects shall be copied.
When trying to compile the NSI I get the following error/warnings:
3 warnings:
unknown variable/constant "APPDIR" detected, ignoring (C:\Users\max\Desktop\PortablePlayer\so.nsi:29)
unknown variable/constant "APPDIR" detected, ignoring (C:\Users\max\Desktop\PortablePlayer\so.nsi:33)
unknown variable/constant "APPDIR\Testfile.txt" detected, ignoring (C:\Users\max\Desktop\PortablePlayer\so.nsi:43)
Here is my script that I'm using:
!include "MUI.nsh"
Name "MyApp"
OutFile "MyApp-Installer.exe"
InstallDir "$PROGRAMFILES\My App"
; Installation Directory for the App
!insertmacro MUI_PAGE_DIRECTORY
; Installation Directory for the samples and projects
!define MUI_PAGE_HEADER_SUBTEXT "Choose your custom Samples Folder"
!define MUI_DIRECTORYPAGE_TEXT_TOP "To separate your App and your samples and projects you can choose a different folder than the installation directory"
!define MUI_PAGE_DIRECTORY_VARIABLE $APPDIR
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES
!insertmacro MUI_LANGUAGE "English"
Section ""
SetOutPath $INSTDIR
File Testfile.txt
SetOutPath $APPDIR
File Testfile.txt
FileOpen $0 "$DESKTOP\Hello_world.txt" w
FileWrite $0 $APPDIR
FileClose $0
WriteUninstaller "$INSTDIR\MyApp-Uninstaller.exe"
SectionEnd
Section "Uninstall"
Delete "$INSTDIR\Testfile.txt"
Delete "$INSTDIR\MyApp-Uninstaller.exe"
RMDir $INSTDIR
Delete "$APPDIR\Testfile.txt"
SectionEnd
For debugging I tried to write the value of $APPDIR into a textfile but it only writes $APPDIR instead of the value.
If I change it to $INSTDIR it prints the correct path for $INSTDIR.
How can I initialize the variable for $APPDIR?
If I try to initialize it on the top, it is there but empty, so the installer fails after succesful compilation of the script.
Running Win 7 with NSIS 2.5.1
Can anybody please shed some light on what is wrong?
Thanks!
The MUI_DIRECTORYPAGE_VARIABLE define tells MUI that you have a custom variable that you want to use but MUI does not create the variable, you have to do that yourself near the top of your script:
Var APPDIR
So it should look something like
Var APPDIR
!include "MUI.nsh"
!define MUI_DIRECTORYPAGE_VARIABLE $APPDIR
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_LANGUAGE "English"
Section
DetailPrint $APPDIR
SectionEnd
Edit: The name of the define is MUI_DIRECTORYPAGE_VARIABLE not MUI_PAGE_DIRECTORY_VARIABLE like you have in your example...

With NSIS can I share values between MUI_PAGE_DIRECTORY pages?

I have two MUI_PAGE_DIRECTORY pages. The first stores the entered data to the default, $INSTDIR. What I would like to do is copy the value in $INSTDIR to the defined var $DataDir so that before the second directory page is displayed, $DataDir becomes $INSTDIR\Data. When the second page is displayed I would like the default value to be $INSTDIR\Data.
Is this possible?
InstallDir $ProgramFiles\MyApp
Var DataDir
!include MUI2.nsh
!define MUI_PAGE_CUSTOMFUNCTION_LEAVE InstDirPageLeave
!insertmacro MUI_PAGE_DIRECTORY
!define MUI_DIRECTORYPAGE_VARIABLE $DataDir
!define MUI_DIRECTORYPAGE_TEXT_TOP "Choose Data directory for bla bla bla..."
!define MUI_DIRECTORYPAGE_TEXT_DESTINATION "Data Directory:"
!define MUI_PAGE_CUSTOMFUNCTION_SHOW DataDirShowPage
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_LANGUAGE English
Function InstDirPageLeave
StrCpy $DataDir "$InstDir\Data"
FunctionEnd
Function DataDirShowPage
!insertmacro MUI_HEADER_TEXT "Foo" "Bar"
FunctionEnd
Section
DetailPrint $InstDir
DetailPrint $DataDir
SectionEnd
What are you trying to achieve?
If you want to install some files into two separate locations use MUI_PAGE_DIRECTORY and custom nsDialogs page (with browse folder button) so user can pick up two directories.
If your location is always $INSTDIR and second is somewhere inside it ($INSTDIR\some\data\path) then you only need to append your inside path to $INSTDIR - there is no need to show dialog twice and select path twice.

Set value of InstallDir in a function, or set auto populate value somehow?

I'm creating an installer using NSIS. This installer actually installs two programs in two different directories in the same installer. I am doing this using the modern user interface (MUI) pages and simply calling MUI_PAGE_DIRECTORY twice specifying different starting parameteres, and capturing the directory in the LEAVE macro. What I'm wondering is, can I somehow call InstallDir in a function, or set the auto directory populate value in a function? Or possibly even call a function after the browse button has been returned from?
The reason I want to do this is so when the user clicks the browse button in either of the two directory pages, after they select a directory, the name of the finnal directory specifed in InstallDir will be appended.
For example:
InstallDir value for program 1: c:\client
InstallDir value for program 2: c:\program files\server
user clicks browse on program 1 and chooses c:\temp the resulting path is c:\temp\client
user clicks browse on program 2 and chooses c:\whatever the resulting path is c:\whatever\server
For reference here are the code snipits of what I have that works, but does not deal with the auto append browse button behaviour:
!define MUI_PAGE_CUSTOMFUNCTION_LEAVE ClientDirectoryLeave
!insertmacro MUI_PAGE_DIRECTORY
!define MUI_PAGE_CUSTOMFUNCTION_LEAVE ServerDirectoryLeave
!insertmacro MUI_PAGE_DIRECTORY
; Setup the page display for the client install page
Function ShowPageClient
!insertmacro MUI_HEADER_TEXT "Client" "Client"
!insertmacro MUI_INNERDIALOG_TEXT 1006 "Client"
; setup intal directory
Push $0
StrCpy $0 $PROGRAMFILES 2 #
; CLIENT_FOLDER_NAME is defined as a folder, but this would basicaly
; result in C:\Client as the first 2 characters of $PROGRAMFILES
; is the hard drive with program files installed on it
StrCpy $INSTDIR "$0\${CLIENT_FOLDER_NAME}"
Pop $0
; set the inital value of the directory text box
!insertmacro MUI_INNERDIALOG_TEXT 1019 $INSTDIR
; find and disable the directory selection box
; We do not want users to type in this box
FindWindow $R0 "#32770" "" $HWNDPARENT
GetDlgItem $R1 $R0 1019 ;Text Box
EnableWindow $R1 0
FunctionEnd
; Setup the page display for the server install location page
Function ShowPageServer
!insertmacro MUI_HEADER_TEXT "Server" "Server"
!insertmacro MUI_INNERDIALOG_TEXT 1006 "Server"
; setup intal directory
; SERVER_FOLDER_NAME is defined as a folder, but this would basicaly
; result in C:\Program Files\Server
StrCpy $INSTDIR "$PROGRAMFILES\${SERVER_FOLDER_NAME}"
; set the inital value of the directory text box
!insertmacro MUI_INNERDIALOG_TEXT 1019 $INSTDIR
; find and disable the directory selection box
; We do not want users to type in this box
FindWindow $R0 "#32770" "" $HWNDPARENT
GetDlgItem $R1 $R0 1019 ;Text Box
EnableWindow $R1 0
FunctionEnd
Note: I can make the browse button work for one of the directory pages, but then when I'm on the second page, the auto populate actual auto populates incorrectly
The appended folder name is constant and set at compile time, there is a bug report related to this.
My advice is to abandon the append feature and let the user have full control over the two destinations:
Name "NSIS Test"
InstallDir ""
!include MUI.nsh
Var DirClient
Var DirServer
Function .onInit
;Set default destinations
StrCpy $DirClient "$ProgramFiles\$(^Name)\Client"
StrCpy $DirServer "$ProgramFiles\$(^Name)\Server"
FunctionEnd
!macro ConfigureMyDirPage type var
!define MUI_DIRECTORYPAGE_VARIABLE ${var}
!define MUI_PAGE_HEADER_SUBTEXT "Choose the folder in which to install $(^NameDA) ${type}"
!define MUI_DIRECTORYPAGE_TEXT_TOP "Setup will install $(^NameDA) ${type} in the following folder. To install in a different folder, click Browse and select another folder. $_CLICK"
!define MUI_DIRECTORYPAGE_TEXT_DESTINATION "${type} $(^DirSubText)"
!macroend
!insertmacro ConfigureMyDirPage "Client" $DirClient
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro ConfigureMyDirPage "Server" $DirServer
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_LANGUAGE "English"
Section
DetailPrint DirClient=$DirClient
DetailPrint DirServer=$DirServer
SectionEnd
Okay I finally figured it out. Basically there is a function that is called to "verify" the path after browse button is clicked. I tied into this function an appended the directory manual if needed. To do this I created a new variable and set it in a function called when the page is displays as follows:
; Client Directory
!define MUI_PAGE_CUSTOMFUNCTION_SHOW ShowPageClient
!define MUI_PAGE_CUSTOMFUNCTION_LEAVE ClientDirectoryLeave
!insertmacro MUI_PAGE_DIRECTORY
; Setup the page display for the client install page
Function ShowPageClient
; setup intal directory
Push $0
StrCpy $0 $PROGRAMFILES 2 #
StrCpy $INSTDIR "$0\${CLIENT_FOLDER_NAME}"
Pop $0
!insertmacro MUI_INNERDIALOG_TEXT 1019 $INSTDIR
FindWindow $R0 "#32770" "" $HWNDPARENT
GetDlgItem $R1 $R0 1019 ;Text Box
EnableWindow $R1 0
; Setup the client or server variable to indicate that
; we're in the client install part to signal the .onVerifyInstDir function
; to put the correct directory
StrCpy $CLIENT_OR_SERVER "client"
FunctionEnd
The function that is called after browse is .onVerifyInstDir so that is where I check the CLIENT_OR_SERVER variable and set the path appropriately
; This function is special and is called any time a
; path is validated on returning from the browse button
; need to append the correct directory if it does not already exists
; in the install directory path
Function .onVerifyInstDir
; save the current $0 register, as it is used in this function
Push $0
${If} $CLIENT_OR_SERVER == "client"
; in the client stage so directory must contain CLIENT_FOLDER_NAME
${StrContains} $0 "${CLIENT_FOLDER_NAME}" "$INSTDIR"
${If} $0 == ""
; the install dir does not contain the folder so append it
StrCpy $INSTDIR "$INSTDIR\${CLIENT_FOLDER_NAME}"
${EndIf}
${Else}
; in the server stage so directory must contain SERVER_FOLDER_NAME
${StrContains} $0 "${SERVER_FOLDER_NAME}" "$INSTDIR"
${If} $0 == ""
; the install dir does not contain the folder so append it
StrCpy $INSTDIR "$INSTDIR\${SERVER_FOLDER_NAME}"
${EndIf}
${EndIf}
; pop the saved register value
Pop $0
FunctionEnd
Couple notes:
the StrContains function I used was found here:
http://nsis.sourceforge.net/StrContains
Further reference to the .onVerifyInstDir function can be found here:
http://nsis.sourceforge.net/Docs/Chapter4.html#4.7.2.1.10

Resources