EP04 Scripting Reference (P2)

290 阅读5分钟

4.2 Variables

All variables are global and can be used in Sections or Functions. Note that by default, variables are limited to 1024 characters. To extend this limit, build NSIS with a bigger value of the NSIS_MAX_STRLEN build setting or use the special build.

所有变量都是全局的,可以在Sections或Functions中使用。

4.2.1 User Variables

$VARNAME

User variables must be declared with the Var command. You can use these variables to store values, work with string manipulation etc.

4.2.1.1 Var

Var [/GLOBAL] var_name

Declare a user variable. Allowed characters for variables names: [a-z][A-Z][0-9], . and _. All defined variables are global, even if defined in a section or a function. To make this clear, variables defined in a section or a function must use the /GLOBAL flag. The /GLOBAL flag is not required outside of sections and functions.

声明一个用户变量。允许的变量名字符[a-z][a-z][0-9]._。所有定义的变量都是全局的,即使是定义在节或函数中。为了明确这点,在条款或函数中定义的变量必须使用/GLOBAL标志。在条款和函数之外不需要/GLOBAL标志。

Var example

Function testVar
  Var /GLOBAL example2

  StrCpy $example "example value"
  StrCpy $example2 "another example value"
FunctionEnd

4.2.2 Other Writable Variables

$0, $1, $2, $3, $4, $5, $6, $7, $8, $9, $R0, $R1, $R2, $R3, $R4, $R5, $R6, $R7, $R8, $R9

Registers. These variables can be used just like user variables, but are usually used in shared functions or macros. You don't have to declare these variables so you won't get any name conflicts when using them in shared code. When using these variables in shared code it's recommended that you use the stack to save and restore their original values. These variables can also be used for communication with plug-ins because they can be read and written by the plug-in DLLs.

寄存器。这些变量可以像用户变量一样使用,但通常在共享函数或宏中使用。您不必声明这些变量,因此在共享代码中使用它们时不会产生任何名称冲突。当在共享代码中使用这些变量时,建议您使用堆栈来保存和恢复它们的原始值。这些变量还可以用于与插件的通信,因为它们可以由插件dll进行读写。

$INSTDIR

The installation directory ($INSTDIR is modifiable using StrCpy, ReadRegStr, ReadINIStr, etc. - This could be used, for example, in the .onInit function to do a more advanced detection of install location).

安装目录($INSTDIR是可以使用StrCpyReadRegStrReadINIStr等修改的)。

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. For example, if you write the uninstaller to $WINDIR and the user doesn't move it, $INSTDIR will be $WINDIR in the uninstaller. If you write the uninstaller to another location, you should keep the installer's $INSTDIR in the registry or an alternative storing facility and read it in the uninstaller.

注意,在卸载程序代码中,$INSTDIR包含卸载程序所在的目录。它不一定包含与安装程序中包含的相同的值。例如,如果你将卸载程序写到$WINDIR,而用户没有移动它,$INSTDIR将在卸载程序中成为$WINDIR。如果你将卸载程序写入另一个位置,你应该将安装程序的$INSTDIR保存在注册表或另一个存储工具中,并在卸载程序中读取它。

$OUTDIR

The current output directory (set implicitly via SetOutPath or explicitly via StrCpy, ReadRegStr, ReadINIStr, etc)

当前输出目录(通过SetOutPath隐式设置或通过StrCpy, ReadRegStr, ReadINIStr等显式设置)。

$CMDLINE

The command line of the installer. The format of the command line can be one of the following:

安装程序的命令行。

  • "full\path to\installer.exe" PARAMETER PARAMETER PARAMETER
  • installer.exe PARAMETER PARAMETER PARAMETER
  • For parsing out the PARAMETER portion, see GetParameters. If /D= is specified on the command line (to override the install directory) it won't show up in $CMDLINE.

$LANGUAGE

The identifier of the language that is currently used. For example, English is 1033. You can only change this variable in .onInit.

当前使用的语言的标识符。例如,英语是1033。你只能在.oninit中修改这个变量。

4.2.3 Constants

Constants can also be used in the InstallDir attribute.

常量也可以在InstallDir属性中使用。

Note that some of the new constants will not work on every OS. For example, $CDBURN_AREA will only work on Windows XP and above. If it's used on Windows 98, it'll be empty. Unless mentioned otherwise, a constant should be available on every OS.

注意,有些新常量并不适用于所有操作系统。

$PROGRAMFILES, $PROGRAMFILES32, $PROGRAMFILES64

The program files directory (usually C:\Program Files but detected at runtime). On 64-bit Windows, $PROGRAMFILES and $PROGRAMFILES32 point to C:\Program Files (x86) while $PROGRAMFILES64 points to C:\Program Files. Use $PROGRAMFILES64 when installing 64-bit applications.

程序文件目录(通常是C:\ program files,在运行时检测)。

$COMMONFILES, $COMMONFILES32, $COMMONFILES64

The common files directory. This is a directory for components that are shared across applications (usually C:\Program Files\Common Files but detected at runtime). On 64-bit Windows, $COMMONFILES and $COMMONFILES32 point to C:\Program Files (x86)\Common Files while $COMMONFILES64 points to C:\Program Files\Common Files. Use $COMMONFILES64 when installing 64-bit applications.

公共文件目录。用于跨应用程序共享的组件(通常是C:\Program Files\Common Files,在运行时检测)。

$DESKTOP

The Windows desktop directory. The context of this constant (All Users or Current user) depends on the SetShellVarContext setting. The default is the current user.

Windows桌面目录。这个常量的上下文(所有用户或当前用户)取决于SetShellVarContext设置。默认是当前用户。

$EXEDIR

The directory containing the installer executable (technically this is a variable and you can modify it, but it is probably not a good idea).

包含安装程序可执行文件的目录(从技术上讲,这是一个变量,您可以修改它,但这可能不是一个好主意)。

$EXEFILE

The base name of the installer executable.

安装程序可执行文件的基本名称。

$EXEPATH

The full path of the installer executable.

安装程序可执行文件的完整路径。

${NSISDIR}

A symbol that contains the path where NSIS is installed. Useful if you want to reference resources that are in NSIS directory e.g. Icons, UIs etc.

包含NSIS安装路径的符号。如果你想引用NSIS目录中的资源,例如图标,ui等,这很有用。

When compiled with support for keeping makensis and the data in the same place (the default on Windows), it is in the same place as makensis, on other platforms it is set at compile time (See the INSTALL file for info). In both instances you can modify it at runtime by setting the NSISDIR environment variable. See section 3.1.3 for more info.

$WINDIR

The Windows directory (usually C:\Windows or C:\WinNT but detected at runtime).

Windows目录(通常是C:\WindowsC:\WinNT,在运行时检测)。

$SYSDIR

The Windows system directory (usually C:\Windows\System or C:\WinNT\System32 but detected at runtime).

Windows系统目录(通常是C:\Windows\systemC:\WinNT\System32,在运行时检测)。

$TEMP

The temporary directory.

临时目录。

$STARTMENU

The start menu folder (useful for adding start menu items using CreateShortcut). The context of this constant (All Users or Current user) depends on the SetShellVarContext setting. The default is the current user.

开始菜单文件夹(用于使用CreateShortcut添加开始菜单项)。这个常量的上下文(所有用户或当前用户)取决于SetShellVarContext设置。默认是当前用户。

$SMPROGRAMS

The start menu programs folder (use this whenever you want $STARTMENU\Programs). The context of this constant (All Users or Current user) depends on the SetShellVarContext setting. The default is the current user.

开始菜单程序文件夹(当你想要$STARTMENU\programs时使用它)。这个常量的上下文(所有用户或当前用户)取决于SetShellVarContext设置。默认是当前用户。

$SMSTARTUP

The start menu programs / startup folder. The context of this constant (All Users or Current user) depends on the SetShellVarContext setting. The default is the current user.

开始菜单程序/启动文件夹。这个常量的上下文(所有用户或当前用户)取决于SetShellVarContext设置。默认是当前用户。

$QUICKLAUNCH

The quick launch folder for IE4 active desktop and above. If quick launch is not available it simply returns the same as $TEMP.

快速启动文件夹为IE4活动桌面和以上。如果快速启动不可用,它只返回与$TEMP相同的值。

$DOCUMENTS

The documents directory. A typical path for the current user is C:\Users\Foo\My Documents. The context of this constant (All Users or Current user) depends on the SetShellVarContext setting. The default is the current user.

文件目录。当前用户的典型路径是C:\Users\Foo\My Documents。这个常量的上下文(所有用户或当前用户)取决于SetShellVarContext设置。默认是当前用户。

This constant is not available on Windows 95 unless Internet Explorer 4 is installed.

$SENDTO

The directory that contains Send To menu shortcut items.

包含“发送到”菜单快捷方式项的目录。

$RECENT

The directory that contains shortcuts to the user's recently used documents.

包含用户最近使用的文档的快捷方式的目录。

$FAVORITES

The directory that contains shortcuts to the user's favorite websites, documents, etc. The context of this constant (All Users or Current user) depends on the SetShellVarContext setting. The default is the current user.

包含用户最喜欢的网站、文档等的快捷方式的目录。这个常量的上下文(所有用户或当前用户)取决于SetShellVarContext设置。默认是当前用户。

This constant is not available on Windows 95 unless Internet Explorer 4 is installed.

$MUSIC

The user's music files directory. The context of this constant (All Users or Current user) depends on the SetShellVarContext setting. The default is the current user.

用户的音乐文件目录。这个常量的上下文(所有用户或当前用户)取决于SetShellVarContext设置。默认是当前用户。

This constant is available on Windows ME, XP and above.

$PICTURES

The user's picture files directory. The context of this constant (All Users or Current user) depends on the SetShellVarContext setting. The default is the current user.

用户的图片文件目录。这个常量的上下文(所有用户或当前用户)取决于SetShellVarContext设置。默认是当前用户。

This constant is available on Windows 2000, XP, ME and above.

$VIDEOS

The user's video files directory. The context of this constant (All Users or Current user) depends on the SetShellVarContext setting. The default is the current user.

用户的视频文件目录。这个常量的上下文(所有用户或当前用户)取决于SetShellVarContext设置。默认是当前用户。

This constant is available on Windows ME, XP and above.

$NETHOOD

The directory that contains link objects that may exist in the My Network Places/Network Neighborhood folder.

该目录包含可能存在于“我的网络位置/网络邻居”文件夹中的链接对象。

This constant is not available on Windows 95 unless Internet Explorer 4 with Active Desktop is installed.

$FONTS

The system's fonts directory.

系统的字体目录。

$TEMPLATES

The document templates directory. The context of this constant (All Users or Current user) depends on the SetShellVarContext setting. The default is the current user.

文档模板目录。这个常量的上下文(所有用户或当前用户)取决于SetShellVarContext设置。默认是当前用户。

$APPDATA

The (roaming) application data directory.

应用程序数据目录。

This constant is not available on Windows 95 unless Internet Explorer 4 with Active Desktop is installed.

$LOCALAPPDATA

The local (non-roaming) application data directory. The context of this constant (All Users or Current user) depends on the SetShellVarContext setting. The default is the current user. The All Users location is also known as %ProgramData% on Vista and above.

本地应用程序数据目录。这个常量的上下文(所有用户或当前用户)取决于SetShellVarContext设置。默认是当前用户。

This constant is available on Windows ME, 2000 and above.

$PRINTHOOD

The directory that contains link objects that may exist in the Printers folder.

包含“打印机”文件夹中可能存在的链接对象的目录。

This constant is not available on Windows 95 and Windows 98.

$INTERNET_CACHE

Internet Explorer's temporary internet files directory.

Internet Explorer的临时Internet文件目录。

This constant is not available on Windows 95 nor Windows NT 4 unless Internet Explorer 4 with Active Desktop is installed.

$COOKIES

Internet Explorer's cookies directory.

Internet Explorer的cookie目录。

This constant is not available on Windows 95 nor Windows NT 4 unless Internet Explorer 4 with Active Desktop is installed.

$HISTORY

Internet Explorer's history directory.

Internet Explorer的历史目录。

This constant is not available on Windows 95 nor Windows NT 4 unless Internet Explorer 4 with Active Desktop is installed.

$PROFILE

The user's profile directory. A typical path is C:\Users\Foo.

用户的配置文件目录。一个典型的路径是C:\Users\Foo

This constant is available on Windows 2000 and above.

$ADMINTOOLS

A directory where administrative tools are kept. The context of this constant (All Users or Current user) depends on the SetShellVarContext setting. The default is the current user.

存放管理工具的目录。这个常量的上下文(所有用户或当前用户)取决于SetShellVarContext设置。默认是当前用户。

This constant is available on Windows 2000, ME and above.

$RESOURCES

The resources directory that stores themes and other Windows resources (usually $WINDIR\Resources but detected at runtime).

存储主题和其他Windows资源的资源目录(通常是$WINDIR\resources,在运行时检测)。

This constant is available on Windows XP and above.

$RESOURCES_LOCALIZED

The localized resources directory that stores themes and other Windows resources (usually $WINDIR\Resources\1033 but detected at runtime).

存放主题和其他Windows资源的本地化资源目录(通常是$WINDIR\resources\1033,在运行时检测)。

This constant is available on Windows XP and above.

$CDBURN_AREA

A directory where files awaiting to be burned to CD are stored.

一个存放等待刻录到CD的文件的目录。

This constant is available on Windows XP and above.

$HWNDPARENT

HWND of the main window (in decimal).

主窗口的HWND(十进制)。

$PLUGINSDIR

The path to a temporary folder created upon the first usage of a plug-in or a call to InitPluginsDir. This folder is automatically deleted when the installer exits. This makes this folder the ideal folder to hold INI files for InstallOptions, bitmaps for the splash plug-in, or any other file that a plug-in needs to work.

在第一次使用插件或调用InitPluginsDir时创建的临时文件夹路径。安装程序退出时将自动删除此文件夹。

$USER.. and $COMMON..

A handful of constants are available as aliases that are not affected by SetShellVarContext: $USERTEMPLATES, $USERSTARTMENU, $USERSMPROGRAMS, $USERDESKTOP, $COMMONTEMPLATES, $COMMONSTARTMENU, $COMMONSMPROGRAMS, $COMMONDESKTOP and $COMMONPROGRAMDATA.

一些作为不受SetShellVarContext影响的常量的别名。

4.2.4 Constants Used in Strings

$$

Use to represent $.

$\r

Use to represent a carriage return (\r).

$\n

Use to represent a newline (\n).

$\t

Use to represent a tab (\t).