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

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.

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

Adding a checkbox to the NSIS Uninstaller Welcome Page

I'm trying to add a checkbox to the welcome screen of my NSIS uninstaller, but I'm having trouble finding an example. From the documentation for MUI2 I can't find any custom functions that can be run on the welcome page.
It looks like the finish page is more easy to customize based on the documentation and other answers I've found.
Is there a way to customize the Welcome Page? If not, what are the other options for accomplishing the intent?
The MUI(1) documentation you linked to has a note about how you can customize the welcome page in the pre/show callbacks. With MUI2 you can add controls in the show callback. See the nsDialogs documentation for more information about these custom controls...
!include MUI2.nsh
!insertmacro MUI_PAGE_INSTFILES
!define MUI_PAGE_CUSTOMFUNCTION_SHOW un.ModifyUnWelcome
!define MUI_PAGE_CUSTOMFUNCTION_LEAVE un.LeaveUnWelcome
!insertmacro MUI_UNPAGE_WELCOME
!insertmacro MUI_UNPAGE_INSTFILES
!insertmacro MUI_LANGUAGE English
Var mycheckbox ; You could just store the HWND in $1 etc if you don't want this extra variable
Function un.ModifyUnWelcome
${NSD_CreateCheckbox} 120u -18u 50% 12u "Do something special"
Pop $mycheckbox
SetCtlColors $mycheckbox "" ${MUI_BGCOLOR}
${NSD_Check} $mycheckbox ; Check it by default
FunctionEnd
Function un.LeaveUnWelcome
${NSD_GetState} $mycheckbox $0
${If} $0 <> 0
MessageBox mb_ok "I'm special"
${EndIf}
FunctionEnd
Section testuninstaller
Initpluginsdir
WriteUninstaller "$pluginsdir\u.exe"
ExecWait '"$pluginsdir\u.exe" _?=$pluginsdir'
Sectionend

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

Resources