Old 19th September 2014, 15:21   #1
DDoutelMS
Junior Member
 
Join Date: Sep 2014
Posts: 5
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:
code:
!define PRODUCT_NAME "Flibbin"


Under what conditions would I need this construct, with the ${} to reference the value, as in:
code:
Name "${PRODUCT_NAME}"


or this definition of a variable:
code:
Var bInstallingForChrome


Under what conditions would I need the ${} construct to reference the variable, when I can do something like:
code:
StrCpy $bInstallingForChrome "1"


So, purely a syntactical question; what purpose does the ${} construct serve in NSIS script?

Thanks in advance!
DDoutelMS
DDoutelMS is offline   Reply With Quote
Old 19th September 2014, 16:11   #2
JasonFriday13
Major Dude
 
JasonFriday13's Avatar
 
Join Date: May 2005
Location: New Zealand
Posts: 916
Quote:
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:
code:
!define PRODUCT_NAME "Flibbin"


Under what conditions would I need this construct, with the ${} to reference the value, as in:
code:
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.

Quote:
Originally Posted by DDoutelMS
or this definition of a variable:
code:
Var bInstallingForChrome


Under what conditions would I need the ${} construct to reference the variable, when I can do something like:
code:
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.

"Only a MouseHelmet will save you from a MouseTrap" -Jason Ross (Me)
NSIS 3 POSIX Ninja
Wiki Profile
JasonFriday13 is offline   Reply With Quote
Old 19th September 2014, 16:15   #3
DDoutelMS
Junior Member
 
Join Date: Sep 2014
Posts: 5
Thanks, Jason! I think that covers it! Much appreciated.

DDoutelMS
DDoutelMS is offline   Reply With Quote
Old 19th September 2014, 16:42   #4
JasonFriday13
Major Dude
 
JasonFriday13's Avatar
 
Join Date: May 2005
Location: New Zealand
Posts: 916
Forgot to mention that you can nest defines as well, like this:
code:
!define bar "test"
!define foo "${bar} string"

Function .onInit
MessageBox MB_OK "${foo}" ; should show "test string" without quotes.
FunctionEnd


A more dynamic example:
code:
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


"Only a MouseHelmet will save you from a MouseTrap" -Jason Ross (Me)
NSIS 3 POSIX Ninja
Wiki Profile
JasonFriday13 is offline   Reply With Quote
Old 19th September 2014, 18:01   #5
DDoutelMS
Junior Member
 
Join Date: Sep 2014
Posts: 5
Quote:
Originally Posted by JasonFriday13 View Post
Forgot to mention that you can nest defines as well, like this:
code:
!define bar "test"
!define foo "${bar} string"

Function .onInit
MessageBox MB_OK "${foo}" ; should show "test string" without quotes.
FunctionEnd


A more dynamic example:
code:
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 is offline   Reply With Quote
Old 19th September 2014, 18:26   #6
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,449
${define}

$variable

$(langstring)

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 19th September 2014, 18:39   #7
DDoutelMS
Junior Member
 
Join Date: Sep 2014
Posts: 5
Quote:
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
DDoutelMS is offline   Reply With Quote
Old 20th September 2014, 02:16   #8
LoRd_MuldeR
Major Dude
 
LoRd_MuldeR's Avatar
 
Join Date: Sep 2005
Location: Somewhere over the Slaughterhouse
Posts: 797
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

My Plugins: StdUtils | NSISList | CPUFeatures | ExecTimeout | KillProc
My source of inspiration: http://youtu.be/lCwY4_0W1YI
LoRd_MuldeR is offline   Reply With Quote
Old 22nd September 2014, 13:18   #9
DDoutelMS
Junior Member
 
Join Date: Sep 2014
Posts: 5
Quote:
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
DDoutelMS is offline   Reply With Quote
Reply
Go Back   Winamp & Shoutcast Forums > Developer Center > NSIS Discussion

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump