EP04 Scripting Reference (P6)

183 阅读3分钟

4.6 Sections

Each NSIS installer contains one or more sections. Each of these sections are created, modified, and ended with the following commands.

  • Each section contains zero or more instructions.
  • Sections are executed in order by the resulting installer, and if a component page is used, the user will have the option of disabling/enabling each visible section.
  • If a section's name is 'Uninstall' or is prefixed with 'un.', it's an uninstaller section.
  • 每个条款包含零个或多个指令。
  • 条款按结果安装程序的顺序执行,如果使用组件页面,用户可以选择禁用/启用每个可见的条款。
  • 如果一个条款的名称是Uninstall或以un.作为前缀,它是一个卸载程序条款。

4.6.1 Section Commands

4.6.1.1 AddSize

AddSize size_kb

Tells the installer that the current section needs an additional "size_kb" kilobytes of disk space. Only valid within a section (will have no effect outside of a section or in a function).

告诉安装程序当前部分需要额外的“size_kb”千字节的磁盘空间。只在区段内有效(条款外或函数内无效)。

Section
AddSize 500
SectionEnd

4.6.1.2 Section

Section [/o] [([!]|[-])section_name] [section_index_output]

Begins and opens a new section. If section_name is empty, omitted, or begins with a -, then it is a hidden section and the user will not have the option of disabling it. If the section name is 'Uninstall' or is prefixed with 'un.', then it is a an uninstaller section. If section_index_output is specified, the parameter will be !defined with the section index (can be used with SectionSetText etc). If the section name begins with a !, the section will be displayed as bold. If the /o switch is specified, the section will be unselected by default.

开始并打开一个新条款。如果section_name为空、省略或以-开头,那么它是一个隐藏的条款,用户将无法禁用它。如果条款名是Uninstall或以un.作为前缀,那么它就是一个卸载程序条款。如果指定了section_index_output,则参数将是使用!defined定义条款索引(可以使用SectionSetText等)。如果条款名以!开始,该条款将显示为粗体。如果指定了/o开关,默认情况下该区段将被取消选中。

Section "-hidden section"
SectionEnd

Section # hidden section
SectionEnd

Section "!bold section"
SectionEnd

Section /o "optional"
SectionEnd

Section "install something" SEC_IDX
SectionEnd

To access the section index, curly brackets must be used and the code must be located below the section in the script.

要访问片段索引,必须使用大括号,并且代码必须位于脚本中片段的下方。

Section test1 sec1_id
SectionEnd

Section test2 sec2_id
SectionEnd

Function .onInit
  SectionGetText ${sec2_id} $0
  MessageBox MB_OK "name of ${sec2_id}:$\n$0" # will correctly display 'name of 1: test2'
FunctionEnd
Function .onInit
  SectionGetText ${sec2_id} $0
  MessageBox MB_OK "name of ${sec2_id}:$\n$0" # will incorrectly display 'name of ${sec2_id}: test1'
    # plus a warning stating:
    #   unknown variable/constant "{sec2_id}" detected, ignoring
FunctionEnd

Section test1 sec1_id
SectionEnd

Section test2 sec2_id
SectionEnd

4.6.1.3 SectionEnd

This command closes the current open section.

4.6.1.4 SectionInstType

SectionInstType insttype_index [insttype_index [...]] [RO]

This command specifies which install types (see InstType) the current section defaults to the enabled state in. Multiple SectionInstType commands can be specified (they are combined). If you specify RO as a parameter, then the section will be read-only, meaning the user won't be able to change its state.

该命令指定当前部分默认为启用状态的安装类型(参见“InstType”)。可以指定多个SectionInstType命令(它们是组合的)。如果您指定RO作为参数,那么该节将是只读的,这意味着用户将不能更改其状态。

InstType "Full" IT_FULL
InstType "Minimal" IT_MIN

Section "Help"
SectionInstType ${IT_FULL} ${IT_MIN}
SectionEnd

Section "Bonus content"
SectionInstType ${IT_FULL}
SectionEnd

4.6.1.5 SectionIn

SectionIn insttype_index [insttype_index [...]] [RO]

Works like SectionInstType except that the first install type defined using InstType is indexed 1, the next 2 and so on.

SectionInstType类似,除了第一个使用InstType定义的安装类型索引为1,然后索引为2,以此类推。

4.6.1.6 SectionGroup

SectionGroup [/e] section_group_name [index_output]

This command inserts a section group. The section group must be closed with SectionGroupEnd, and should contain 1 or more sections. If the section group name begins with a !, its name will be displayed with a bold font. If /e is present, the section group will be expanded by default. If index_output is specified, the parameter will be !defined with the section index (can be used with SectionSetText etc). If the name is prefixed with 'un.' the section group is an uninstaller section group.

这个命令插入一个条款组。条款组必须用SectionGroupEnd关闭,并且应该包含1个或更多的条款。如果条款组名以!开始,其名称将以粗体显示。如果存在/e,默认情况下将展开条款组。如果指定了index_output,则参数将是使用!defined的条款索引(可以使用SectionSetText等)。如果名称以un.作为前缀。条款组是一个卸载程序的条款组。

SectionGroup "some stuff"
Section "a section"
SectionEnd
Section "another section"
SectionEnd
SectionGroupEnd

4.6.1.7 SectionGroupEnd

Closes a section group opened with SectionGroup.

4.6.2 Uninstall Section

A special Section named 'Uninstall' must be created in order to generate an uninstaller. This section should remove all files, registry keys etc etc that were installed by the installer, from the system. Here is an example of a simple uninstall section:

Section "Uninstall"
  Delete $INSTDIR\Uninst.exe ; delete self (see explanation below why this works)
  Delete $INSTDIR\myApp.exe
  RMDir $INSTDIR
  DeleteRegKey HKLM SOFTWARE\myApp
SectionEnd

The first Delete instruction works (deleting the uninstaller), because the uninstaller is transparently copied to the system temporary directory for the uninstall.

Note that in uninstaller code, $INSTDIR contains the directory where the uninstaller lies. It does not necessarily contain the same value it contained in the installer.

注意,在卸载程序代码中,$INSTDIR包含卸载程序所在的目录。它不一定包含与安装程序中包含的相同的值。