NSIS script condition compilation based upon existence of a file - nsis

I have an NSIS based installer that I need to be able generate slightly different versions of under different conditions.
The conditions are easy to establish at compile time, if a particular file exists on the disk then the alternative branding can be used. I know I could use a command-line option to the makensis.exe to provide this behaviour, but it would be better if the compiler could take care of this for me.
Is there a way of making a compile time "IfExist" type logic?

!macro CompileTimeIfFileExist path define
!tempfile tmpinc
!system 'IF EXIST "${path}" echo !define ${define} > "${tmpinc}"'
!include "${tmpinc}"
!delfile "${tmpinc}"
!undef tmpinc
!macroend
Section
!insertmacro CompileTimeIfFileExist "$%windir%\explorer.exe" itsThere
!ifdef itsThere
MessageBox mb_Topmost yes
!else
MessageBox mb_Topmost no
!endif
SectionEnd
Note: the !system command used here assumes you are compiling on windows

I don't have the answer to your generic question of compile-time file detection, but I do have a solution to what it sounds like you're trying to accomplish.
My installers use something like this:
In the file CustomBranding.nsh:
!define CUSTOM_BRANDING
!define APPNAME "Brand X"
!define LOGO "C:\Brand_X_Logo.png"
In the main installer script:
!include /NONFATAL "CustomBranding.nsh"
!ifndef CUSTOM_BRANDING
!define APPNAME "Generic"
!define LOGO "C:\Generic_Logo.png"
!endif
Is that the kind of "alternative branding" you're asking about?

Related

NSIS MUI_FINISHPAGE_RUN

I don't understand why:
If I use
!define MUI_FINISHPAGE_RUN notepad.exe
it works, Notepad starts.
If I use
!define MUI_FINISHPAGE_RUN winword.exe
nothing happens.
Notepad is in the Windows directory and that directory is searched by Exec. Winword is in some subfolder under program files most likely (and not a part of %path%) and therefore it is not found.
The best solution is to provide the full path to the application you want to run.
Normally you do
!define MUI_FINISHPAGE_RUN "$InstDir\MyApp.exe"
You could try
!define MUI_FINISHPAGE_RUN
!define MUI_FINISHPAGE_RUN_FUNCTION myrun
!insertmacro MUI_PAGE_FINISH
Function myrun
ExecShell "" "winword.exe"
FunctionEnd
This will work if Word registers itself in the App Paths key. Alternatively you could ExecShell a .Doc file if you know Word is installed.

Space required on MUI_PAGE_DIRECTORY is 0.0KB

I'm writing an installer for Windows application. I'm using MUI for NSIS. One of the pages of the installer is Directory Page, where I have the "Space required" field. The problem is that the space required is always 0.0KB.
I was looking for some answers, but all I found was that space is calculated automatically. I wonder if there is some way to check which folder size this macro gets? Or any other ideas?
;Pages
; Installation
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_LICENSE "#CPACK_RESOURCE_FILE_LICENSE#"
!insertmacro MUI_PAGE_DIRECTORY
Page custom pgAppLanguageCreate
The size is automatically calculated by the File instructions in Sections plus any extra you add with AddSize.
If you are not happy with this calculation you can force a specific size in .onInit with SectionSetSize.
I found what causes the problem.
In the "Installer Section" I used a function that copies all files.
Instead of calling it here, I just paste the entire function and now that works. It looks like you cant use "File" instruction in function because it doesn't add size to the required space this way.
If you use it in section, it works perfectly.
; Installer Sections
Section "install" Install
${If} $instState != "1"
Call deleteAllFiles
Call deleteInfoFromRegistry
Call deleteShortcut
${EndIf}
SetOutPath "$INSTDIR"
; copy files
File ${ICON}
File "slicer_license"
File /r "${INSTALL_FILE}\package\"
;File "README.txt" ;Readme file.
Call createShortcut
Call addInfoToRegistry
Call setAppLanguageShortcut
Call createLanguageFile
SectionEnd

NSIS pages and sections execution

probably I am not getting basics of pages and sections in nsis script.
I have to analyse installation script which was not made by me. In the top of the script there are macros of MUI pages for example
!insertmacro MUI_PAGE_LICENSE $(license)
!insertmacro MUI_PAGE_INSTFILES ....
And then further down the code there are sections
Section "MainSection" SEC01
SetShellVarContext current
SetOutPath "$INSTDIR"
SetOverwrite ifnewer
File "${xy_TEMP_SRC}\InstallSrc\xy.exe"
File "${xy_TEMP_SRC}\InstallSrc\xy.exe.config"
SetOutPath "$INSTDIR\sk"
File "${xy_TEMP_SRC}\InstallSrc\sk\xy.resources.dll"
SetOutPath "$INSTDIR"
CreateDirectory "$SMPROGRAMS\xy"
CreateShortCut "$SMPROGRAMS\xy\xy.lnk" "$INSTDIR\xy.exe"
CreateShortCut "$DESKTOP\xy.lnk" "$INSTDIR\xy.exe"
SectionEnd
+ another sections for instance unninstall section
My question is how and when the sections are executed when there is no reference from pages to the sections.
My brain is telling me that the sections should be executed sometimes during the pages confirmation during the installation process, but I guess it's wrong, so please can anyone tell me how it actualy works?
All sections are executed on the instfiles page and in the order of your sections. If you need stuff to be executed before, after or in between, you can use functions (e.g. pre- or leave functions)
!insertmacro MUI_PAGE_INSTFILES Execute the sections.

Executing Bat File in NSIS installer-not working

I have builded nsis script successfully for my java project.I have a batch file that I need to run within my NSIS installer.It must run after all the files have been extracted.I have tried following commands
!define MUI_FINISHPAGE_RUN $INSTDIR\bin\batch.bat
This one also have tried:
SetOutPath $INSTDIR
ExpandEnvStrings $0 %COMSPEC%
nsExec::ExecToStack '"$INSTDIR\batch.bat"'
I have referred this link.
My Requirement is:
1.How to start batch file after installation completion using Nsis script?
Why call ExpandEnvStrings if you are not going to use the result? The path does not even match in your two examples.
As long as you get the path and quotes correct it should work:
!include MUI2.nsh
!insertmacro MUI_PAGE_INSTFILES
!define MUI_FINISHPAGE_RUN
!define MUI_FINISHPAGE_RUN_FUNCTION RunBatch
!insertmacro MUI_PAGE_FINISH
!insertmacro MUI_LANGUAGE English
Function RunBatch
;The most basic version, runs with visible console:
ExecWait '"$temp\test.cmd" /foo "bar baz" /blergh'
;You must use cmd.exe if you want redirection (With stupid extra quotes for cmd.exe):
ExpandEnvStrings $0 %COMSPEC%
ExecWait '"$0" /C ""$temp\test.cmd" /foo "bar baz" /blergh > "$temp\stdout.txt""'
;Use one of the exec plugins if you want to hide the console:
nsExec::Exec '"$temp\test.cmd" /foo "bar baz" /blergh'
FunctionEnd
There are several exec plugins you can use depending on your needs: nsExec, ExecDos
or ExecCmd

How do I use/include StrContains in NSIS

Do I need to include a specific .nsh library or function definition to use the function 'StrContains' in NSIS?
I have looked for a download for the library but I cant seem to find it?
When I go to compile this code I get the compile error: "Invalid command: ${StrContains}"
!include "LogicLib.nsh"
# Compile error below
!macro test
${StrContains} $0 $1 "abc"
!macroend
Section
DetailPrint ""
SectionEnd
You need to add the function definition shown in the StrContains function page of the NSIS wiki (in the category of strings functions) in your code.
Don't forget the last statement !define StrContains ... to be able to call it with ${StrContains}

Resources