How to change the text box value Using NSIS Script? - nsis

I am new to NSIS Script and creating installers.
In My page there is a text box. While the page loading the text box show the value of reading the text from one text file. I want to know how to do this Using NSIS Scripts.. Need code samples...

Function .onInit
InitPluginsDir
FileOpen $0 "$pluginsdir\text.txt" w ; Normally the text file would come from a File command
FileWrite $0 "Hello World$\r$\nfrom$\r$\nNSIS$\r$\n" ; Add some dummy text
FileClose $0
FunctionEnd
Page Custom MyCustomPageInit
Page InstFiles
Var TxtCtl
!include nsDialogs.nsh
!include LogicLib.nsh
Function MyCustomPageInit
nsDialogs::Create 1018
Pop $0
!define MYMULTILINEEDIT_STYLE ${DEFAULT_STYLES}|${WS_TABSTOP}|${ES_AUTOHSCROLL}|${ES_MULTILINE}|${ES_READONLY}
nsDialogs::CreateControl EDIT ${MYMULTILINEEDIT_STYLE} ${WS_EX_WINDOWEDGE}|${WS_EX_CLIENTEDGE} 5% 10u 90% 50% ""
Pop $TxtCtl
FileOpen $0 "$pluginsdir\text.txt" r
loop:
FileRead $0 $1 ; Read a line
IfErrors eof
SendMessage $TxtCtl ${EM_REPLACESEL} 0 "STR:$1$\n"
Goto loop
eof:
FileClose $0
nsDialogs::Show
FunctionEnd

Related

NSIS Installer button skinning not working in custom page

I'm creating a custom page for my installer which I have the default buttons (back, next and cancel) hidden and only a custom button (install) is shown.
Problem is that, I'm not able to set the background color of the custom button using skinned controls plugin.
If I show the default buttons, I see the background color applied to them via the bitmap.
This means the skinning is getting applied for the default buttons, but not on the custom button.
Any pointers on what I would be missing ?
Here is the code:
!include "logiclib.nsh"
;--------------------------------
;Include Modern UI
!include "MUI2.nsh"
!include "InstallOptions.nsh"
;--------------------------------
;General
;Name and file
Name "Custom App"
OutFile "custom_installer.exe"
;Default installation folder
InstallDir "$LOCALAPPDATA\Custom Test"
RequestExecutionLevel user
;Remove default branding text of Nullsoft
;BrandingText " "
var title
var description
var button
Var ImageCtrl
Var BmpHandle
;--------------------------------
;Interface Settings
!define MUI_ABORTWARNING
!define Black "814EFA"
!define LightRed "FFFFFF"
!define MUI_CUSTOMFUNCTION_GUIINIT myGUIInit
XPStyle off
;--------------------------------
;Functions
Function .onInit
InitPluginsDir
; Extract bitmaps for buttons
File "/oname=$PLUGINSDIR\button.bmp" "${NSISDIR}\Contrib\SkinnedControls\skins\button_background.bmp"
FunctionEnd
Function myGUIInit
DetailPrint "myGUIInit"
; start the plugin
; the /disabledtextcolor, /selectedtextcolor and /textcolor parameters are optionnal
SkinnedControls::skinit /NOUNLOAD \
/disabledtextcolor=808080 \
/selectedtextcolor=000080 \
/textcolor=000000 \
"/button=$PLUGINSDIR\button.bmp"
FunctionEnd
Function Start
nsDialogs::Create 1044
Pop $0
SetCtlColors $0 222425 FBFBFB
SetCtlColors $HWNDPARENT 222425 FBFBFB
${NSD_CreateLabel} 0 150 100% 24u "Welcome to Custom App"
pop $title
SetCtlColors $title 0x000000 0xFBFBFB
${NSD_AddStyle} $title ${SS_CENTER}
CreateFont $0 "Arial" 24
SendMessage $title ${WM_SETFONT} $0 1
${NSD_CreateLabel} 0 190 100% 28u "By clicking install you agree to the terms of the End $\r$\n User License Agreement"
pop $description
SetCtlColors $description 0x000000 0xFBFBFB
${NSD_AddStyle} $description ${SS_CENTER}
CreateFont $0 "Arial" 14
SendMessage $description ${WM_SETFONT} $0 1
${NSD_CreateButton} 25 250 90% 15u INSTALL
pop $button
;SetCtlColors $button 0x000000 0x0FFFFF
${NSD_AddStyle} $button ${SS_CENTER}
GetDlgItem $0 $HWNDPARENT 3 ; Back Button
GetDlgItem $1 $HWNDPARENT 1 ; Next/Close Button
GetDlgItem $2 $HWNDPARENT 2 ; Cancel Button
;ShowWindow $0 ${SW_HIDE}
;ShowWindow $1 ${SW_HIDE}
;ShowWindow $2 ${SW_HIDE}
GetDlgItem $0 $HWNDPARENT 1035
ShowWindow $0 ${SW_HIDE}
GetDlgItem $0 $HWNDPARENT 1036
ShowWindow $0 ${SW_HIDE}
GetDlgItem $0 $HWNDPARENT 1045
ShowWindow $0 ${SW_HIDE}
GetDlgItem $0 $HWNDPARENT 1028
ShowWindow $0 ${SW_HIDE}
GetDlgItem $0 $HWNDPARENT 1256
ShowWindow $0 ${SW_HIDE}
nsDialogs::Show
${NSD_FreeBitmap} $BmpHandle
FunctionEnd
;--------------------------------
;Pages
Page custom Start
!insertmacro MUI_PAGE_COMPONENTS
;--------------------------------
;Languages
!insertmacro MUI_LANGUAGE "English"
;--------------------------------
;Installer Sections
Section "Dummy Section" SecDummy
SetOutPath "$INSTDIR"
SectionEnd
And here is the current UI (which clearly shows default buttons skinned):
This is a bug in Skinned Control plug-in you use, you need to fix it.
There is a chance some older version had this issue fixed, but your script is not working for me with older version for unknown reasons and I do not remember the details/versions exactly.
I am developer of Graphical Installer (http://www.graphical-installer.com/) mentioned in the Wiki page of that plug-in and I worked on the SkinnedControls plugin some time ago and fixed some issues. But that was almost 10 years ago.
As far as I remember there were some issues with this plug-in because
we wanted to use it in Graphical Installer (tool that allows to skin the whole installer).
However we could not solve all issues so we ended writing custom plugin where the buttons are working fine:
When I have time I will look at this plug-in, meanwhile you can open the project (it is Visual Studio 2008 C++ project I suppose) and debug it. The plugin should enumerate all controls, detect all Buttons (this is probably a problem) and skin them.

NSIS - Skip certain dialogs using command-line arguments?

Is there a way to make the NSIS installer skip certain dialogs?
It has these command-line arguments,
/S, /NCRC and /D=dir
Although /S and /NCRC can used for silent and unattended modes, is there are command-line arguments to make the installer skip certain dialogs in the installer and show the rest of the dialog? For example. skip the Welcome dialog and the next two dialogs and go to the fourth one.
/S, /NCRC and /D= are the only installer parameters with built-in support, anything else you have to handle yourself.
Pages can be skipped by calling Abort in the page pre callback. It is also possible to jump forward a specific number of pages. The GetOptions macro can be used to parse the command line.
OutFile Test.exe
RequestExecutionLevel user
InstallDir $Temp
!include LogicLib.nsh
!include FileFunc.nsh
Page License LicPre
Page Components CmpPre
Page Directory "" DiShow
Page InstFiles
Var SkippedL
Var SkippedC
!macro AbortIfCmdlineParam Switch Var
${GetParameters} $0
ClearErrors
${GetOptions} $0 "${Switch}" $0
${IfNot} ${Errors}
${If} ${Var} = 0
StrCpy ${Var} 1
Abort
${EndIf}
${EndIf}
!macroend
Function LicPre
!insertmacro AbortIfCmdlineParam "/SkipL" $SkippedL
FunctionEnd
Function CmpPre
!insertmacro AbortIfCmdlineParam "/SkipC" $SkippedC
FunctionEnd
Function DiShow
# Disable back button if both pages skipped, this is optional
${If} $SkippedL <> 0
${AndIf} $SkippedC <> 0
GetDlgItem $0 $hwndparent 3
EnableWindow $0 0
${EndIf}
FunctionEnd
Section
SectionEnd
Run as Test /SkipL /SkipC to skip both.
Or:
OutFile Test.exe
RequestExecutionLevel user
InstallDir $Temp
!include LogicLib.nsh
!include FileFunc.nsh
Page License "" LicShow
Page Components
Page Directory
Page InstFiles
Function LicShow
Var /Global HasSkipped
${GetParameters} $0
ClearErrors
${GetOptions} $0 "/Skip=" $0
${IfNot} ${Errors}
${AndIf} $0 < 4 ; Don't let user skip InstFiles
${AndIf} $HasSkipped = 0
StrCpy $HasSkipped 1
SendMessage $HWNDPARENT 0x408 $0 ""
${EndIf}
FunctionEnd
Section
SectionEnd
...and run as Test /Skip=2.

Add link to Abort page

As a follow-up to this answer, I'm trying to add the link after issuing Abort command, but for some reason it does not appear, no trace of it when viewed in Spy++.
The idea is to add the link above the progress bar, but somehow the macro does not work. Is there a reason for this that I'm missing and is it possible to add that link after calling Abort? I've read somewhere that Abort command can have different effect so I'm guessing this is one of it.
I've tried to make this sample script as concise as best I can and would greatly appreciate any help as I'm still learning NSIS.
!include "MUI2.nsh"
;--------------------------------
;General
ShowInstDetails hide
SetCompressor /SOLID lzma
;Request application privileges for Windows Vista
RequestExecutionLevel user
;--------------------------------
;Interface Configuration
!define MUI_ABORTWARNING
!define MANUAL_DOWNLOAD_TEXT "Automatic download not working? Click here to download manually."
;--------------------------------
;Macros
!macro AddDownloadLink yCoord
FindWindow $0 "#32770" "" $HWNDPARENT ; Find the inner dialog
System::Call 'USER32::CreateWindowEx(i0, t "STATIC", t "${MANUAL_DOWNLOAD_TEXT}", i${WS_CHILD}|${WS_VISIBLE}|${SS_NOTIFY}, i 1, i ${yCoord}, i 500, i 50, p $0, i 0x666, p 0, p 0)p.s'
Pop $0
SetCtlColors $0 0000ff transparent
CreateFont $1 "$(^Font)" "$(^FontSize)" "400" /UNDERLINE
SendMessage $0 ${WM_SETFONT} $1 1
GetFunctionAddress $1 fnLinkClicked
ButtonEvent::AddEventHandler 0x666 $1
!macroend
;--------------------------------
;Pages
!insertmacro MUI_PAGE_INSTFILES
;--------------------------------
;Languages
!insertmacro MUI_LANGUAGE "English"
;--------------------------------
;Installer Sections
Section
Var /global Filename
StrCpy $Filename "test100Mb.db"
Var /global DownloadUrl
StrCpy $DownloadUrl "http://speedtest.ftp.otenet.gr/files/$Filename"
!insertmacro AddDownloadLink 70
inetc::get /caption "Downloading package" $DownloadUrl "$Filename" /end
Pop $R0 ;Get the return value
StrCmp $R0 "OK" 0 dlfailed
Goto quit
dlfailed:
DetailPrint "Download failed: $R0 $DownloadUrl"
SetDetailsView show
Abort
!insertmacro AddDownloadLink 1
quit:
Quit
SectionEnd
Function fnLinkClicked
ExecShell "open" "$DownloadUrl"
FunctionEnd
Abort stops executing the section(s) code, you must do whatever you need to do before calling Abort.
Adding controls in a section can be problematic because it executes on a different thread and windows are tied to the thread that created them. If you need the window to stick around longer than the install thread you can create it as a hidden window in the instfiles page show callback and simply call ShowWindow in the section when you need to display it...

How to retain data on custom page when back/next button clicked when the data is taken from a file?

I have written a custom component page which is having a rich text box and that text box shows the information which is read from the "component.rtf" file. When I go first time on my custom page it shows me rich text box filled with data, but when I click next or back button and come back to custom page again at that time it shows me the rich text box as blank. It shows nothing.
I have written following code for my custom page-
;-------Custom page variables---------
Var Dialog
Var CustomHeaderText
Var CustomSubText
Var path
Var temp1
Var CONTROL
;-------------------------------------
Page custom nsDialogsPage
;------Custom page function----------
Function nsDialogsPage
StrCpy $CustomHeaderText "Components of My Installer"
StrCpy $CustomSubText "Detail list of components are"
!insertmacro MUI_HEADER_TEXT $CustomHeaderText $CustomSubText
!define SF_RTF 2
!define EM_STREAMIN 1097
nsDialogs::Create /NOUNLOAD 1018
Pop $Dialog
${If} $Dialog == error
Abort
${EndIf}
nsDialogs::CreateControl /NOUNLOAD "RichEdit20A" ${ES_READONLY}|${WS_VISIBLE}|${WS_CHILD}|${WS_TABSTOP}|${WS_VSCROLL}|${ES_MULTILINE}|${ES_WANTRETURN} ${WS_EX_STATICEDGE} 0 10u 100% 110u ''
Pop $CONTROL
FileOpen $4 "$path\components.rtf" r
StrCpy $0 $CONTROL
SendMessage $CONTROL ${EM_EXLIMITTEXT} 0 0x7fffffff
; set EM_AUTOURLDETECT to detect URL automatically
SendMessage $CONTROL 1115 1 0
System::Get /NoUnload "(i, i .R0, i .R1, i .R2) iss"
Pop $2
System::Call /NoUnload "*(i 0, i 0, k r2) i .r3"
System::Call /NoUnload "user32::SendMessage(i r0, i ${EM_STREAMIN}, i ${SF_RTF}, i r3) i.s"
loop:
Pop $0
StrCmp $0 "callback1" 0 done
System::Call /NoUnload "kernel32::ReadFile(i $4, i $R0, i $R1, i $R2, i 0)"
Push 0 # callback's return value
System::Call /NoUnload "$2"
goto loop
done:
System::Free $2
System::Free $3
FileClose $4
nsDialogs::Show
FunctionEnd
;--------Custom page function end------------
In above code it reads the file "components.rtf" and displays it. Can someone tell me how to write the code which will retain this data when I will click Back/Next button on component page.?
It works fine for me. Perhaps you are overwriting $path on another page? Add MessageBox MB_OK $4 after FileOpen to make sure you are able to open the file.
!include MUI2.nsh
Var Dialog
Var CustomHeaderText
Var CustomSubText
Var path
#Var temp1
Var CONTROL
;-------------------------------------
Page custom nsDialogsPage
;------Custom page function----------
Function nsDialogsPage
StrCpy $CustomHeaderText "Components of My Installer"
StrCpy $CustomSubText "Detail list of components are"
!insertmacro MUI_HEADER_TEXT $CustomHeaderText $CustomSubText
!define SF_RTF 2
!define EM_STREAMIN 1097
nsDialogs::Create /NOUNLOAD 1018
Pop $Dialog
${If} $Dialog == error
Abort
${EndIf}
nsDialogs::CreateControl /NOUNLOAD "RichEdit20A" ${ES_READONLY}|${WS_VISIBLE}|${WS_CHILD}|${WS_TABSTOP}|${WS_VSCROLL}|${ES_MULTILINE}|${ES_WANTRETURN} ${WS_EX_STATICEDGE} 0 10u 100% 110u ''
Pop $CONTROL
FileOpen $4 "$path\components.rtf" r
StrCpy $0 $CONTROL
SendMessage $CONTROL ${EM_EXLIMITTEXT} 0 0x7fffffff
; set EM_AUTOURLDETECT to detect URL automatically
SendMessage $CONTROL 1115 1 0
System::Get /NoUnload "(i, i .R0, i .R1, i .R2) iss"
Pop $2
System::Call /NoUnload "*(i 0, i 0, k r2) i .r3"
System::Call /NoUnload "user32::SendMessage(i r0, i ${EM_STREAMIN}, i ${SF_RTF}, i r3) i.s"
loop:
Pop $0
StrCmp $0 "callback1" 0 done
System::Call /NoUnload "kernel32::ReadFile(i $4, i $R0, i $R1, i $R2, i 0)"
Push 0 # callback's return value
System::Call /NoUnload "$2"
goto loop
done:
System::Free $2
System::Free $3
FileClose $4
nsDialogs::Show
FunctionEnd
Function .onInit
; Create a RTF file
InitPluginsDir
StrCpy $path "$PluginsDir" ; Set $path used by the custom page
FileOpen $0 "$path\components.rtf" w
FileWrite $0 '{\rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0\pard{Show {\b Me}}\par{http://example.com/#Funny}\par{Goodbye}}'
FileClose $0
FunctionEnd
!insertmacro MUI_PAGE_COMPONENTS
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_LANGUAGE "English"
Section
SectionEnd

How can I get a finish button after a nsDialogs page

I'm trying to create a post install configuration page in my nsis script using nsDialogs. My script for gathering the input and executing the configuration works however I'm never presented with a finish/close/exit button after I'm done. Currently my pages declaration looks like:
Page directory
Page instfiles
Page custom nsDialogsPage nsDialogsPageLeave
How can I get a finish/exit/done button to show after nsDialogsPageLeave executes?
The classic NSIS UI does not have a finish page, the instfiles page is usually the last page and it will show a "finish button" after all the sections have executed. You can set the text of any button to the same string with SendMessage $hwndButton ${WM_SETTEXT} 0 "STR:$(^CloseBtn)" if you want to provide your own finish page.
Most installers request the required information before the instfiles page, if you cannot do this then you might want to use the Modern UI, it will provide a finish page for you:
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
Page custom nsDialogsPage nsDialogsPageLeave
!insertmacro MUI_PAGE_FINISH
It was a little unclear to me if you wanted two pages; a input page and then a finish page or a combined input/finish page. A combined page is a little weird but it is possible:
!define AppName "Test"
Name "${AppName}"
Outfile "${AppName} setup.exe"
InstallDir $temp
!include LogicLib.nsh
!include WinMessages.nsh
!include nsDialogs.nsh
Var MyEndConfigPageStage
Page Directory
Page InstFiles
Page Custom MyEndConfigPageCreate MyEndConfigPageLeave /EnableCancel
Function MyEndConfigPageCreate
StrCpy $MyEndConfigPageStage 0
GetDlgItem $0 $hwndparent 1
SendMessage $0 ${WM_SETTEXT} 0 "STR:&Apply"
nsDialogs::Create 1018
Pop $0
${NSD_CreateCheckBox} 0 13u 100% -13u "FooBar"
Pop $1
nsDialogs::Show
FunctionEnd
Function MyEndConfigPageLeave
${If} $MyEndConfigPageStage > 0
Return
${EndIf}
${NSD_GetState} $1 $2
ClearErrors
WriteIniStr "$instdir\settings.ini" Dummy "FooBar" $2
${If} ${Errors}
MessageBox mb_iconstop "Unable to apply settings!"
Abort
${EndIf}
IntOp $MyEndConfigPageStage $MyEndConfigPageStage + 1
GetDlgItem $0 $hwndparent 1
SendMessage $0 ${WM_SETTEXT} 0 "STR:$(^CloseBtn)"
GetDlgItem $0 $hwndparent 2
EnableWindow $0 0 ;Disable cancel
EnableWindow $1 0 ;Disable the checkbox
Abort
FunctionEnd
Section
SectionEnd

Resources