Archive: Using variables as CreateShortCut arguments


Using variables as CreateShortCut arguments
I'm working with a quick-and-dirty installer that creates a number of shortcuts with CreateShortCut. One of the sections I have set up gives the user the option of setting the SW_SHOWMAXIMIZED flag on all of them (a horrible, horrible abuse of the system, I know ;)), but was unsatisfied with my solution. Here's what I'm using:

Section "-Default"
StrCpy $9 ""
SectionEnd

Section "Maximize"
StrCpy $9 "max"
SectionEnd

Section "Desktop Shortcut"
StrCmp $9 "max" deskMax
CreateShortCut "$DESKTOP\App.lnk" "$INSTDIR\App.exe"
Goto deskNorm
deskMax:
CreateShortCut "$DESKTOP\App.lnk" "$INSTDIR\App.exe" "" "" "" SW_SHOWMAXIMIZED
deskNorm:
SectionEnd
It works perfectly well, but it's ugly and I have to end up creating jumps and labels for each of the dozen or so shortcuts involved. What I would much prefer is something like this:
Section "-Default"
StrCpy $9 "SW_SHOWNORMAL"
SectionEnd

Section "Maximize"
StrCpy $9 "SW_SHOWMAXIMIZED"
SectionEnd

Section "Desktop Shortcut"
CreateShortCut "$DESKTOP\App.link" "$INSTDIR\App.exe" "" "" "" $9
SectionEnd
Much cleaner, no? But it doesn't work. I get a bad argument error (it's interpreting $9 literally). Same when I put the variable name in quotes. Anybody got any ideas?

It won't work this way. This should work:

StrCmp $9 "value" "" done
...createshortcut with parameter 1...

StrCmp $9 "value2" "" done
...createshortcut with parameter 2...

done:


Maybe, just maybe if you are using the Modern UI with
the Startmenu page; the $9 is only for it.
Another thing *maybe* using defines:


!define status "SW_SHOWNORMAL"

Section "Maximize"
StrCpy ${status} "SW_SHOWMAXIMIZED"
SectionEnd

Section "Desktop Shortcut"
CreateShortCut "$DESKTOP\App.link" "$INSTDIR\App.exe" "" "" "" ${status}
SectionEnd


No, no -- the version using StrCmp is working. I just don't feel that's a good solution, and I'd like to be able to use a variable as an argument in CreateShortCut.


Dark Boy, let me give that a shot. I hadn't thought of using defines...


Defines are processed by the compiler and cannot be changed on run-time. StrCmp is the only way because the parameter is a compiler parameter.


Even so. StrCpy gives an argument error if you try copying to a define. Oh, well. And come to think of it, I'd bet that determining it at compile time is the only reasonable way to do it, too, because otherwise you'd have to include code for evaluating strings into the appropriate numerical value in the installer itself. Too bad. Thanks, everybody!


You can make your code a little prettier with a macro. For example:

!macro cs target loc
StrCmp $0 max +3
CreateShortcut "${target}" "${loc}"
Goto +2
CreateShortcut "${target}" "${loc}" "" "" "" SW_SHOWMAXIMIZED
!macroend


Thanks, kichik! Works just fine, once I RTFM ;). I haven't used NSIS in a long while (v1.4x, I think), so I keep forgetting about the new stuff like macros and relative jumps.


Hasn't it become a great scripting language since then? ;)

B.t.w. KiCHiK: wouldn't this problem be solved when SW_SHOWMAXIMIZED is not read as a switch, but as a string, performing escape and variable/symbol replace functions on it?


I see no reason to change that for only this command. It will also add size.