Skip to content
⌘ NSIS Forum Archive

A totally n00b question, if you please?

9 posts

DDoutelMS#

A totally n00b question, if you please?

Hi folks,

New to NSIS, and I have what no doubt is a totally dumb syntactical question.

If I have this definition of a constant:
!define PRODUCT_NAME "Flibbin"
Under what conditions would I need this construct, with the ${} to reference the value, as in:
Name "${PRODUCT_NAME}"
or this definition of a variable:
Var bInstallingForChrome
Under what conditions would I need the ${} construct to reference the variable, when I can do something like:
StrCpy $bInstallingForChrome "1"
So, purely a syntactical question; what purpose does the ${} construct serve in NSIS script?

Thanks in advance!
DDoutelMS
JasonFriday13#
Originally Posted by DDoutelMS View Post
Hi folks,

New to NSIS, and I have what no doubt is a totally dumb syntactical question.

If I have this definition of a constant:
!define PRODUCT_NAME "Flibbin"
Under what conditions would I need this construct, with the ${} to reference the value, as in:
Name "${PRODUCT_NAME}"
Always. !define is a compile time command. The only time you don't need ${} is when using the compiler commands (starts with !), so !undef PRODUCT_NAME is correct.

Originally Posted by DDoutelMS
or this definition of a variable:
Var bInstallingForChrome
Under what conditions would I need the ${} construct to reference the variable, when I can do something like:
StrCpy $bInstallingForChrome "1"
Never. Var is a runtime command, so you should always use $ to reference it. If you use ${}, you should get a compiler error about it not being defined.
JasonFriday13#
Forgot to mention that you can nest defines as well, like this:
!define bar "test"
!define foo "${bar} string"

Function .onInit
MessageBox MB_OK "${foo}" ; should show "test string" without quotes.
FunctionEnd
A more dynamic example:
Function .onInit
!define foo "first"
!define bar "second"
!define string "${foo},${bar}"
MessageBox MB_OK "${string}" ; should show "first,second" without quotes.
!undef string
!define string "${bar},${foo}"
MessageBox MB_OK "${string}" ; should show "second,first" without quotes.
FunctionEnd
DDoutelMS#
Originally Posted by JasonFriday13 View Post
Forgot to mention that you can nest defines as well, like this:
!define bar "test"
!define foo "${bar} string"

Function .onInit
MessageBox MB_OK "${foo}" ; should show "test string" without quotes.
FunctionEnd
A more dynamic example:
Function .onInit
!define foo "first"
!define bar "second"
!define string "${foo},${bar}"
MessageBox MB_OK "${string}" ; should show "first,second" without quotes.
!undef string
!define string "${bar},${foo}"
MessageBox MB_OK "${string}" ; should show "second,first" without quotes.
FunctionEnd
Ah, see, I think this is what's got me confused; I just don't know what the ${} actually does! Is there a "nutshell" answer for what purpose they serve? Toldja I'm a total NSIS n00b... 😛
DDoutelMS#
Originally Posted by Anders View Post
${define}

$variable

$(langstring)
So I read this as:

If I'm referencing the value of a constant, I should use ${}; if I'm referencing a variable, all I need is the $, and if I'm using a langstring, I need $(); is that correct?

Thanks again in advance!
DDoutelMS
LoRd_MuldeR#
You can think of ${foo} as something that will be replaced with a fixed string at the moment when your .nsi script file gets compiled to an .exe file. So, yes, it's pretty much a constant. Note that you can also pass the "/Dfoo=something" parameter to makensis.exe to set the value of ${foo}. So it's useful for things you need to set at compile time. It's also similar to the preprocessor defines in C and C++.

Conversely, $X is a variable. So its value can still be changed at the time when your installer is actually running, e.g. via StrCpy. NSIS has the variables $0 to $1 as well as $r1 to $r9 that are available for general usage. Pretty much like "registers" in assembly language. Furthermore, there are special variables like $DESKTOP and so on that NSIS will fill with the corresponding path at runtime. You can also define your own variables via var something. Last but not least, $(bar) is for translatable strings that will be loaded from language file.

See also:
DDoutelMS#
Originally Posted by LoRd_MuldeR View Post
You can think of ${foo} as something that will be replaced with a fixed string at the moment when your .nsi script file gets compiled to an .exe file. So, yes, it's pretty much a constant. Note that you can also pass the "/Dfoo=something" parameter to makensis.exe to set the value of ${foo}. So it's useful for things you need to set at compile time. It's also similar to the preprocessor defines in C and C++.

Conversely, $X is a variable. So its value can still be changed at the time when your installer is actually running, e.g. via StrCpy. NSIS has the variables $0 to $1 as well as $r1 to $r9 that are available for general usage. Pretty much like "registers" in assembly language. Furthermore, there are special variables like $DESKTOP and so on that NSIS will fill with the corresponding path at runtime. You can also define your own variables via var something. Last but not least, $(bar) is for translatable strings that will be loaded from language file.

See also:
http://nsis.sourceforge.net/Docs/Chapter4.html#varother
Excellent explanation! Thanks , LoRd_MuldeR, for taking the time!

DDoutelMS