22

I'm trying to create an installer using NSIS Modern User Interface for the first time. I would like to know how I can add an option (checkbox) for users to select to have a desktop shortcut created on the Finish Page (the last screen of the installer) in addition to the "Run XXXX" option that's already there.

takoloco
  • 247
  • 1
  • 3
  • 9
  • 3
    Two pleas: (a) Don't check the "Put useless desktop icon" checkbox by default. The desktop is a far inferior program launching method in Vista and later compared to the search in the start menu. And (b) Make sure that the run checkbox causes the program to run not with adminsitrativ privileges but as the currently interactively logged-on user. – Joey Oct 04 '09 at 21:59

2 Answers2

41

If you are not using readme checkbox on the finish page, you can use it to perform whatever action you want:

Function finishpageaction
CreateShortcut "$desktop\foo.lnk" "$instdir\foo.exe"
FunctionEnd

!define MUI_FINISHPAGE_SHOWREADME ""
!define MUI_FINISHPAGE_SHOWREADME_NOTCHECKED
!define MUI_FINISHPAGE_SHOWREADME_TEXT "Create Desktop Shortcut"
!define MUI_FINISHPAGE_SHOWREADME_FUNCTION finishpageaction
!insertmacro MUI_PAGE_FINISH
Anders
  • 97,548
  • 12
  • 110
  • 164
  • 2
    Shouldn't we use MUI_FINISHPAGE_RUN, MUI_FINISHPAGE_RUN_TEXT, MUI_FINISHPAGE_RUN_FUNCTION, MUI_PAGE_FINISH instead? What is the difference between these and MUI_FINISHPAGE_SHOWREADME, etc.? – TechAurelian Mar 15 '12 at 13:33
  • @David There is no real difference other than the default text. – Anders Mar 15 '12 at 17:17
  • @Anders Can we add this code :- CreateShortCut "$DESKTOP\${MUI_PRODUCT}.lnk" "$INSTDIR\${MUI_FILE}.exe" "" in Function .onInstSuccess – Ankesh kumar Jaisansaria Apr 11 '19 at 19:54
  • @AnkeshkumarJaisansaria You can but if you are not giving the user a choice then you might as well do it in a `Section`. – Anders Apr 11 '19 at 22:58
  • How do you check whether the checkbox is checked or not? I want to use it to enable/disable telemetry... – tig Mar 24 '20 at 16:57
  • @tig the function is not executed if the checkbox is unchecked. You can inspect the state in the page leave callback function. – Anders Mar 24 '20 at 17:39
  • The NSIS scripts compiler is very sensitive to the order in which things are placed in the script. The code which Anders has shown us must be placed before the !insertmacro MUI_PAGE_FINISH statement or you get the error: NSIS install function not referenced - zeroing code. You may not even know you have the error because the installer runs, it's just that the finish page does not have the shortcut option.. – David Casper May 16 '20 at 07:21
  • @DavidCasper Per-page settings need to be defined before the insertmacro for the specific page. The function can be anywhere. The MUI documentation tells you which defines are global and which are per-page. – Anders May 16 '20 at 12:03
11

An alternate, and the simplest way to allow the user to add a desktop icon is to create a custom Section that does it. The user can then choose to add the shortcut in the "features" page of the installer and you don't have to do heavy modifications of the UI.

Section "Desktop Shortcut" SectionX
    SetShellVarContext current
    CreateShortCut "$DESKTOP\Your Program.lnk" "$INSTDIR\YourProgram.exe"
SectionEnd
Julien Lebosquain
  • 40,639
  • 8
  • 105
  • 117
  • As this puts the option in an unusual, unexpected and often simply skipped section of the installer, I would recommend against doing it like this. Asking whether the user wants a shortcut to be placed is expected to be at the end of the installation process, for whatever reason. – J. Stoever Mar 06 '11 at 16:36
  • Generally, it is expected at the end of the installation, because it is a minor detail that can be completely skipped. Desktop shortcuts are _convenient, but not necessary_. They are not required in order to install the application. Keeping these kinds of options at the _end_ of the installation process helps make the user feel like more is getting done with less effort involved. – jay_t55 Jun 17 '13 at 08:48