Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSIS - Run program on finish only if installed

I have a working NSIS Modern UI 2 script that has five components. One of them is the main application, and there are four helper applications. Due to the nature of the applications, none of them requires the other to run; hence, they are all optional installs. This includes the main application.

At the finish page, I can have an option to start the main application with

!define MUI_FINISHPAGE_RUN "$INSTDIR\MyProgram.exe"
!define MUI_FINISHPAGE_RUN_TEXT "Start the main program"

as long as that goes before the

!insertmacro MUI_PAGE_FINISH

command. However, I don't want the checkbox to be visible (or at least enabled) if the user doesn't install the main application.

I've tried putting the first two lines inside the Section MainSection, but it doesn't show the box because by then, the UI has already been created.

I'd prefer not to always have it enabled and point to a function that runs if it's been installed, and shows a MessageBox otherwise.

Is there a way to do this?

like image 620
wchargin Avatar asked Feb 18 '23 07:02

wchargin


1 Answers

Those MUI defines are used at compile time, you need to modify the checkbox at runtime:

!include LogicLib.nsh
!include MUI2.nsh
!insertmacro MUI_PAGE_COMPONENTS
!insertmacro MUI_PAGE_INSTFILES
!define MUI_FINISHPAGE_RUN "$instdir\Maybe.exe"
!define MUI_PAGE_CUSTOMFUNCTION_SHOW ModifyRunCheckbox
!insertmacro MUI_PAGE_FINISH
!insertmacro MUI_LANGUAGE English

Section "Maybe" SID_MAYBE
; File "Maybe.exe"
SectionEnd

Function ModifyRunCheckbox
${IfNot} ${SectionIsSelected} ${SID_MAYBE} ; You could also check if the file exists...
    SendMessage $mui.FinishPage.Run ${BM_SETCHECK} ${BST_UNCHECKED} 0
    EnableWindow $mui.FinishPage.Run 0 ; Or ShowWindow $mui.FinishPage.Run 0
${EndIf}
FunctionEnd
like image 70
Anders Avatar answered Feb 23 '23 13:02

Anders