With NSIS can I share values between MUI_PAGE_DIRECTORY pages? - nsis

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.

Related

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...

How to display a custom text on MUI_PAGE_INSTFILES in NSIS

I am beginner in NSIS, and I want to display the custom text in MUI_PAGE_INSTFILES page as I have two options in Components page 1. Installation and 2. UnInstallation.
So when I choose Below options I am expecting the corresponding text in MUI_PAGE_INSTFILES page.
For Installation Option the text should be "Installation completed"
For UnInstallation Option the text should be "UnInstallation Completed"
Thanks in advance for your help on this.
The CompletedText attribute supports variables:
var mycompletedtext
var myfinishsubtext
CompletedText $mycompletedtext
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_COMPONENTS
!define MUI_INSTFILESPAGE_FINISHHEADER_TEXT "$mycompletedtext"
!define MUI_INSTFILESPAGE_FINISHHEADER_SUBTEXT "$myfinishsubtext"
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_LANGUAGE "English"
Section Installer
StrCpy $mycompletedtext "Installation completed"
StrCpy $myfinishsubtext "Something something"
SectionEnd
Section Uninstaller
StrCpy $mycompletedtext "UnInstallation Completed"
StrCpy $myfinishsubtext "Bye bye"
SectionEnd

Custom functions by pages in NSIS disappear

I have the following script:
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_COMPONENTS
!define MUI_PAGE_CUSTOMFUNCTION_PRE ComponentPost
!insertmacro MUI_PAGE_DIRECTORY
!define MUI_PAGE_CUSTOMFUNCTION_SHOW DirectoryShow
!define MUI_PAGE_CUSTOMFUNCTION_LEAVE DirectoryLeave
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH
Unfortunately, after the first directory selection dialog, directory page returns to default, what am i doing wrong?
When using multiple directory pages you also normally want to store the selected paths in different variables:
Var SomeOtherFolder
!define MUI_DIRECTORYPAGE_VARIABLE $SomeOtherFolder
!insertmacro MUI_PAGE_DIRECTORY
...
Section
SetOutPath $SomeOtherFolder
File /r "c:\otherfiles"
Section
All of this information can be found in the Modern UI readme...

NSIS Installer Start Menu Directory

I have an NSIS installer that has the start menu folders hard-coded using this code...
Var SMDir ;Start menu folder
!insertmacro MUI_PAGE_STARTMENU 0 $SMDir
Section -StartMenu
!insertmacro MUI_STARTMENU_WRITE_BEGIN 0
CreateDirectory "$SMPrograms\MY Program\My Folder"
CreateShortCut "$DESKTOP\My Program" "$INSTDIR\start.exe"
CreateShortCut "$SMPROGRAMS\MY Program\My Shortcut.lnk" "$INSTDIR\start.exe"
CreateShortCut "$SMPROGRAMS\My Program\Uninstall.lnk" "$INSTDIR\uninstall.exe"
This all works apart from on the 'Choose Start Menu Folder' it doesn't let me change the default installation directory.
Is there a way to fix this or alternatively how can I skip this page but still trigger the StartMenu section?
If you don't want a startmenu selection page at all just remove !insertmacro MUI_PAGE_STARTMENU ... and the two !insertmacro MUI_STARTMENU_* macros in the -StartMenu section.
If you want to allow the user to choose the directory then you must use the variable and not hardcode the path:
outfile test.exe
name "SM Test"
requestexecutionlevel user ;Single user/"Just me" installer
!define MUI_COMPONENTSPAGE_NODESC
!include mui2.nsh
Var SMDir ;Start menu folder
!insertmacro MUI_PAGE_COMPONENTS
;!define MUI_STARTMENUPAGE_DEFAULTFOLDER "MY Program" ;Default, name is used if not defined
!insertmacro MUI_PAGE_STARTMENU 0 $SMDir
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_LANGUAGE English
Section -StartMenu
!insertmacro MUI_STARTMENU_WRITE_BEGIN 0 ;This macro sets $SMDir and skips to MUI_STARTMENU_WRITE_END if the "Don't create shortcuts" checkbox is checked...
CreateDirectory "$SMPrograms\$SMDir"
CreateShortCut "$SMPROGRAMS\$SMDir\Myapp.lnk" "$INSTDIR\start.exe"
!insertmacro MUI_STARTMENU_WRITE_END
SectionEnd
Section "Desktop Shortcut"
CreateShortCut "$DESKTOP\Myapp.lnk" "$INSTDIR\start.exe"
SectionEnd
(If you are installing as administrator then you should call SetShellVarContext all before accessing $SMPrograms and you should probably not create a desktop shortcut)

Resources