In my NSIS installer, I want to display an input dialog (text + textbox) to the user and to retrieve the result of that input, so that I can use it later in the NSIS script.
I've found this reference page:
http://nsis.sourceforge.net/Docs/Chapter4.html#4.9.4.15
But I couldn't find any reference to a textbox.
If it helps, what I need is the NSIS equivalent of the following AppleScript code:
display dialog "Insert value:" default answer ""
set value to text returned of result
Use nsDialogs or InstallOptions (both part of NSIS) to create a custom page:
Outfile test.exe
Requestexecutionlevel user
!include nsDialogs.nsh
Page Custom mypagecreate mypageleave
Page Instfiles
Function mypagecreate
Var /Global MyTextbox
nsDialogs::Create /NOUNLOAD 1018
Pop $0
${NSD_CreateText} 10% 20u 80% 12u "Hello World"
Pop $MyTextbox
nsDialogs::Show
FunctionEnd
Function mypageleave
${NSD_GetText} $MyTextbox $0
MessageBox mb_ok $0
Abort ;Don't move to next page (If the input was invalid etc)
FunctionEnd
Section
SectionEnd
Popup dialogs are not really supported but it can be done with this plugin...
Related
I have a installer file called sample.exe. This exe file will have some components that use have to be defined such as port number, installation directory and etc. I have to bundle this sample.exe file inside another installer called test.exe. So when i try to install the test.exe, it should also install the sample.exe. i could see there are options to achieve this in nsis, but how to provide options for the use to enter the port, directory path of sample.exe while installing the test.exe ? i am beginner to nsis and any reference are example script will help me lot. Thanks in advance.
You need to create custom (installer) page where these values are entered.
Use nsDialogs for this (recommended): http://nsis.sourceforge.net/Docs/nsDialogs/Readme.html
There is no exact solution for this as your specification is really vague, rather check the examples and use provided code snippets from them.
You generally use the directory page to let the user choose the installation directory. Non-standard user input can be recorded on a custom page using the nsDialogs plug-in:
Name "Foo"
OutFile "TestSetup.exe"
RequestExecutionLevel admin
InstallDir "$ProgramFiles\Test"
!define DEFAULTPORT 666
!include nsDialogs.nsh
Var MyPort
Var PortEdit
Function .onInit
StrCpy $MyPort "${DEFAULTPORT}"
FunctionEnd
Page Directory
Page Custom MyPageCreate MyPageLeave
Page InstFiles
Function MyPageCreate
nsDialogs::Create 1018
Pop $0
${NSD_CreateLabel} 0u 10% 20u 12u "Port: "
Pop $0
${NSD_CreateNumber} 20u 10% 40u 12u "$MyPort"
Pop $PortEdit
nsDialogs::Show
FunctionEnd
Function MyPageLeave
${NSD_GetText} $PortEdit $MyPort
FunctionEnd
Section
SetOutPath $InstDir
File "Sample.exe"
; Write the chosen port to a config file:
WriteIniStr "$InstDir\Config.ini" "Network" "Port" "$MyPort"
SectionEnd
I was trying to include a check box in my uninstaller. I was able to put the check box in place. But when I try to get the state of check box it always returns 0 even though check box is checked. Here is the code I am using
!define MUI_WELCOMEPAGE_TITLE_3LINES
!define MUI_PAGE_CUSTOMFUNCTION_SHOW un.ModifyUnWelcome
!insertmacro MUI_UNPAGE_WELCOME
!insertmacro MUI_UNPAGE_INSTFILES
!define MUI_FINISHPAGE_TITLE_3LINES
!define MUI_PAGE_CUSTOMFUNCTION_LEAVE un.LeaveUnWelcome
!insertmacro MUI_UNPAGE_FINISH
Function un.ModifyUnWelcome
${NSD_CreateCheckbox} 120u -18u 50% 25u "Do something special"
Pop $mycheckbox
SetCtlColors $mycheckbox "" ${MUI_BGCOLOR}
${NSD_Check} $mycheckbox ; Check it by default
FunctionEnd
Function un.LeaveUnWelcome
${NSD_GetState} $mycheckbox $0
MessageBox MB_OK "On Leave mycheckbox = $mycheckbox $\n $$0 = $0"
${If} $0 <> 0
MessageBox mb_ok "I'm special"
${EndIf}
FunctionEnd
As a result I could not verify if the check box is checked or not.
what is wrong with my code and how can I fix it?
I even tried something like below
${NSD_Check} $mycheckbox ; Check it by default
${NSD_SetState} $mycheckbox ${BST_CHECKED}
I got the above code from Adding a checkbox to the NSIS Uninstaller Welcome Page
Your custom functions referred to two different pages.
ModifyUnWelcome is calling when creates MUI_UNPAGE_WELCOME page, LeaveUnWelcome - when user leaves last MUI_UNPAGE_FINISH page.
Control, owned by handle at ModifyUnWelcome will be destroyed at the moment when you are calling ${NSD_GetState}. You should to place custom functions definitions near each other and before referred page declaration.
!define MUI_WELCOMEPAGE_TITLE_3LINES
!define MUI_PAGE_CUSTOMFUNCTION_SHOW un.ModifyUnWelcome
!define MUI_PAGE_CUSTOMFUNCTION_LEAVE un.LeaveUnWelcome
!insertmacro MUI_UNPAGE_WELCOME
!insertmacro MUI_UNPAGE_INSTFILES
!define MUI_FINISHPAGE_TITLE_3LINES
!insertmacro MUI_UNPAGE_FINISH
I have installer, which supports choosing installation directory. And I want to detect if given folder exists and if it is empty. If it is NOT empty, show warning message box, then remove all its contents and install program into that folder. Only problem is to get into the right code section, where I can get installation folder given by user during installation, I can handle the rest.
Thank you for any advices.
Normally you would just check if the directory exists:
Outfile "$%Temp%\Test.exe"
RequestExecutionLevel user
InstallDir "$Documents\Test"
!include LogicLib.nsh
Page Directory "" "" DirLeave
Page InstFiles
Function DirLeave
${If} ${FileExists} "$InstDir\*"
MessageBox MB_YESNO `"$InstDir" already exists, delete it's content and continue installing?` IDYES yep
Abort
yep:
RMDir /r "$InstDir"
${EndIf}
FunctionEnd
Section
SetOutPath $InstDir
File myfile.ext
SectionEnd
This will also display the message if the directory exists but is empty. To work around that you would need some custom detection:
!macro _IsNonEmptyDirectory _a _b _t _f
!insertmacro _LOGICLIB_TEMP
!insertmacro _IncreaseCounter
Push $0
FindFirst $0 $_LOGICLIB_TEMP "${_b}\*"
_IsNonEmptyDirectory_loop${LOGICLIB_COUNTER}:
StrCmp "" $_LOGICLIB_TEMP _IsNonEmptyDirectory_done${LOGICLIB_COUNTER}
StrCmp "." $_LOGICLIB_TEMP +2
StrCmp ".." $_LOGICLIB_TEMP 0 _IsNonEmptyDirectory_done${LOGICLIB_COUNTER}
FindNext $0 $_LOGICLIB_TEMP
Goto _IsNonEmptyDirectory_loop${LOGICLIB_COUNTER}
_IsNonEmptyDirectory_done${LOGICLIB_COUNTER}:
FindClose $0
Pop $0
!insertmacro _!= "" $_LOGICLIB_TEMP `${_t}` `${_f}`
!macroend
!define IsNonEmptyDirectory `"" IsNonEmptyDirectory`
Function DirLeave
${If} ${IsNonEmptyDirectory} "$InstDir"
MessageBox MB_YESNO `"$InstDir" already exists, delete it's content and continue installing?` IDYES yep
Abort
yep:
RMDir /r "$InstDir"
${EndIf}
FunctionEnd
I'm having a weird error with NSIS:
!include "MUI2.nsh"
!include "FileFunc.nsh" # To use GetParameters
Name nsDialogs
OutFile nsDialogs.exe
Function .onInit
${GetParameters} $R0
MessageBox MB_OK "$R0"
FunctionEnd
!insertmacro MUI_PAGE_WELCOME
Section
DetailPrint "hello world"
SectionEnd
If I use this command line
nsDialogs.exe /d=hello
the message box says: "/d=hello" as expected, but if I use
nsDialogs.exe /D=hello
the message box says "" and this is wrong.
Why is this happening?
From the documentation:
/D sets the default installation directory ($INSTDIR), overriding
InstallDir and InstallDirRegKey. It must be the last parameter used in
the command line and must not contain any quotes, even if the path
contains spaces. Only absolute paths are supported.
This means you cannot use /D with ${GetParameters} (/S and /NCRC are also switches used by NSIS). NSIS by design uses everything after /D= as $instdir.
The only way to detect /D is to not use InstallDir[RegKey] in your script and check if $instdir is != "" in .onInit
/D is command line parameter that let define the installation directory directly from the installer command line invocation.
See the Installer usage / Common Options chapter for details.
I don't know for sure, but I assume that NSIS strips out its inbuilt parameters by default. In that case you could try something like this:
!define myInstDir "$PROGRAMFILES\myApp"
Function .onInit
${GetParameters} $R0
StrCpy $R0 ${myInstDir} +2
MessageBox MB_OK "$$INSTDIR was changed on runtime"
FunctionEnd
I want to add a label displaying the full version-string in the welcome screen in the installer I am creating using NSIS with MUI2.
I have searched for info on how to do this, but only found references to using MUI_INSTALLOPTIONS* which I found ws deprecated for MUI2. Another one referred to the newer versions using INSTALLOPTIONS* with the same options, but I could not get it working. I finally also found a reference to using nsDialogs for this - which is what I am using for my custom pages. However - I found no reference or samples on how to change any of the existing pages that comes with MUI2.nsh.
I found a way to change the MUI_HEADERTEXT, but that doesn't affect the welcome-screen. I wish there was a way to also change the welcometext. Maybe using MUI_WELCOMETITLE and MUI_WELCOMEBODY or similar.
There is MUI_WELCOMEPAGE_TEXT but it is only useful if you want to change all of the text and not just append something.
During the show function for the page, you can change the text of any control:
outfile test.exe
requestexecutionlevel user
!include MUI2.nsh
#!define MUI_WELCOMEPAGE_TEXT "New text goes here"
!define MUI_PAGE_CUSTOMFUNCTION_SHOW MyWelcomeShowCallback
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_LANGUAGE "English"
Function MyWelcomeShowCallback
SendMessage $mui.WelcomePage.Text ${WM_SETTEXT} 0 "STR:$(MUI_TEXT_WELCOME_INFO_TEXT)$\n$\nVersion: foo.bar"
FunctionEnd
Section
SectionEnd
..or add a new control:
outfile test.exe
requestexecutionlevel user
!include MUI2.nsh
!define MUI_PAGE_CUSTOMFUNCTION_SHOW MyWelcomeShowCallback
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_LANGUAGE "English"
Function MyWelcomeShowCallback
${NSD_CreateLabel} 120u 150u 50% 12u "Version: foo.bar"
Pop $0
SetCtlColors $0 "" "${MUI_BGCOLOR}"
FunctionEnd
Section
SectionEnd
I also had the issue with NSIS. In my case it worked to define MUI_WELCOMEPAGE_TITLE before inserting the macro MUI_PAGE_WELCOME.
It should look like:
!define MUI_WELCOMEPAGE_TITLE "CUSTOM TITLE HERE"
!insertmacro MUI_PAGE_WELCOME