NSIS Pick a file - file

what I want to do is have a page where the user browses there filesystem for a specific value, and continues on if the filename matches.
To be specific, I would like the user to locate there php executable file, and also the directory I suppose(Not sure how I would extract the directory from the full path).

You can do this with a custom page:
!include nsDialogs.nsh
!include FileFunc.nsh
Page Custom MyPageCreate MyPageLeave
Var PhpPath
Function MyPageLeave
${NSD_GetText} $PhpPath $0
${GetFileName} $0 $1
${IfNot} ${FileExists} $0
${OrIf} $1 != "php.exe"
MessageBox mb_iconstop "You must locate php.exe to continue!"
Abort
${Else}
#php path is in $0, do something with it...
${EndIf}
FunctionEnd
Function MyPageComDlgSelectPHP
Pop $0
${NSD_GetText} $PhpPath $0
nsDialogs::SelectFileDialog open $0 "php.exe|php.exe"
Pop $0
${If} $0 != ""
${NSD_SetText} $PhpPath $0
${EndIf}
FunctionEnd
Function MyPageCreate
nsDialogs::Create 1018
Pop $0
${NSD_CreateText} 0 5u -25u 13u "$ProgramFiles\PHP\php.exe"
Pop $PhpPath
${NSD_CreateBrowseButton} -23u 4u 20u 15u "..."
Pop $0
${NSD_OnClick} $0 MyPageComDlgSelectPHP
nsDialogs::Show
FunctionEnd
or you can use the directory page:
!include LogicLib.nsh
Var PhpPath
Function .onInit
StrCpy $PhpPath "$ProgramFiles\PHP" ; Default (You could probably do better by checking the registry)
FunctionEnd
PageEx Directory
DirVar $PhpPath
DirVerify leave
PageCallbacks "" PhpPageShow PhpPageLeave
DirText "Select PHP folder" "PHP Folder" "" "Select PHP folder"
PageExEnd
Function PhpPageShow
;Hide space texts
FindWindow $0 "#32770" "" $HWNDPARENT
GetDlgItem $1 $0 0x3FF
ShowWindow $1 0
GetDlgItem $1 $0 0x400
ShowWindow $1 0
FunctionEnd
Function PhpPageLeave
GetInstDirError $0
${If} $0 <> 0
${OrIfNot} ${FileExists} "$PhpPath\php.exe"
MessageBox mb_iconstop "You must locate the php folder to continue!"
Abort
${EndIf}
FunctionEnd

This can be achieved very easily with custom nsDialogs page and nsDialogs::SelectFileDialog which is designed for this purpose.
http://nsis.sourceforge.net/Docs/nsDialogs/Readme.html#ref-selectfiledialog

Related

Add each element of the array separately to the DIALOG command as a menu

I need to add each element of the array to the Dialog menu. The problem is that with the command $ {array [#]} the dialog is recognizing each word as an element. Look:
First attempt: Adding element with spaces
list=('Google Chrome' 'Brackets' 'Visual Studio') # my list
declare -a dia # dialog array
for index in ${!list[#]}; do # for each index in the list
dia[${#dia[#]}]=$index # add index
dia[${#dia[#]}]=${list[$index]} #add element
done
dialog --menu "MENU" 0 0 0 $(echo ${dia[#]})
# Format: dialog --menu "TITLE" 0 0 0 'Index1' 'Element1' 'Index2' 'Element2'...
# dia[0] = 0
# dia[1] = 'Google Chrome'
# dia[1] = 1
# dia[2] = 'Brackets'...
PRINT: first attempt
I did this to avoid processing each word, with the return of $ {list [#]} separating word by word, obtaining a sequence of number and string.
Second attempt: Replace ' ' with '-'
for index in ${!list[#]}; do
dgl[${#dgl[#]}]=$index
dgl[${#dgl[#]}]=${list[$index]/' '/'-'}
done
PRINT: Second attempt
What I think is happening
I believe that when passing the elements of the array to the DIALOG command it considers spaces ("Google Chrome", for example). Is there a way for me to show this with spaces?
Made all necessary changes to your code so it now works as expected.
#!/usr/bin/env bash
list=('Google Chrome' 'Brackets' 'Visual Studio') # my list
declare -a dia=() # dialog array
for index in "${!list[#]}"; do # for each index in the list
dia+=("$index" "${list[index]}")
done
choice=$(
dialog --menu "MENU" 0 0 0 "${dia[#]}" \
3>&1 1>&2 2>&3 3>&- # Swap stdout with stderr to capture returned dialog text
)
dialog_rc=$? # Save the dialog return code
clear # restore terminal background and clear
case $dialog_rc in
0)
printf 'Your choice was: %s\n' "${list[choice]}"
;;
1)
echo 'Cancelled menu'
;;
255)
echo 'Closed menu without choice'
;;
*)
printf 'Unknown return code from dialog: %d\n' $dialog_rc >&2
;;
esac

Getting a Windows command prompt contents to a text file

I want to write a batch utility to copy the output of a command prompt window to a file. I run my command prompt windows with the maximum depth of 9999 lines, and occasionally I want to grab the output of a command whose output is off-screen. I can do this manually with the keys Ctrl-A, Ctrl-Cand then pasting the result into Notepad - I just want to automate it in a batch file with a call to:
SaveScreen <text file name>
I know I can do it with redirection, but that would involve knowing that I will need to save the output of a batch command sequence beforehand.
So if I had a batch script:
call BuildPhase1.bat
if "%ErrorLevel% gtr 0 goto :ErrorExit
call BuildPhase2.bat
if "%ErrorLevel% gtr 0 goto :ErrorExit
call BuildPhase3.bat
if "%ErrorLevel% gtr 0 goto :ErrorExit
I could write:
cls
call BuildPhase1.bat
if "%ErrorLevel% gtr 0 call SaveScreen.bat BuildPhase1.err & goto :ErrorExit
call BuildPhase2.bat
if "%ErrorLevel% gtr 0 call SaveScreen.bat BuildPhase2.err & goto :ErrorExit
call BuildPhase3.bat
if "%ErrorLevel% gtr 0 call SaveScreen.bat BuildPhase3.err & goto :ErrorExit
or I could just type SaveScreen batch.log when I see that a run has failed.
My experiments have got me this far:
<!-- : Begin batch script
#cscript //nologo "%~f0?.wsf" //job:JS
#exit /b
----- Begin wsf script --->
<package>
<job id="JS">
<script language="JScript">
var oShell = WScript.CreateObject("WScript.Shell");
oShell.SendKeys ("hi folks{Enter}") ;
oShell.SendKeys ("^A") ; // Ctrl-A (select all)
oShell.SendKeys ("^C") ; // Ctrl-C (copy)
oShell.SendKeys ("% ES") ; // Alt-space, E, S (select all via menu)
oShell.SendKeys ("% EY") ; // Alt-space, E, Y (copy via menu)
// ... invoke a notepad session, paste the clipboard into it, save to a file
WScript.Quit () ;
</script>
</job>
</package>
My keystrokes are making it to the command prompt so presumably I have the correct window focused - it just seems to be ignoring the Ctrl and Alt modifiers. It also recognises Ctrl-C but not Ctrl-A. Because it has ignored the Ctrl-A to select all the text, the Ctrl-C causes the batch file to think it has seen a break command.
I've seen the other answers like this one but they all deal with methods using redirection, rather than a way of doing it after the fact "on demand".
* UPDATE *
On the basis of #dxiv's pointer, here is a batch wrapper for the routine:
Get-ConsoleAsText.bat
:: save the contents of the screen console buffer to a disk file.
#set "_Filename=%~1"
#if "%_Filename%" equ "" #set "_Filename=Console.txt"
#powershell Get-ConsoleAsText.ps1 >"%_Filename%"
#exit /b 0
The Powershell routine is pretty much as was presented in the link, except that:
I had to sanitise it to remove some of the more interesting character substitutions the select/copy/paste operation introduced.
The original saved the trailing spaces as well. Those are now trimmed.
Get-ConsoleAsText.ps1
# Get-ConsoleAsText.ps1 (based on: https://devblogs.microsoft.com/powershell/capture-console-screen/)
#
# The script captures console screen buffer up to the current cursor position and returns it in plain text format.
#
# Returns: ASCII-encoded string.
#
# Example:
#
# $textFileName = "$env:temp\ConsoleBuffer.txt"
# .\Get-ConsoleAsText | out-file $textFileName -encoding ascii
# $null = [System.Diagnostics.Process]::Start("$textFileName")
#
if ($host.Name -ne 'ConsoleHost') # Check the host name and exit if the host is not the Windows PowerShell console host.
{
write-host -ForegroundColor Red "This script runs only in the console host. You cannot run this script in $($host.Name)."
exit -1
}
$textBuilder = new-object system.text.stringbuilder # Initialize string builder.
$bufferWidth = $host.ui.rawui.BufferSize.Width # Grab the console screen buffer contents using the Host console API.
$bufferHeight = $host.ui.rawui.CursorPosition.Y
$rec = new-object System.Management.Automation.Host.Rectangle 0,0,($bufferWidth - 1),$bufferHeight
$buffer = $host.ui.rawui.GetBufferContents($rec)
for($i = 0; $i -lt $bufferHeight; $i++) # Iterate through the lines in the console buffer.
{
$Line = ""
for($j = 0; $j -lt $bufferWidth; $j++)
{
$cell = $buffer[$i,$j]
$line = $line + $cell.Character
}
$line = $line.trimend(" ") # remove trailing spaces.
$null = $textBuilder.Append($line)
$null = $textBuilder.Append("`r`n")
}
return $textBuilder.ToString()
The contents of the console buffer can be retrieved with the PS script from PowerShell's team blog Capture console screen mentioned in a comment, now edited into OP's question.
The last line could also be changed to copy the contents to the clipboard instead of returning it.
Set-Clipboard -Value $textBuilder.ToString()
As a side note, the reasons for using a StringBuilder rather than direct concatenation are discussed in How does StringBuilder work internally in C# and How the StringBuilder class is implemented.

How to customize NSIS checkbox in custom dialog?

How to customize NSIS checkbox in custom dialog using plugin or bitmap image?
Here is an example of NSIS installer with custom chechboxes
The current checkboxes working in options dialog only.
Maybe there's a way to do this by using OnClick event?
Any idea?
Thanks.
This is not a native NSIS feature but it can be implemented with some custom code:
!include WinMessages.nsh
!include nsDialogs.nsh ; WS_*
ShowInstDetails nevershow
Var Task1
Var Task2
Page InstFiles "" InitTasks FreeTasks
!macro SetTaskIcon hwnd icon
SetDetailsPrint none
Push $0
!if "${icon}" != ""
File "/oname=$PluginsDir\Task${hwnd}.ico" "${icon}"
System::Call 'USER32::LoadImage(i 0, t "$PluginsDir\Task${hwnd}.ico", i ${IMAGE_ICON}, i 32, i 32, i ${LR_LOADFROMFILE})i.r0'
SendMessage ${hwnd} ${STM_SETICON} $0 0 $0
!else
SendMessage ${hwnd} ${STM_GETICON} 0 0 $0
!endif
System::Call 'USER32::DestroyIcon(i $0)'
Pop $0
SetDetailsPrint lastused
!macroend
!macro FreeTask hwnd
!insertmacro SetTaskIcon ${hwnd} ""
!macroend
!macro CreateTask outvar icon text x y
System::Store S
FindWindow $1 "#32770" "" $HWNDPARENT
System::Call 'USER32::CreateWindowEx(i 0, t "Static", t "${text}", i ${WS_CHILD}|${WS_VISIBLE}|${SS_ICON}|${SS_REALSIZEIMAGE}, i ${x}, i ${y}, i 32, i 32, i r1, i 0, i 0, i 0)i.r0'
StrCpy ${outvar} $0
!insertmacro SetTaskIcon ${outvar} "${icon}"
IntOp $2 ${x} + 32
IntOp $2 $2 + 5 ; Padding
System::Call 'USER32::CreateWindowEx(i 0, t "Static", t "${text}", i ${WS_CHILD}|${WS_VISIBLE}|${SS_CENTERIMAGE}, i $2, i ${y}, i 999, i 32, i r1, i 0, i 0, i 0)i.r0'
SendMessage $1 ${WM_GETFONT} 0 0 $2
SendMessage $0 ${WM_SETFONT} $2 1
System::Store L
!macroend
Function InitTasks
!insertmacro CreateTask $Task1 "${NSISDIR}\Contrib\Graphics\Icons\llama-grey.ico" "Foo" 40 50
!insertmacro CreateTask $Task2 "${NSISDIR}\Contrib\Graphics\Icons\llama-grey.ico" "Bar" 40 90
FunctionEnd
Function FreeTasks
!insertmacro FreeTask $Task1
!insertmacro FreeTask $Task2
FunctionEnd
Section "Foo task"
Sleep 2222 ; Pretend to do some work
!insertmacro SetTaskIcon $Task1 "${NSISDIR}\Contrib\Graphics\Icons\llama-blue.ico"
SectionEnd
Section "Bar task"
Sleep 3333 ; Pretend to do some work
!insertmacro SetTaskIcon $Task2 "${NSISDIR}\Contrib\Graphics\Icons\llama-blue.ico"
SectionEnd

How to delete all the elements from the combo box in nsis

I want to delete all the elements from the combo box on click of the button in the nsis installer
nsDialogs does not have helper macros for every feature implemented by the Windows controls so sometimes you have to consult MSDN to find information about which message to send yourself.
!include nsDialogs.nsh
!include WinMessages.nsh
Page Custom MyPageCreate
Page InstFiles
Var MyComboHandle
Function ResetCombo
Pop $0
SendMessage $MyComboHandle ${CB_RESETCONTENT} 0 0
FunctionEnd
Function DeleteItemsButNotText
Pop $0
${NSD_GetText} $MyComboHandle $0
SendMessage $MyComboHandle ${CB_RESETCONTENT} 0 0
${NSD_SetText} $MyComboHandle $0
FunctionEnd
Function DeleteItemsButNotText_AlternateVersion
Pop $0
loop:
SendMessage $MyComboHandle ${CB_DELETESTRING} 0 0 $0 ; This will also clear the text if it matches the item
IntCmp $0 ${CB_ERR} "" loop loop
FunctionEnd
Function AddItems
Pop $0
${NSD_CB_AddString} $MyComboHandle "Foo"
${NSD_CB_AddString} $MyComboHandle "Bar"
SendMessage $MyComboHandle ${CB_SETCURSEL} 0 ""
FunctionEnd
Function MyPageCreate
nsDialogs::Create 1018
Pop $0
${NSD_CreateCombobox} 0 30u 100% 200u ""
Pop $MyComboHandle
Push ""
Call AddItems
${NSD_CreateButton} 0 50u 33% 12u "Reset"
Pop $0
${NSD_OnClick} $0 ResetCombo
${NSD_CreateButton} 33% 50u 33% 12u "Delete all items"
Pop $0
${NSD_OnClick} $0 DeleteItemsButNotText
${NSD_CreateButton} 66% 50u 33% 12u "Add items"
Pop $0
${NSD_OnClick} $0 AddItems
nsDialogs::Show
FunctionEnd

How to invoke the onclick event on combo box in nsis

I want to apply onClick event on the combo box in nsis installer,so that I can execute a functionality on the click of button on combo box.
The OnClick event is not suitable for a combo-box, you want the OnChange event because the user can change it with the arrow keys etc.
To fire the change event programmatically you can call your handler directly or trick the combobox control:
!include nsDialogs.nsh
!include WinMessages.nsh
Page Custom MyPageCreate
Page InstFiles
Function OnComboChange
Pop $0
SendMessage $0 ${CB_GETCURSEL} "" "" $2
System::Call 'USER32::SendMessage(i$0,i${CB_GETLBTEXT},i$2,t.r2)'
MessageBox mb_ok "OnComboChange: $2"
FunctionEnd
Function EmulateChangeMethodA
Pop $0 ; Throw away parameter we don't care about
Push $1
Call OnComboChange
FunctionEnd
Function EmulateChangeMethodB
Pop $0 ; Throw away parameter we don't care about
FindWindow $0 "EDIT" "" $1
SendMessage $1 ${WM_COMMAND} 0x4000000 $0 ; Send WM_COMMAND,MAKELONG(0,EN_UPDATE),hwndEdit
FunctionEnd
Function EmulateChangeMethodC
Pop $0 ; Throw away parameter we don't care about
!ifndef CB_GETCOMBOBOXINFO
!define CB_GETCOMBOBOXINFO 0x0164
!endif
System::Call '*(&l4,&i16,&i16,i,i,i,i)i.r2'
SendMessage $1 ${CB_GETCOMBOBOXINFO} "" $2 ; This only works on Vista+?
System::Call '*$2(i,&i16,&i16,i,i,i,i.r0)'
System::Free $2
SendMessage $1 ${WM_COMMAND} 0x30000 $0 ; ; Send WM_COMMAND,MAKELONG(0,LBN_SELCANCEL),hwndList
FunctionEnd
Function MyPageCreate
nsDialogs::Create 1018
Pop $0
${NSD_CreateCombobox} 0 30u 100% 200u ""
Pop $1 ; This is the only control handle we care about in this example so make sure to never overwrite it!
${NSD_CB_AddString} $1 "Foo"
${NSD_CB_AddString} $1 "Bar"
SendMessage $1 ${CB_SETCURSEL} 0 ""
${NSD_OnChange} $1 OnComboChange
${NSD_CreateButton} 0 50u 33% 12u "Emulate change: A"
Pop $0
${NSD_OnClick} $0 EmulateChangeMethodA
${NSD_CreateButton} 33% 50u 33% 12u "Emulate change: B"
Pop $0
${NSD_OnClick} $0 EmulateChangeMethodB
${NSD_CreateButton} 66% 50u 33% 12u "Emulate change: C"
Pop $0
${NSD_OnClick} $0 EmulateChangeMethodC
nsDialogs::Show
FunctionEnd

Resources