NSIS 脚本的基本语法

来源:互联网 发布:网络空间 概念股 编辑:程序博客网 时间:2024/05/16 12:03
注释

单行注释用井号"#"或分号";",跨行注释用可以用c/C++中注释语法。

数据类型

数字
数字常量可以用十进制、十六进制(0x为前缀)、八进制(0为前缀)表示,颜色用类似html的中RGB表示法,但去井号"#"。

字符串
字符串常量可以用引号引用,转意字符用"$\"作前缀。美元符号、常用转意字符换行、回车、制表符的nsi语法表示分别为:$$,$\n,$\r,$\t

续行符
nsi脚本用行尾的反斜杠"\"表示下一行和当前行逻辑上是同一行

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

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

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

相对跳转
nsi脚本常常使用相对跳转表示条件分枝,其语法是[+-][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安装程序中最重要的界面元素,在nsi脚本中可以使用NSIS内置页面或者定制界面,通过脚本可以指定页面的顺序、显示样子和行为。 Page指令用来定义安装程序中的页面,UninstPage用来定义,此外PageEx指令提供类是功能,但提供更多选项。页面的顺序和它在nsi脚本中出现的次序一致。

示例:

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


规定安装程序首先显示license页面,然后显示components选择页面,接着显示安装目录选择页面。

页面选项

不同的页面有不同的选项:

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] [caption]

示例:
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] [caption]

OR

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

原创粉丝点击