Archive: Setting working directory on SM links


Setting working directory on SM links
I am new to NSIS and built a fairly extensive install script using the GUI installer. I got it all working under a deadline, now I want to know why a few things didn't work as I expected. The biggest issue I ran into was when I tried to create a start menu link or desktop link with a working directory pointing to the $INSTDIR. I found I could not create a link with a working directory different than where I was creating the link. I ended up creating a link in the install directory and then a link to that link on the start menu or desktop. Here is an exmaple of my workaround:


Section "Start Menu Shortcuts" SecStartMenu
SectionIn 1 2
SetOutPath "$INSTDIR"
CreateShortCut "$INSTDIR\Start Server.lnk" "$INSTDIR\start.bat"
CreateShortCut "$INSTDIR\Stop Server.lnk" "$INSTDIR\stop.bat"
SetOutPath "$SMPROGRAMS\${COMPANY}"
WriteINIStr "$SMPROGRAMS\${COMPANY}\${PRODUCT}.url" "InternetShortcut" "URL" "http://127.0.0.1/${COMPANY}/setup/edit.do?action=newJob"
CreateShortCut "$SMPROGRAMS\${COMPANY}\Start Server.lnk" "$INSTDIR\Start Server.lnk"
CreateShortCut "$SMPROGRAMS\${COMPANY}\Stop Server.lnk" "$INSTDIR\Stop Server.lnk"
SectionEnd

I couldn't find this mentioned anywhere in the forums but it would seem like something others would have run into. I tested this on Windows XP and it may work differently on W98 based on feedback I received from a W98 tester.

Is this the correct way to do this and will it work on all Windows platforms?

Thanks,

David Morris


CreateShortcut takes its current directory from the output directory ($OUTDIR) which is set by SetOutPath.


Hi kichik,

Maybe this is an error then. If I set $OUTDIR to anything other than the directory where I will be installing the shortcut the CreateShortCut does not work. Here is what I tried originally:

Section "Start Menu Shortcuts" SecStartMenu
SectionIn 1 2
SetOutPath "$INSTDIR"
WriteINIStr "$SMPROGRAMS\${COMPANY}\${PRODUCT}.url" "InternetShortcut" "URL" "http://127.0.0.1/${COMPANY}/setup/edit.do?action=newJob"
CreateShortCut "$SMPROGRAMS\${COMPANY}\Start Server.lnk" "$INSTDIR\start.bat"
CreateShortCut "$SMPROGRAMS\${COMPANY}\Stop Server.lnk" "$INSTDIR\stop.bat"
SectionEnd

This wouldn't run until I changed SetOutPath to "$SMPROGRAMS". That made my software break because the working directory was wrong. The work-around was the solution in my first post.

Thanks,

David Morris


What version of NSIS are you using?


I am using Alpha 7, but I just checked and see that Beta 0 is now posted so I will retry with that version and let you know if I see a problem.

Thanks again,

David Morris


I am having the same problem with the Beta 0 installer. Another issue cropped up in Beta 0 -- the MUI macros don't appear to be loading. For example, the MUI_DESCRIPTION_INIT macro is no longer working in this script. I also see the same thing with the MUI example script, which I used as a base for this script. Here is a test script that demonstrates the problem:

[edited by kichik. please attach large scripts. script attached below :down:]

Thanks,

David Morris


A lot of MUI macros have been changed from a7 to b0. Please read the the MUI readme and edit your script according to it if you want it to work with b0.

What working directory do you get with this script if not $INSTDIR? Or does it not create the shortcut at all?


Thanks, I'll take a look at the readme and fix the alpha 7 -> beta 0 problems. I bet the example.nsi script has the same problem. When I locate the problems in my script I will post the results if they apply to the MUI example script.

When I try to create the link nothing is written and I get an error in the log window after running the installer. I don't know enough about NSI to know how to get more detailed debugging messages, but I will see if I can find out how to do more logging.

Thanks,

David Morris


The old example file will surely have all of these problems, but the new files from 2.0b0 compile just fine with 2.0b0...

There is no way in NSIS to get detailed debugging messages.
Have you tried any other working directories but $INSTDIR? Try $TEMP for example and see if that works.


I did get my script fixed so that the tool tips and everything work with beta 0. Like you said, I just had to remove some of the internal functions and replace them with includes -- not too hard. The examples.nsi I downloaded in the examples/modern ui directory appears to be based on the alpha 7 version. The date on the examples file I downloaded as part of beta 0 is August 29th.

Using anything other than the the same directory fails. The following test does not run:

SetOutPath "$TEMP"
WriteINIStr "$SMPROGRAMS\${COMPANY}\${PRODUCT}.url" "InternetShortcut" "URL" "http://127.0.0.1/${COMPANY}/setup/edit.do?action=newJob"
CreateShortCut "$SMPROGRAMS\${COMPANY}\Start Server.lnk" "$INSTDIR\start.bat"
CreateShortCut "$SMPROGRAMS\${COMPANY}\Stop Server.lnk" "$INSTDIR\stop.bat"

This does work:

SetOutPath "$SMPROGRAMS\${COMPANY}"
WriteINIStr "$SMPROGRAMS\${COMPANY}\${PRODUCT}.url" "InternetShortcut" "URL" "http://127.0.0.1/${COMPANY}/setup/edit.do?action=newJob"
CreateShortCut "$SMPROGRAMS\${COMPANY}\Start Server.lnk" "$INSTDIR\start.bat"
CreateShortCut "$SMPROGRAMS\${COMPANY}\Stop Server.lnk" "$INSTDIR\stop.bat"

Thanks,

David Morris


I couldn't find this mentioned anywhere in the forums but it would seem like something others would have run into. I tested this on Windows XP and it may work differently on W98 based on feedback I received from a W98 tester.
Does differently means it's working on 98? I have XP here too and I have no problems with shortcuts creation... Can you please test this on some other computers too? Are you running the installer as an adminstrator or a normal user?

After trying the shortcut creation sample and having it work, I went back and modified my script to create the start menu directory before creating the start menu links. From my testing, it seems that if the working directory matches the start menu directory the create shortcut will create the directory if it doesn't exist. If they are not the same, you have to add an extra step to create the short cut directory. So the following works OK:

SetOutPath "$INSTDIR"
CreateDirectory "$SMPROGRAMS\${MUI_STARTMENU_VARIABLE}"
CreateShortCut "$SMPROGRAMS\${MUI_STARTMENU_VARIABLE}\Start Server.lnk" "$INSTDIR\start.bat"

Taking out the create directory causes this to fail unless SetOutPath specifies "$SMPROGRAMS\${MUI_STARTMENU_VARIABLE}"

I have not had a chance to go back and recreate this on W98, but I also had someone report a problem with the working directory on a W98 box. That same install definately worked on an XP box. I hope to be able to test this later today.

Thanks,

David Morris


Remove the contents of your old NSIS version before installing the latest snapshot. The Modern UI example.nsi has been renamed a long time ago.


I should have noticed that eariler :(

SetOutPath creates the directory too for ease of use. It's not CreateShortcut that creates the directory... Your script was only missing the directory creation command. A shortcut can't be created into a directory that doesn't exist... The last snippest you have posted it the way to go.