- NSIS Discussion
- Using variables as CreateShortCut arguments
Archive: Using variables as CreateShortCut arguments
Dark Ryder
26th March 2003 19:11 UTC
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?
Joost Verburg
26th March 2003 19:15 UTC
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:
Joel
26th March 2003 19:18 UTC
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
Dark Ryder
26th March 2003 19:20 UTC
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 Ryder
26th March 2003 19:22 UTC
Dark Boy, let me give that a shot. I hadn't thought of using defines...
Joost Verburg
26th March 2003 19:25 UTC
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.
Dark Ryder
26th March 2003 19:32 UTC
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!
kichik
26th March 2003 19:41 UTC
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
Dark Ryder
26th March 2003 20:13 UTC
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.
virtlink
28th March 2003 13:47 UTC
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?
Joost Verburg
28th March 2003 15:03 UTC
I see no reason to change that for only this command. It will also add size.