Nullsoft Scriptable Install System

出自 ProgWiki
於 2012年6月11日 (一) 18:17 由 Player (對話 | 貢獻) 所做的修訂 (通過HotCat移除分類「軟體」及快速增加分類「安裝檔封裝工具」)

(差異) ←上個修訂 | 最新修訂 (差異) | 下個修訂→ (差異)
前往: 導覽搜尋

Nullsoft Scriptable Install System,參照:『維基百科~Nullsoft_Scriptable_Install_System』。

相關網站

用途

  • 打包安裝程式的工具。

範例Script

!include "MUI.nsh"
!include "Sections.nsh"
 
;---------------------------------------------------------------------
; 介面設定
Name "MyApp"
Caption "MyApp Setup"
!define MUI_ABORTWARNING
!define MUI_WELCOMEFINISHPAGE_BITMAP "Setup_MyApp.bmp"
!define MUI_ICON "install.ico"
!define MUI_UNICON "uninstall.ico"
!define MUI_COMPONENTSPAGE_SMALLDESC
 
; 輸出的安裝檔名
OutFile "Setup.exe"
 
!define CFG_SEC_DESKTOPICON
!define CFG_SEC_STARTMENUICON
!define MUI_FINISHPAGE_RUN "$INSTDIR\MyApp.exe"
 
; 預設的安裝目錄
InstallDir $PROGRAMFILES\MyApp
InstallDirRegKey HKLM Software\MyApp ""
 
!define MUI_WELCOMEPAGE_TITLE "歡迎進入MyApp安裝程式"
!define MUI_WELCOMEPAGE_TEXT "\r\n\r\n$_CLICK"
 
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
 
!define MUI_FINISHPAGE_LINK "http://www.myapp.idv.tw"
!define MUI_FINISHPAGE_LINK_LOCATION "http://www.myapp.idv.tw"
!define MUI_FINISHPAGE_NOREBOOTSUPPORT
 
!insertmacro MUI_PAGE_FINISH
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES
 
; 安裝介面的語系(繁體中文)
!insertmacro MUI_LANGUAGE "TradChinese"
 
;---------------------------------------------------------------------
; 安裝用
Section ""
 
	; 指定輸出的路徑與那些檔案需要被安裝?
	SetOutPath $INSTDIR
		File MyApp.ico
		File MyApp.exe
		File *.dll
 
	SetOutPath $INSTDIR\data
		File data\*.dat
 
	; 建立桌面的捷徑
	SetOutPath $INSTDIR
	CreateShortCut "$DESKTOP\MyApp.lnk" \
		"$INSTDIR\MyApp.exe" "" "$INSTDIR\MyApp.ico" 0
 
	; 建立"開始-程式集"裡面的捷徑
	CreateDirectory "$SMPROGRAMS\MyApp"
	CreateShortCut "$SMPROGRAMS\MyApp\MyApp.lnk" \
		"$INSTDIR\MyApp.exe\" "" "$INSTDIR\MyApp.ico" 0
	CreateShortCut "$SMPROGRAMS\MyApp\Uninstall.lnk" \
		"$INSTDIR\Uninstall.exe" "" "$INSTDIR\Uninstall.exe" 0
 
	; 建立移除用的執行檔
	WriteUninstaller $INSTDIR\Uninstall.exe
 
	WriteRegStr HKLM "Software\MyApp" "DestDir" "$INSTDIR"
	WriteRegStr HKLM "Software\MyApp" "RunFileName" "$INSTDIR\MyApp.exe"
	WriteRegStr HKLM "Software\MyApp" "version" "1.0"
	WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\MyApp" \
		"DisplayName" "MyApp(remove only)"
	WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\MyApp" \
		"UninstallString" "$INSTDIR\uninstall.exe"
 
SectionEnd
 
;---------------------------------------------------------------------
; 移除用
Section Uninstall
 
	; 刪除已安裝的檔案
	Delete "$INSTDIR\*.*"
	RMDir /r $INSTDIR\data
	RMDir $INSTDIR
 
	; 刪除安裝時建立的捷逕
	SetOutPath $INSTDIR
	Delete "$DESKTOP\MyApp.lnk"
	Delete "$SMPROGRAMS\MyApp\*.*"
	RMDir "$SMPROGRAMS\MyApp"
 
	; 刪除相關的登錄資訊
	DeleteRegKey HKLM "Software\MyApp"
	DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\MyApp"
 
SectionEnd
 
;---------------------------------------------------------------------
; 重覆安裝判別用
 
Function .onInit
 
	ReadRegStr $R0 HKLM \
		"Software\Microsoft\Windows\CurrentVersion\Uninstall\MyApp" \
		"UninstallString"
	StrCmp $R0 "" done
 
	MessageBox MB_OKCANCEL|MB_ICONEXCLAMATION \
		"您的電腦已有安裝過MyApp了。 \
		$\n$\n如果,你要移除上一次安裝的版本,請按『確定』按鈕。 \
		$\n如果,你要結束安裝程式,請按『取消』按鈕。" \
		IDOK uninst
	Abort
 
;跑移除程序
uninst:
	ClearErrors
 
	; 複製移除程式到 temp 的位置(避免發生移除時, 檔案使用中, 無法刪除)
	GetTempFileName $0
	CopyFiles $R0 $0
 
	; 開始跑移除
	ExecWait '$0 _?=$INSTDIR'
 
	IfErrors no_remove_uninstaller
		goto done
 
no_remove_uninstaller:
	MessageBox MB_ICONEXCLAMATION \
		"無法移除上一版的MyApp,請恰詢客服人員。"
	Abort
 
done:
	; 刪除已複製的移除程式
	Delete '$0'
 
FunctionEnd