Skip to content

Instantly share code, notes, and snippets.

@vpiotr
Last active May 16, 2019 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vpiotr/e6b64861c32fa25f1b5c3d37cc86265f to your computer and use it in GitHub Desktop.
Save vpiotr/e6b64861c32fa25f1b5c3d37cc86265f to your computer and use it in GitHub Desktop.
Sample NSIS script used in my desktop application
;--------------------------------------------------------------
;Sample NSIS script for MyDesktopApp
;
;Author: Piotr Likus, 2019
; https://github.com/vpiotr
;
;Initial script automatically created by Mihov NSIS Helper 3.2
; http://freeware.mihov.com
;
;Some patterns to be replaced:
; "MyDesktopApp" - name of application
; "Acme" - company or organization name
; "www.mysite.com" - product home page
; "mydesktopapp.exe" - executable name
; ".esf" - file extension to be associated with application
;
;Note:
;This sample may include some copy-pasted parts from other
;web sites.
;For more info see NSIS home page: https://nsis.sourceforge.io/
;--------------------------------------------------------------
!ifndef INST_LANGUAGE
!define INST_LANGUAGE "English"
!else
!define INST_LANGUAGE_PL
!endif
!define INST_SOURCE_DIR "p:\MyDesktopApp\Installer\Src\SourceData\${INST_LANGUAGE}"
!ifdef DISTRIB_TRIAL
!define PRODUCT_SELL_SUFF " (Trial)"
!else
!ifdef DISTRIB_BETA
!define PRODUCT_SELL_SUFF " (Beta)"
!else
!define PRODUCT_SELL_SUFF ""
!endif
!endif
!define PROD_NAME "MyDesktopApp"
!define PROD_PUBLISHER "Acme"
!define PROD_WEB_ADDRESS "http://www.mysite.com/mydesktopapp"
!define PROD_WEB_ADDRESS_SUPP "http://www.mysite.com/support"
!define PROD_FULL_NAME "Acme MyDesktopApp version ${PROD_VER}${PRODUCT_SELL_SUFF}"
!ifdef DISTRIB_BETA
!define REG_SUB_KEY "_Beta"
!define DISTRIB_MENU_SUFF " Beta"
!define DISTRIB_DIR_SUFF " Beta"
!define DISTRIB_INST_SUFF " Beta"
!define DISTRIB_SHORTCUT_SUFF " Beta"
!else
!define REG_SUB_KEY ""
!define DISTRIB_MENU_SUFF ""
!define DISTRIB_DIR_SUFF ""
!define DISTRIB_INST_SUFF ""
!define DISTRIB_SHORTCUT_SUFF ""
!endif
!define HKLM_PROD_REG_KEY "SOFTWARE\Acme\MyDesktopApp${REG_SUB_KEY}"
!define HKCU_PROD_REG_KEY "${HKLM_PROD_REG_KEY}"
;!include "MyDesktopApp_lang.nsh"
;!insertmacro RIS_INST_TYPES
;!insertmacro RIS_OPT_DESC
;InstType "${LTEXT_InstTypeStd}"
;InstType /COMPONENTSONLYONCUSTOM
!include "MUI.nsh"
Name "${PROD_FULL_NAME}"
OutFile "Output\${OUT_FNAME}"
InstallDir "$PROGRAMFILES\Acme MyDesktopApp${DISTRIB_DIR_SUFF}"
;Get install folder from registry for updates
InstallDirRegKey HKLM "${HKLM_PROD_REG_KEY}\InstInfo" "InstallDir"
!define MUI_WELCOMEFINISHPAGE_BITMAP "${NSISDIR}\Contrib\Graphics\Wizard\orange.bmp"
!define MUI_UNWELCOMEFINISHPAGE_BITMAP "${NSISDIR}\Contrib\Graphics\Wizard\orange-uninstall.bmp"
!define MUI_HEADERIMAGE
!define MUI_HEADERIMAGE_BITMAP "${NSISDIR}\Contrib\Graphics\Header\orange.bmp"
!define MUI_HEADERIMAGE_UNBITMAP "${NSISDIR}\Contrib\Graphics\Header\orange-uninstall.bmp"
!define MUI_ICON "${NSISDIR}\Contrib\Graphics\Icons\orange-install.ico"
!define MUI_UNICON "${NSISDIR}\Contrib\Graphics\Icons\orange-uninstall.ico"
!define MUI_ABORTWARNING
!define MUI_FINISHPAGE_TEXT_LARGE
!define MUI_FINISHPAGE_TEXT "${PROD_FULL_NAME} has been installed on your computer.\r\n\r\nClick Finish to close this wizard.\r\n\r\nNote: If you are installing this application for other users in Windows 2000 and above, you should start the application at least once before use by other users."
!define MUI_FINISHPAGE_RUN "$INSTDIR\MyDesktopApp.exe"
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_LICENSE "${INST_SOURCE_DIR}\License.rtf"
!insertmacro MUI_PAGE_COMPONENTS
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES
!insertmacro MUI_LANGUAGE "English"
;!insertmacro RIS_LANG_DEFS
ShowUninstDetails show
Section "!Program Files" SecCore
SectionIn RO
SetOutPath "$INSTDIR"
File "${INST_SOURCE_DIR}\MyDesktopApp.exe"
File "${INST_SOURCE_DIR}\MyDesktopApp.chm"
File "${INST_SOURCE_DIR}\readme.txt"
File "${INST_SOURCE_DIR}\license.txt"
File "${INST_SOURCE_DIR}\Acme Home Page.url"
File /r "${INST_SOURCE_DIR}\Templates"
File /r "${INST_SOURCE_DIR}\Scripts"
File /r "${INST_SOURCE_DIR}\Syntax"
File /r "${INST_SOURCE_DIR}\Config"
File /r "${INST_SOURCE_DIR}\Locale"
;Store install folder
WriteRegStr HKLM "Software\Acme\MyDesktopApp\InstInfo" "InstallDir" $INSTDIR
;Create uninstaller
WriteUninstaller "$INSTDIR\Uninst.exe"
Push "${INST_SOURCE_DIR}\MyDesktopApp.exe"
Call GetFileVersion
Pop $R0 ; $R0="1.1.0.12"
!define PROD_FULL_VERNO $R0
!define PROD_FULL_VER_DESC "${PROD_FULL_VERNO}${PRODUCT_SELL_SUFF}"
; Write the uninstall keys for Windows
Push "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Acme MyDesktopApp${DISTRIB_INST_SUFF}"
Pop $0
WriteRegStr HKLM $0 "DisplayName" "Acme MyDesktopApp ${PROD_VER}${PRODUCT_SELL_SUFF} (remove only)"
WriteRegStr HKLM $0 "UninstallString" "$INSTDIR\Uninst.exe"
WriteRegStr HKLM $0 "DisplayVersion" "${PROD_FULL_VER_DESC}"
WriteRegStr HKLM $0 "Publisher" "${PROD_PUBLISHER}"
WriteRegStr HKLM $0 "URLInfoAbout" "${PROD_WEB_ADDRESS}"
WriteRegStr HKLM $0 "HelpLink" "${PROD_WEB_ADDRESS_SUPP}"
WriteRegStr HKLM $0 "URLUpdateInfo" "${PROD_WEB_ADDRESS}"
WriteRegDWORD HKLM $0 "NoModify" 1
WriteRegDWORD HKLM $0 "NoRepair" 1
WriteUnInstaller "uninst.exe"
SectionEnd
Section "Start Menu Shortcuts" SecStartMenu
Call InitShortcutRoot
CreateDirectory "$SMPROGRAMS\Acme MyDesktopApp${DISTRIB_MENU_SUFF}"
CreateShortCut "$SMPROGRAMS\Acme MyDesktopApp${DISTRIB_MENU_SUFF}\MyDesktopApp.lnk" "$INSTDIR\mydesktopapp.exe" "" "$INSTDIR\mydesktopapp.exe" 0
CreateShortCut "$SMPROGRAMS\Acme MyDesktopApp${DISTRIB_MENU_SUFF}\MyDesktopApp Help.lnk" "$INSTDIR\MyDesktopApp.chm" "" "$INSTDIR\MyDesktopApp.chm" 0
CreateShortCut "$SMPROGRAMS\Acme MyDesktopApp${DISTRIB_MENU_SUFF}\Acme Home Page.lnk" "$INSTDIR\Acme Home Page.url" "" "$INSTDIR\Acme Home Page.url" 0
CreateShortCut "$SMPROGRAMS\Acme MyDesktopApp${DISTRIB_MENU_SUFF}\Uninstall MyDesktopApp.lnk" "$INSTDIR\uninst.exe" "" "$INSTDIR\uninst.exe" 0
CreateShortCut "$SMPROGRAMS\Acme MyDesktopApp${DISTRIB_MENU_SUFF}\ReadMe.txt.lnk" "$INSTDIR\readme.txt" "" "$INSTDIR\readme.txt" 0
SetShellVarContext current ; reset to current user
SectionEnd
Section "'Send To' shortcut" SecSendTo
Call InitShortcutRoot
CreateShortCut "$SENDTO\MyDesktopApp${DISTRIB_SHORTCUT_SUFF}.lnk" "$INSTDIR\mydesktopapp.exe" "" "$INSTDIR\mydesktopapp.exe" 0
SetShellVarContext current ; reset to current user
SectionEnd
Section "Quick Launch icon" SecQuickLaunch
Call InitShortcutRoot
CreateShortCut "$QUICKLAUNCH\MyDesktopApp${DISTRIB_SHORTCUT_SUFF}.lnk" "$INSTDIR\mydesktopapp.exe" "" "$INSTDIR\mydesktopapp.exe" 0
SetShellVarContext current ; reset to current user
SectionEnd
Section "'Open With MyDesktopApp' command" SecOpenWithMyApp
WriteRegStr HKCR "*\Shell\open_mydesktopapp" "" "Open with MyDesktopApp"
WriteRegStr HKCR "*\Shell\open_mydesktopapp\command" "" '"$INSTDIR\mydesktopapp.exe" "%1"'
SectionEnd
Section "Associate with .ESF files" SecAssocESF
; ---------- back up old value of .esf
!define Index "Line${__LINE__}"
ReadRegStr $1 HKCR ".esf" ""
StrCmp $1 "" "${Index}-NoBackup"
StrCmp $1 "MyDesktopAppEsf" "${Index}-NoBackup"
WriteRegStr HKCR ".esf" "backup_val" $1
"${Index}-NoBackup:"
WriteRegStr HKCR ".esf" "" "MyDesktopAppEsf"
ReadRegStr $0 HKCR "MyDesktopAppEsf" ""
StrCmp $0 "" 0 "${Index}-Skip"
WriteRegStr HKCR "MyDesktopAppEsf" "" "MyDesktopApp Script File"
WriteRegStr HKCR "MyDesktopAppEsf\shell" "" "open"
WriteRegStr HKCR "MyDesktopAppEsf\DefaultIcon" "" "$INSTDIR\mydesktopapp.exe,0"
"${Index}-Skip:"
WriteRegStr HKCR "MyDesktopAppEsf\shell\open\command" "" \
'"$INSTDIR\mydesktopapp.exe" "%1"'
WriteRegStr HKCR "MyDesktopAppEsf\shell\exec" "" "Execute Script"
Push "/e:'%1'"
Pop $0
WriteRegStr HKCR "MyDesktopAppEsf\shell\exec\command" "" \
'"$INSTDIR\mydesktopapp.exe" "$0"'
System::Call 'Shell32::SHChangeNotify(i 0x8000000, i 0, i 0, i 0)'
!undef Index
; ---------- Rest of script
; Call InitFileAssoc
SectionEnd
Section "Desktop icon" SecDesktop
Call InitShortcutRoot
CreateShortcut "$DESKTOP\MyDesktopApp${DISTRIB_SHORTCUT_SUFF}.lnk" "$INSTDIR\mydesktopapp.exe" "" "$INSTDIR\mydesktopapp.exe" 0
SetShellVarContext current ; reset to current user
SectionEnd
Section "Shortcuts for all users" SecForAllUsers
SectionIn 1 2
SectionEnd
!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
!insertmacro MUI_DESCRIPTION_TEXT ${SecCore} "Program files"
!insertmacro MUI_DESCRIPTION_TEXT ${SecStartMenu} "Start Menu shortcuts"
!insertmacro MUI_DESCRIPTION_TEXT ${SecDesktop} "An icon of MyDesktopApp on the desktop"
!insertmacro MUI_DESCRIPTION_TEXT ${SecSendTo} "Create a 'Send To' shortcut"
!insertmacro MUI_DESCRIPTION_TEXT ${SecQuickLaunch} "Create Quick Launch icon"
!insertmacro MUI_DESCRIPTION_TEXT ${SecForAllUsers} "Install shortcuts for all users"
!insertmacro MUI_DESCRIPTION_TEXT ${SecOpenWithMyApp} "Install 'Open With MyDesktopApp' command"
!insertmacro MUI_DESCRIPTION_TEXT ${SecAssocESF} "Associate MyDesktopApp with .ESF files"
!insertmacro MUI_FUNCTION_DESCRIPTION_END
Section "Uninstall"
;Delete shortcuts
Push $R7
ReadRegStr $R7 HKLM "${HKLM_PROD_REG_KEY}\InstInfo" "ForAllUsers"
StrCmp $R7 0 +2
SetShellVarContext all
Pop $R7
Delete $INSTDIR\uninst.exe
Delete $INSTDIR\mydesktopapp.exe
Delete $INSTDIR\readme.txt
Delete $INSTDIR\license.txt
Delete $INSTDIR\mydesktopapp.chm
Delete "$INSTDIR\Uninst.exe"
Delete "$INSTDIR\Acme Home Page.url"
RMDir /r "$INSTDIR\Scripts"
RMDir /r "$INSTDIR\Syntax"
RMDir /r "$INSTDIR\Templates"
RMDir /r "$INSTDIR"
; remove shortcuts, if any.
Delete "$SMPROGRAMS\Acme MyDesktopApp${DISTRIB_MENU_SUFF}\MyDesktopApp.lnk"
Delete "$SMPROGRAMS\Acme MyDesktopApp${DISTRIB_MENU_SUFF}\MyDesktopApp Help.lnk"
Delete "$SMPROGRAMS\Acme MyDesktopApp${DISTRIB_MENU_SUFF}\Acme Home Page.lnk"
Delete "$SMPROGRAMS\Acme MyDesktopApp${DISTRIB_MENU_SUFF}\Uninstall MyDesktopApp.lnk"
Delete "$SMPROGRAMS\Acme MyDesktopApp${DISTRIB_MENU_SUFF}\ReadMe.txt.lnk"
Delete "$DESKTOP\MyDesktopApp${DISTRIB_SHORTCUT_SUFF}.lnk"
Delete "$SENDTO\MyDesktopApp${DISTRIB_SHORTCUT_SUFF}.lnk"
Delete "$QUICKLAUNCH\MyDesktopApp${DISTRIB_SHORTCUT_SUFF}.lnk"
RMDir /r "$SMPROGRAMS\Acme MyDesktopApp${DISTRIB_MENU_SUFF}"
DeleteRegKey HKEY_LOCAL_MACHINE "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Acme MyDesktopApp${DISTRIB_INST_SUFF}"
DeleteRegKey HKCU "${HKCU_PROD_REG_KEY}"
DeleteRegKey /ifempty HKCU "Software\Acme"
DeleteRegKey HKLM "${HKLM_PROD_REG_KEY}"
DeleteRegKey /ifempty HKLM "Software\Acme"
DeleteRegKey HKCR "*\Shell\open_mydesktopapp"
;----- restore ESF file
!define Index "Line${__LINE__}"
ReadRegStr $1 HKCR ".esf" ""
StrCmp $1 "MyDesktopAppEsf" 0 "${Index}-NoOwn" ; only do this if we own it
ReadRegStr $1 HKCR ".esf" "backup_val"
StrCmp $1 "" 0 "${Index}-Restore" ; if backup="" then delete the whole key
DeleteRegKey HKCR ".esf"
Goto "${Index}-NoOwn"
"${Index}-Restore:"
WriteRegStr HKCR ".esf" "" $1
DeleteRegValue HKCR ".esf" "backup_val"
DeleteRegKey HKCR "MyDesktopAppEsf" ;Delete key with association settings
System::Call 'Shell32::SHChangeNotify(i 0x8000000, i 0, i 0, i 0)'
"${Index}-NoOwn:"
!undef Index
;--------- rest of script
SectionEnd
Function .onInstSuccess
Push $R7
SectionGetFlags ${SecForAllUsers} $R7
WriteRegStr HKLM "${HKLM_PROD_REG_KEY}\InstInfo" "ForAllUsers" $R7
Pop $R7
FunctionEnd
;---------------------------------------------------------------------------
Function GetFileVersion
Exch $0
Push $1
Push $2
Push $3
Push $4
Push $5
Push $6
GetDllVersion '$0' $1 $2
IfErrors error
IntOp $3 $1 / 0x00010000
IntOp $4 $1 & 0x0000FFFF
IntOp $5 $2 / 0x00010000
IntOp $6 $2 & 0x0000FFFF
StrCpy $0 '$3.$4.$5.$6'
goto end
error:
SetErrors
StrCpy $0 ''
end:
Pop $6
Pop $5
Pop $4
Pop $3
Pop $2
Pop $1
Exch $0
FunctionEnd
Function InitShortcutRoot
Push $R7
SectionGetFlags ${SecForAllUsers} $R7
StrCmp $R7 0 +2
SetShellVarContext all
Pop $R7
FunctionEnd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment