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

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

Related

How to pass command line arguments when run after install with electron-builder NSIS installer

With electron-builder NSIS installer we're able to create an executable installer which launches the installed electron app immediately after the install is finished. My question is is there a way to pass any command line parameters the installer itself was launched with to the installed app on this first launch?
I have seen some NSIS custom scripts which suggest that an executable can be launched with Exec, and the installer parameters can be retrieved with GetParameters. Is that a recommended direction, or is there some configuration option either in electron-builder or NSIS?
EDIT:
Here is a possible solution:
set nsis.runAfterFinish electron-builder option to false (true is the default);
implement customInstall event handler to customize the normal electron-builder provided template:
!macro StartAppWithParameters
Var /GLOBAL startAppWithParametersArgs
${if} ${isUpdated}
StrCpy $startAppWithParametersArgs "--updated"
${else}
StrCpy $startAppWithParametersArgs ""
${endif}
${StdUtils.GetAllParameters} $R0 0
${StdUtils.ExecShellAsUser} $0 "$launchLink" "open" '$startAppWithParametersArgs $R0'
!macroend
!macro customInstall
HideWindow
!insertmacro StartAppWithParameters
!macroend
Details are in electron-builder NSIS configuration, and electron-builder NSIS template
Thanks!
Yes you can do it manually with Exec and GetParameters:
!include "FileFunc.nsh"
!include "MUI2.nsh"
!macro RunWithInstallersParameters app
Push "${app}"
Call RunWithInstallersParameters
!macroend
Function RunWithInstallersParameters
Exch $0
Push $1
${GetParameters} $1
Exec '"$0" $1'
Pop $1
Pop $0
FunctionEnd
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!define MUI_FINISHPAGE_RUN
!define MUI_FINISHPAGE_RUN_FUNCTION MyFinishRun
!insertmacro MUI_PAGE_FINISH
!insertmacro MUI_LANGUAGE English
Function MyFinishRun
!insertmacro RunWithInstallersParameters "$sysdir\Calc.exe"
FunctionEnd
Section
SetOutPath $InstDir
File "blahblah"
!insertmacro RunWithInstallersParameters "$windir\Notepad.exe"
SectionEnd
The MUI Finish page also supports a way to specify the parameters directly but since we don't know the parameters at compile time we have to use a variable:
!include "FileFunc.nsh"
!include "MUI2.nsh"
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!define MUI_FINISHPAGE_RUN "$windir\Notepad.exe"
!define MUI_FINISHPAGE_RUN_PARAMETERS $1 ; Initialized by our MUI_PAGE_CUSTOMFUNCTION_SHOW function
!define MUI_PAGE_CUSTOMFUNCTION_SHOW InitFinishPage
!insertmacro MUI_PAGE_FINISH
!insertmacro MUI_LANGUAGE English
Function InitFinishPage
${GetParameters} $1
FunctionEnd
I don't know anything about electron-builder but I assume there is a way for you to customize the NSIS script somehow.

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.

Files installed to C:\ instead of C:\folder\

My installer copies Uninstall.exe and /temp/ directory to the installation folder (for me it is C:\MyExample), but MyExample.dll/ico/exe, and success/error.wav are copied directly to C:\ and I can't get this working.
!include "MUI2.nsh"
;General
Function .onInit
ReadEnvStr $R0 SYSTEMDRIVE
StrCpy $INSTDIR `$R0\MyExample\`
FunctionEnd
Name "MyExample"
OutFile "Setup.exe"
RequestExecutionLevel admin
;--------------------------------
!define MUI_ABORTWARNING
!define MUI_FINISHPAGE_RUN
!define MUI_FINISHPAGE_RUN_FUNCTION "LaunchLink"
!define MUI_FINISHPAGE_SHOWREADME ""
!define MUI_FINISHPAGE_SHOWREADME_NOTCHECKED
!define MUI_FINISHPAGE_SHOWREADME_TEXT "Create Desktop Shortcut"
!define MUI_FINISHPAGE_SHOWREADME_FUNCTION finishpageaction
;--------------------------------
;Pages
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_LICENSE "${NSISDIR}\Docs\Modern UI\License.txt"
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH
!insertmacro MUI_UNPAGE_WELCOME
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES
!insertmacro MUI_UNPAGE_FINISH
;--------------------------------
;Languages
!insertmacro MUI_LANGUAGE "English"
;--------------------------------
;Installer Sections
Section "Dummy Section" SecDummy
File "MyExample.exe"
File "MyExample.ico"
File "error.wav"
File "success.wav"
File "MyExample.dll"
CreateDirectory $INSTDIR\temp
SetOutPath "$INSTDIR"
WriteRegStr HKCU "Software\MyExample" "" $INSTDIR
;Create uninstaller
WriteUninstaller "$INSTDIR\Uninstall.exe"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Product\MyExample" \
"MyExample" "$\"$INSTDIR\uninstall.exe$\""
SectionEnd
Section "Uninstall"
# Always delete uninstaller first
delete $INSTDIR\uninstall.exe
delete $INSTDIR\MyExample.exe
delete $INSTDIR\MyExample.ico
delete $INSTDIR\error.wav
delete $INSTDIR\success.wav
delete $INSTDIR\MyExample.dll
SectionEnd
;--------------------------------
;Descriptions
LangString DESC_SecDummy ${LANG_ENGLISH} "Setup"
!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
!insertmacro MUI_DESCRIPTION_TEXT ${SecDummy} $(DESC_SecDummy)
!insertmacro MUI_FUNCTION_DESCRIPTION_END
;--------------------------------
;Uninstaller Section
Section "Uninstall"
Delete "$INSTDIR\Uninstall.exe"
RMDir "$INSTDIR"
DeleteRegKey /ifempty HKCU "Software\MyExample"
SectionEnd
Function LaunchLink
ExecShell "" "$INSTDIR\MyExample.exe"
FunctionEnd
Function finishpageaction
CreateShortcut "$desktop\MyExample.lnk" "$instdir\MyExample.exe"
WriteRegStr HKEY_CURRENT_USER "Software\Microsoft\Windows\CurrentVersion\Run" \
"MyExample.exe" "$INSTDIR\MyExample.exe"
FunctionEnd
In your Dummy Section section, you are installing some files without telling the destination directory. I suppose that implicitly meant $INSTDIR, but you need to be explicit as a section might or might not be selected (when you have multiple sections), and they must not rely on side effects of previously executed sections (if ever).
Add the destination folder at the beginning of the section, before the File statements:
SetOutPath $INSTDIR

Changing the selection in the components page of NSIS based on the OS on which we are installing

I am finding it difficult to change the selection in the components page of nsis.
The requirement is during installation i get a license agreement page ,if the user agrees then he/she will click on I AGREE ,after the user clicks on I AGREE ,i want to know on which OS the
setup is being installed that is it can be either on a Windows Embedded OS or WinXp/Win7.
So if it is Windows Embedded OS i want to change the installation package and if it is not Windows Embedded OS then the installation package will be different.
I am using MUI ver1 not MUI2 in my project.
Please let me know how this can be achieved.
To test for the OS where the setup is running, you can use macros defined Winver.nsh with those provided with LogicLib.nsh to make elegant tests like this
;Dont't forget to include
!include "LogicLib.nsh" # use of various logic statements
!include "WinVer.nsh" # LogicLib extension for OS tests
A platform test example:
${if} ${AtLeastWin95}
${AndIf} ${AtMostWinME}
;here we are on a pre-win2k
;do something
${elseIf} ${isWin2008}
${orIf} ${AtLeastWin2008R2}
;this is post-win7
;do other thing
${endif}
To change at runtime the components to install, you can use the macros from Sections.nsh:
;if you have
Section "Sample Database" SecApplicationDB
;...
SectionEnd
;you can select or un select by code:
!insertmacro SelectSection ${SecApplicationDB}
;or
!insertmacro UnselectSection ${SecApplicationDB}
WinVer.nsh does not support checking for Embedded NT but you can perform the check yourself:
!include Sections.nsh
!include MUI.nsh
!ifndef VER_SUITE_EMBEDDEDNT
!define VER_SUITE_EMBEDDEDNT 0x00000040
!endif
!insertmacro MUI_PAGE_LICENSE "${__FILE__}"
!insertmacro MUI_PAGE_COMPONENTS
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_LANGUAGE English
Section "Embedded" SID_EMBED
SectionIn RO
SectionEnd
Section "Normal" SID_NORMAL
SectionIn RO
SectionEnd
Function .onInit
System::Call '*(i156,&i152)i.r1'
System::Call 'KERNEL32::GetVersionExA(ir1)'
System::Call '*$1(&i152,&i2.r2)'
System::Free $1
IntOp $2 $2 & ${VER_SUITE_EMBEDDEDNT}
${If} $2 <> 0
!insertmacro SelectSection ${SID_EMBED}
!insertmacro UnselectSection ${SID_NORMAL}
${Else}
!insertmacro UnselectSection ${SID_EMBED}
!insertmacro SelectSection ${SID_NORMAL}
${EndIf}
FunctionEnd

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