1. 程式人生 > >NSIS 指令碼的基本語法

NSIS 指令碼的基本語法

註釋

單行註釋用井號"#"或分號";",跨行註釋用可以用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