NSIS脚本的基本语法

注释:
单行注释用井号”#”或分号”;”,跨行注释用可以用c/C++中注释语法。
数据类型:

数字
数字常量可以用十进制、十六进制(0x为前缀)、八进制(0为前缀)表示,颜色用类似html的中RGB表示法,但去井号”#”。字符串
字符串常量可以用引号引用,转意字符用”$\”作前缀。美元符号、常用转意字符换行、回车、制表符的nsi语法表示分别为:$$,$\n,$\r,$\t续行符
NSIS脚本用行尾的反斜杠”\”表示下一行和当前行逻辑上是同一行

默认头文件
如果在makensis同目录下有nsisconf.nsh文件,该文件会被自动包含,除非编译时指定/NOCONFIG选项

标号
NSIS使用GOTO语句和IfErrors, MessageBox, IfFileExists及StrCmp进行程序控制流表示,标号是这些语句的目标语句。标号定义的语法:

标号语句
标号必须定义在函数和区段中,其作用范围仅限于定义它的区段或函数。以点号”.”开头的标号是全局标号。

相对跳转
NSIS脚本常常使用相对跳转表示条件分枝,其语法是[+-][1-9],加号表示从当前位置往前跳转,减号则表示从当前位置往后跳转。数字表示跳转的语句条数。示例:

Goto +4
MessageBox MB_OK “The following message will be skipped”
Goto +3
MessageBox MB_OK “You will never ever see this message box”
Goto -3
MessageBox MB_OK “Done”

页面:
向导页面是NSIS安装程序中最重要的界面元素,在NSIS脚本中可以使用NSIS内置页面或者定制界面,通过脚本可以指定页面的顺序、显示样子和行为。 Page指令用来定义安装程序
中的页面,UninstPage用来定义,此外PageEx指令提供类是功能,但提供更多选项。页面的顺序和它在nsi脚本 中出现的次序一致。
示例:

Page license
Page components
Page directory
Page instfiles
UninstPage uninstConfirm
UninstPage instfiles

页面选项:

不同的页面有不同的选项:
License page有LicenseText,LicenseData,LicenseForceSelection;
Components selection页面有ComponentText;
Directory selection页面有DirText,DirVar(仅能在PageEx中使用),DirVerify;
Un/Installation log页面有DetailsButtonText,CompletedText;
Uninstall confirmation页面有DirVar(仅能在PageEx中使用),UninstallText
对于内置的Page,NSIS支持三个回调函数用于定制界面和验证,对于自定义页面NSIS支持两个回调函数。

Page指令语法:

Page license|components|directory|instfiles|uninstConfirm) [pre_function] [show_function] [leave_function]
或者
Page custom [creator_function] [leave_function]

示例:

Page license skipLicense "" stayInLicense
Page custom customPage "" ": custom page"
Page instfiles

Function skipLicense
MessageBox MB_YESNO "Do you want to skip the license page?" IDNO no
Abort
no:
FunctionEnd

Function stayInLicense
MessageBox MB_YESNO "Do you want to stay in the license page?" IDNO no
Abort
no:
FunctionEnd

Function customPage
GetTempFileName $R0
File /oname=$R0 customPage.ini
InstallOptions::dialog $R0
Pop $R1
StrCmp $R1 "cancel" done
StrCmp $R1 "back" done
StrCmp $R1 "success" done
error: MessageBox MB_OK|MB_ICONSTOP "InstallOptions error:$\r$\n$R1"
done:
FunctionEnd

UninstPage指令语法:

UninstPage custom [creator_function] [leave_function]
或者
UninstPage (license|components|directory|instfiles|uninstConfirm) [pre_function] [show_function] [leave_function]

PageEx语法:
PageEx使用嵌套结构,比如:

PageEx license
LicenseText “Readme”
LicenseData readme.rtf
PageCallbacks licensePre licenseShow licenseLeave
PageExEnd

常用的nsi指令:

NSIS大致可以分为基本指令、注册表及ini操作指令、通用指令、流程控制指令、文件操作指令、卸载指令、字符串处理指令、多语言支持指令、重启指令。

以下是常用的基本指令:
Delete
Delete [/REBOOTOK] file
Exec
Exec command
ExecShell
ExecShell action command [parameters] [SW_SHOWNORMAL | SW_SHOWMAXIMIZED | SW_SHOWMINIMIZED | SW_HIDE]ExecShell “open” 示例”http://nsis.sf.net/”
ExecWait
ExecWait command [user_var(exit code)]

示例1:

ExecWait '"$INSTDIR\someprogram.exe"'
ExecWait '"$INSTDIR\someprogram.exe"' $0
DetailPrint "some program returned $0"

File
File [/nonfatal] [/a] ([/r] [/x file|wildcard […]] (file|wildcard) […] | /oname=file.dat infile.dat)
/r选项用作递归模式,/x用于排出文件

示例2:

File something.exe
File /a something.exe
File *.exe
File /r *.dat
File /r data
File /oname=$TEMP\temp.dat somefile.ext
File /nonfatal "a file that might not exist"
File /r /x CVS myproject
File /r /x *.res /x *.obj /x *.pch source

Rename
Rename [/REBOOTOK] source_file dest_file

RMDir
RMDir [/r] [/REBOOTOK] directory_name

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容