最新消息:

NSIS 脚本学习

nsis 永恒心锁 1262浏览 0评论

使用NSIS作为软件打包发布安装包工具
可使用NSIS Edit作为脚本编辑工具

;———————————————
;CoXoo安装包脚本
;文件创建日期: 2005-08026
;文件作者: 周敏
;备注: 修改脚本请先阅读NSIS帮助和脚本注释
;———————————————

; ——————————————–
; 安装包信息
!define PRODUCT_NAME “CoXoo”
; 要修改安装包,快捷方式版本,请修改此常量
!define PRODUCT_VERSION “1.6.1”
!define PRODUCT_PUBLISHER “CoXoo.com”
!define PRODUCT_WEB_SITE “http://www.yhxs3344.net”

;———————————————
;引用的系统脚本
;Include Modern UI
!include “MUI.nsh”
!include “WinMessages.nsh”

;———————————————
;定义语言常量
!define ENG 1033
!define CHS 2052
!define CHT 1028
!define CHT_HongKong? 3076
!define CHS_Singapore 4100
!define CHT_Macao 5124

;———————————————
;General
;Name and file
Name “CoXoo”
OutFile “CoXoo-${PRODUCT_VERSION}-Setup.exe”

;Default installation folder
InstallDir “$PROGRAMFILES/CoXoo”

;———————————————–
;Interface Settings
!define MUI_ABORTWARNING

;———————————————–
;程序显示的页眉图片
!define MUI_HEADERIMAGE
!define MUI_HEADERIMAGE_BITMAP “files/headimage.bmp”
;欢迎页显示的图片
!define MUI_WELCOMEFINISHPAGE_BITMAP “files/welcomefinishpage.bmp”

;———————————————–
;安装显示图标及图片
!define MUI_ICON “files/CoXoo.ico”
;卸载图标
!define MUI_UNICON “files/uninst.ico”

;———————————————–
;定义多语言字符串
; 要加载的许可文件路径
LicenseLangString? SETUP_License_Path ${CHS} “files/LicenseCHS.rtf”
LicenseLangString? SETUP_License_Path ${CHT} “files/LicenseCHT.rtf”

; 要打包的访问coxoo主页文件路径
LangString ACCESS_Home_Path ${CHS} “files/coxoo.com_CHS.htm”
LangString ACCESS_Home_Path ${CHT} “files/coxoo.com_CHT.htm”

; 要打包的帮助文件路径
LangString Help_File_Path ${CHS} “files/CoXooHelp_CHS.chm”
LangString Help_File_Path ${CHT} “files/CoXooHelp_CHT.chm”

; 欢迎页面显示信息字符串
LangString WELCOME_Page_Text ${CHS} “这个向导将指引你完成 $(^Name) 的安装进程。/r/n/r/n在开始安装之前,建议你先关闭正在运行的 $(^Name) 程序。这将允许“安装程序”更新或者删除指定的 $(^Name) 程序文件。/r/n/r/n单击[下一步(N)]继续。”
LangString WELCOME_Page_Text ${CHT} “硂翾旧盢まЧΘ $(^Name) 杆秈祘/r/n/r/n秨﹍杆ぇ玡某闽超タ笲︽ $(^Name) 祘硂盢す砛杆祘穝┪埃﹚ $(^Name) 祘ゅン/r/n/r/n虫阑[˙(N)]膥尿”

;许可协议页面显示信息字符串
LangString LICENSE_RadTest_Accept ${CHS} “我接受$/”CoXoo 软件最终用户许可协议$/”中的条款”
LangString LICENSE_RadTest_Decline ${CHS} “我不接受$/”CoXoo 软件最终用户许可协议$/”中的条款”
LangString LICENSE_RadTest_Accept ${CHT} “и钡$/”CoXoo 硁砰程沧ノめ砛某$/”い兵蹿”
LangString LICENSE_RadTest_Decline ${CHT} “иぃ钡$/”CoXoo 硁砰程沧ノめ砛某$/”い兵蹿”

; 安装完成页面显示信息字符串
LangString FINISH_Page_Text ${CHS} “$(^Name) 已成功安装到你的系统。/r/n单击 [完成(F)] 关闭 $(^Name) 安装向导。”
LangString FINISH_Page_Text ${CHT} “$(^Name) Θ杆╰参/r/n虫阑 [ЧΘ(F)] 闽超 $(^Name) 杆翾旧”
LangString FINISH_Auto_Run ${CHS} “运行CoXoo”
LangString FINISH_Auto_Run ${CHT} “笲︽CoXoo”
LangString FINISH_Access_Home ${CHS} “访问www.coxoo.com首页”
LangString FINISH_Access_Home ${CHT} “砐拜www.coxoo.com”

; 写入注册表标记程序运行语言版本
LangString PROGRAM_Language ${CHS} “GB2312”
LangString PROGRAM_Language ${CHT} “BIG5”

; 检测到CoXoo已经运行的提示信息
LangString ProgramRunningPrompt ${CHS} “$(^Name) 安装程序检测到你的计算机上 $(^Name) 软件正在运行,请关闭正在运行的 $(^Name) 软件。”
LangString ProgramRunningPrompt ${CHT} “$(^Name) 杆祘浪代璸衡诀 $(^Name) 硁ンタ笲︽叫闽超タ笲︽ $(^Name) 硁ン”

; 检测到已安装CoXoo提示
LangString CheckPreInstPrompt ${CHS} “安装程序检测到您的计算机上存在以前安装的 $(^Name) 软件,是否卸载以前安装的 $(^Name) 软件并重新安装 $(^Name) 软件?”
LangString CheckPreInstPrompt ${CHT} “杆祘浪代眤璸衡诀玡杆 $(^Name) 硁ン琌更玡杆 $(^Name) 硁ン穝杆 $(^Name) 硁ン”

;———————————————–
;安装,卸载页面设置
; 欢迎页面显示信息
!define MUI_WELCOMEPAGE_TEXT $(WELCOME_Page_Text)

; 许可页面设置
!define MUI_LICENSEPAGE_RADIOBUTTONS
Section License_GUI_Setting
;协议许可选择按钮显示的说明字符
!define MUI_LICENSEPAGE_RADIOBUTTONS_TEXT_ACCEPT “$(LICENSE_RadTest_Accept)”
!define MUI_LICENSEPAGE_RADIOBUTTONS_TEXT_DECLINE “$(LICENSE_RadTest_Decline)”
SectionEnd

; 安装页面设置
; 去掉安装完成自动关闭功能
!define MUI_FINISHPAGE_NOAUTOCLOSE

; 安装完成页面设置
;运行软件复选框
!define MUI_FINISHPAGE_TEXT $(FINISH_Page_Text)
!define MUI_FINISHPAGE_RUN “$INSTDIR/${PRODUCT_NAME}.exe”
!define MUI_FINISHPAGE_RUN_TEXT $(FINISH_Auto_Run)

;访问首页复选框
!define MUI_FINISHPAGE_SHOWREADME “$INSTDIR/CoXoo.com.htm”
!define MUI_FINISHPAGE_SHOWREADME_NOTCHECKED
!define MUI_FINISHPAGE_SHOWREADME_TEXT $(FINISH_Access_Home)

; 卸载页面设置
; 去掉卸载完成自动关闭功能
!define MUI_UNFINISHPAGE_NOAUTOCLOSE

;———————————————–
;安装显示的页
Page custom Region_Check ;自定义页,检测安装包运行的操作系统区域
!define MUI_PAGE_CUSTOMFUNCTION_LEAVE PROGRAM_Running_Check
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_LICENSE $(SETUP_License_Path)
!define MUI_PAGE_CUSTOMFUNCTION_PRE CheckPreInstallation
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH

;卸载显示的页
!define MUI_PAGE_CUSTOMFUNCTION_LEAVE un.Uninstall_Check
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES
UninstPage custom un.DeleteAllFile_Show un.DeleteAllFile_Leave ;自定义页,删除所有文件
!insertmacro MUI_UNPAGE_FINISH

;———————————————
;安装程序窗口底部的文本
BrandingText “CoXoo.com”

;———————————————
;Languages
!insertmacro MUI_LANGUAGE “TradChinese”
!insertmacro MUI_LANGUAGE “SimpChinese”

;———————————————
;加载自定义界面
;Reserve Files
ReserveFile “files/RegionError.ini”
ReserveFile “files/DeleteAllFile.ini”
!insertmacro MUI_RESERVEFILE_INSTALLOPTIONS

;******************************安装脚本*********************************
;———————————————
;Installer Sections
Section “Dummy Section” SecDummy
SetOutPath $INSTDIR
;Create uninstaller
WriteUninstaller $INSTDIR/Uninstall.exe
SectionEnd

;将安装文件加入安装包
Section “MainSection” SEC01
SetOutPath “$INSTDIR”
SetOverwrite ifnewer

;CoXoo基本运行文件
File “files/CoXoo.exe”
StrCmp $LANGUAGE ${LANG_TRADCHINESE} 0 +3
File “/oname=coxoo.com.htm” “files/CoXoo.com_CHT.htm”
File “/oname=CoXoo.chm” “files/CoXoo_CHT.chm”
StrCmp $LANGUAGE ${LANG_SIMPCHINESE} 0 +3
File “/oname=coxoo.com.htm” “files/CoXoo.com_CHS.htm”
File “/oname=CoXoo.chm” “files/CoXoo_CHS.chm”

;CoXoo自动升级文件
File “files/AutoUpdate.exe”
File “files/CoXoo.ini”

;CoXoo使用的Dll
File “files/id3lib.dll”
File “files/msvbvm60.dll”
CreateDirectory “$INSTDIR/dll”
File “/oname=$INSTDIR/dll/Helper.dll” “files/dll/Helper.dll”
File “/oname=$INSTDIR/dll/Pluggable.dll” “files/dll/Pluggable.dll”

;创建开始菜单目录
CreateDirectory “$SMPROGRAMS/CoXoo”

; 创建开始菜单快捷方式
CreateShortCut “$SMPROGRAMS/CoXoo/CoXoo ${PRODUCT_VERSION}.lnk” “$INSTDIR/CoXoo.exe”
CreateShortCut “$SMPROGRAMS/CoXoo/www.coxoo.com.lnk” “$INSTDIR/coxoo.com.htm”
CreateShortCut “$SMPROGRAMS/CoXoo/CoXoo Help.lnk” “$INSTDIR/CoXoo.chm”

;创建桌面快捷方式
CreateShortCut “$DESKTOP/CoXoo ${PRODUCT_VERSION}.lnk” “$INSTDIR/CoXoo.exe”
SectionEnd

;———————————————
; 创建反安装快捷方式
Section -AdditionalIcons
CreateShortCut “$SMPROGRAMS/CoXoo/Uninstall.lnk” “$INSTDIR/Uninstall.exe”
SectionEnd

;———————————————
; 创建注册表项
Section “Registry”
WriteRegStr HKCU “Software/${PRODUCT_NAME}Client” “LANGUAGE” $(PROGRAM_Language)
WriteRegStr HKLM “Software/Microsoft/Windows/CurrentVersion/Uninstall/CoXoo” “DisplayName” “${PRODUCT_NAME}”
WriteRegStr HKLM “Software/Microsoft/Windows/CurrentVersion/Uninstall/CoXoo” “UninstallString” “$INSTDIR/Uninstall.exe”

; 将安装路径路径写入注册表
WriteRegStr HKCU “Software/${PRODUCT_NAME}Client” “MainFolder” “$INSTDIR/”
; 在注册表中写入安装时选择的语言编码
WriteRegDWORD HKLM “Software/Microsoft/Windows/CurrentVersion/Uninstall/CoXoo” “InstallLanguage” $LANGUAGE
SectionEnd

;———————————————
; 注册CoXoo运行需要Dll
Section AutoReg_Dll
RegDLL $INSTDIR/dll/Helper.dll
RegDll $INSTDIR/dll/Pluggable.dll
SectionEnd

;———————————————
;安装初始化函数
Function .onInit
!insertmacro MUI_INSTALLOPTIONS_EXTRACT_AS “files/RegionError.ini” “RegionError.ini”
;安装语言选择页面显示
!insertmacro MUI_LANGDLL_DISPLAY
FunctionEnd

;———————————————
; 检测已安装信息
Function CheckPreInstallation
StrCpy $0 0
loop:
EnumRegKey $1 HKLM “Software/Microsoft/Windows/CurrentVersion/Uninstall/” $0
StrCmp $1 “” exit +1
ReadRegStr $2 HKLM “Software/Microsoft/Windows/CurrentVersion/Uninstall/$1” “DisplayName”
StrCmp $2 “CoXoo” done +1
IntOp $0 $0 + 1
Goto loop

done:
ReadRegStr $3 HKLM “Software/Microsoft/Windows/CurrentVersion/Uninstall/$1” “UninstallString”
MessageBox MB_YESNO|MB_ICONQUESTION $(CheckPreInstPrompt) IDYES +1 IDNO +3
;Abort
ExecWait $3
exit:
FunctionEnd

;———————————————
; 检测CoXoo是否正在运行函数
Function PROGRAM_Running_Check
; 检测CoXoo是否正在运行
;MessageBox MB_OK “PROGRAM_Running_Check”
Push $0
FindWindow $0 “CoXooWindowClass”
IntCmp $0 0 +4 +4
MessageBox MB_OK|MB_ICONEXCLAMATION $(ProgramRunningPrompt)
Pop $0
Abort
FunctionEnd

;———————————————
; 自定义对话框函数
Function Region_Check ;FunctionName defined with Page command
;Display the Install Options dialog
Push $0
retry:
; 获取系统区域语言ID
System::Call “Kernel32::GetSystemDefaultLangID(v ..) i .s”
Pop $0
IntOp $0 $0 & 0xFFFF
;MessageBox MB_OK $0
; 判断获取的区域语言ID是否符合CoXoo运行要求
IntCmp $0 ${CHS} installcontinue +1 +1
IntCmp $0 ${CHT} installcontinue +1 +1
IntCmp $0 ${CHT_HongKong} installcontinue +1 +1
IntCmp $0 ${CHS_Singapore} installcontinue +1 +1
IntCmp $0 ${CHT_Macao} installcontinue +1 +1
Push $R0
; 提示用户更改区域语言设置
!insertmacro MUI_HEADER_TEXT “Warning” “Your region option and default language is NOT Chinese”
;!insertmacro MUI_INSTALLOPTIONS_INITDIALOG “RegionError.ini”
;!insertmacro MUI_INSTALLOPTIONS_SHOW
;!insertmacro MUI_INSTALLOPTIONS_SHOW_RETURN
!insertmacro MUI_INSTALLOPTIONS_DISPLAY_RETURN “RegionError.ini”
Pop $R0
;MessageBox MB_OK $R0
StrCmp $R0 “success” 0 +2
Goto retry
Pop $R0

installcontinue:
Pop $0
FunctionEnd

;******************************卸载脚本*********************************
;———————————————
;定义卸载语言字符串常量
LangString Page_DeleteAllFile_Text ${CHS} “注意”
LangString Page_DeleteAllFile_Text ${CHT} “猔種”
LangString Page_DeleteAllFile_SubText ${CHS} “请选择是否删除所有文件”
LangString Page_DeleteAllFile_SubText ${CHT} “叫匡拒琌埃┮Τ郎”
LangString Page_DeleteAllFile_Field1 ${CHS} “卸载程序发现CoXoo的安装目录下,有一些用户自己创建的文件夹。其中可能是用户的登录信息或已下载的文件。请您选择删除或保留这些内容。”
LangString Page_DeleteAllFile_Field1 ${CHT} “更祘Α祇瞷CoXoo杆ヘ魁Τㄇノめ承郎Жㄤい琌ノめ祅魁玹高┪更郎叫眤匡拒埃┪玂痙硂ㄇず甧”
LangString Page_DeleteAllFile_Field2 ${CHS} “把CoXoo安装目录下的内容全部删除”
LangString Page_DeleteAllFile_Field2 ${CHT} “рCoXoo杆ヘ魁ず甧场埃”
LangString Page_DeleteAllFile_Field3 ${CHS} “保留这些内容”
LangString Page_DeleteAllFile_Field3 ${CHT} “玂痙硂ㄇず甧”

LangString Delete_dll_Prompt1 ${CHS} “文件:”
LangString Delete_dll_Prompt1 ${CHT} “郎”
LangString Delete_dll_Prompt2 ${CHS} “正在被使用,不能成功卸载,请您关闭所有运行的程序或重新启动计算机后手动删除这些文件。”
LangString Delete_dll_Prompt2 ${CHT} “タ砆ㄏノぃΘ更叫眤闽超┮Τ笲︽祘Α┪穝币笆筿福も笆埃硂ㄇ郎”
;———————————————
;定义变量
Var HWND
Var DLGITEM
Var DELETE_DLL_EXCEPTION

;———————————————
; 删除安装的文件
Section “Uninstall”
SetOutPath $TEMP

; 反注册dll
UnRegDLL $INSTDIR/dll/Helper.dll
;UnRegDLL $INSTDIR/dll/IEHelper.dll
UnRegDLL $INSTDIR/dll/Pluggable.dll

; 删除注册表项
DeleteRegKey HKLM “Software/Microsoft/Windows/CurrentVersion/Uninstall/CoXoo”
;MessageBox MB_OK “SOFTWARE/$(^Name)Client”
DeleteRegKey HKCU “SOFTWARE/$(^Name)Client”

; 删除程序菜单项
Delete “$SMPROGRAMS/$(^Name)/*.*”
;MessageBox MB_OK “$SMPROGRAMS/$(^Name)”
RMDir “$SMPROGRAMS/$(^Name)”

; 删除桌面快捷方式
Delete “$DESKTOP/$(^Name)*.lnk”
;MessageBox MB_OK “$DESKTOP/$(^Name)*.lnk”

Delete “$INSTDIR/debuglog/*.*”
RMDir “$INSTDIR/debuglog”

;删除CoXoo基本文件
Delete “$INSTDIR/CoXoo*.*”
Delete “$INSTDIR/Uninstall.exe”

; 删除Dll
Delete “$INSTDIR/id3lib.dll”
Delete “$INSTDIR/msvbvm60.dll”
Delete “$INSTDIR/dll/Helper.dll”
Delete “$INSTDIR/dll/Pluggable.dll”

RMDir “$INSTDIR/dll”

; 删除Dll生成的文件
Delete “$INSTDIR/CmdFile.ini”

; 删除自动更新可执行文件
Delete “$INSTDIR/AutoUpdate.exe”
Delete “$INSTDIR/CoXoo.ini”

; 删除自动更新下载得到的文件
IfFileExists “$INSTDIR/UpdateConfig.ini” 0 +2
Delete “$INSTDIR/UpdateConfig.ini”
SectionEnd
;———————————————
;卸载初始化函数
Function un.onInit
ReadRegDWORD $LANGUAGE HKLM “Software/Microsoft/Windows/CurrentVersion/Uninstall/CoXoo” “InstallLanguage”
;!insertmacro MUI_UNGETLANGUAGE
IntCmp $LANGUAGE 2052 0 +2 +2
MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 “你确认要完全移除 $(^Name),及其所有的组件?” IDYES uninstall_run IDNO uninstall_abort
IntCmp $LANGUAGE 1028 0 +2 +2
MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 “絋粄璶Ч簿埃 $(^Name)のㄤ┮Τ舱ン” IDYES uninstall_run IDNO uninstall_abort

uninstall_abort:
Abort

uninstall_run:

;提取删除所有文件页面INI文件
!insertmacro MUI_INSTALLOPTIONS_EXTRACT_AS “files/DeleteAllFile.ini” “DeleteAllFile.ini”
FunctionEnd

Function un.onUninstSuccess
; 根据设置删除所有文件
Push $R0
!insertmacro MUI_INSTALLOPTIONS_READ $R0 “DeleteAllFile.ini” “Field 2” “State”
;MessageBox MB_OK $R0
IntCmp $R0 1 0 +2 +2
;MessageBox MB_OK “选择删除所有文件”
RMDir /r $INSTDIR ;递归删除安装目录
Pop $R0

; 检查Dll是否删除成功
StrCpy $DELETE_DLL_EXCEPTION “”
IfFileExists “$INSTDIR/dll/Helper.dll” 0 +2
StrCpy $DELETE_DLL_EXCEPTION “$INSTDIR/dll/Helper.dll,”
IfFileExists “$INSTDIR/dll/Pluggable.dll” 0 +2
StrCpy $DELETE_DLL_EXCEPTION “$DELETE_DLL_EXCEPTION$INSTDIR/dll/Pluggable.dll”
StrCmp $DELETE_DLL_EXCEPTION “” +4 0
; 提示用户
StrCpy $DELETE_DLL_EXCEPTION “$(Delete_dll_Prompt1) $DELETE_DLL_EXCEPTION”
StrCpy $DELETE_DLL_EXCEPTION “$DELETE_DLL_EXCEPTION $(Delete_dll_Prompt2)”
MessageBox MB_OK|MB_ICONINFORMATION $DELETE_DLL_EXCEPTION

;MessageBox MB_OK $INSTDIR
RMDir $INSTDIR
FunctionEnd

;卸载检测
Function un.Uninstall_Check
; 检测CoXoo是否正在运行
;MessageBox MB_OK “Call Uninstall_Check”
Push $0
FindWindow $0 “CoXooWindowClass”
IntCmp $0 0 +4? +4
MessageBox MB_OK|MB_ICONEXCLAMATION $(ProgramRunningPrompt)
Pop $0
Abort
FunctionEnd

;删除所有文件及目录页显示
Function un.DeleteAllFile_Show
Push $0
Push $1
!insertmacro MUI_HEADER_TEXT $(Page_DeleteAllFile_Text) $(Page_DeleteAllFile_SubText)
!insertmacro MUI_INSTALLOPTIONS_INITDIALOG “DeleteAllFile.ini”
;更改页显示的信息字符串
Pop $HWND
;MessageBox MB_OK $(Page_DeleteAllFile_SubText)
;MessageBox MB_OK $DLGITEM_TEXT
GetDlgItem $DLGITEM $HWND 1200
SendMessage $DLGITEM ${WM_SETTEXT} 0 ‘STR:$(Page_DeleteAllFile_Field1)’
GetDlgItem $DLGITEM $HWND 1201
SendMessage $DLGITEM ${WM_SETTEXT} 0 ‘STR:$(Page_DeleteAllFile_Field2)’
GetDlgItem $DLGITEM $HWND 1202
SendMessage $DLGITEM ${WM_SETTEXT} 0 ‘STR:$(Page_DeleteAllFile_Field3)’
FindFirst $0 $1 $INSTDIR/*.*
StrCmp $1 “.” 0 +1
FindNext $0 $1
StrCmp $1 “..” 0 +1
FindNext $0 $1
StrCmp $1 “” Page_Disable 0
!insertmacro MUI_INSTALLOPTIONS_SHOW

Page_Disable:

FindClose $0
Pop $1
Pop $0
FunctionEnd

;删除所有文件及目录页离开
Function un.DeleteAllFile_Leave

FunctionEnd

永恒心锁,版权所有丨如未注明,均为转载丨本网站采用BY-NC-SA协议进行授权

转载请注明:永恒心锁-分享互联网 » NSIS 脚本学习

您必须 登录 才能发表评论!