1
votes

I am working on a Windows desktop application. It is an Electron app and uses electron-builder to create the application package. NSIS configurations of the electron-builder are used to create the installer of the application.

I have programmed the installer to add keys to the registry, so that application could be launched from the Windows context menu when right clicked on the background of a folder.

But when the application is launched from the Windows context menu JavaScript error occurs.JavaScript Error image

However when the path of the installation directory is written in the environmental variables, application can be launched from the Windows context menu successfully (without any errors).

I would like to launch my Electron-app from the Windows context menu without writing to the environmental variables. Is there a way to do that ? Thanks in advance.

1
Don't use relative pathsAnders
@Anders Can you please explain your answer? I have not used relative path in installer script to add the keys to the registry. I am beginner in this area. Apologies if I misunderstood your answer.Saumya
Not in the installer, in your app. Explorer probably sets the working directory of your app to the path where the user clicked.Anders
@Anders Thank you. After some debugging found that JavaScript error occurs due to the reason you have mentioned. However application relies on relative paths and has to run from the installation directory. Is it possible to change the working directory of the executable that starts the process using electron? Thanks in advanceSaumya

1 Answers

1
votes

Applications should not rely on the current directory being the same as the directory the .exe is in, this is not why the current directory concept exists!

The current directory can be set to other paths by the shell (context menus, "open with", "send to", open associated file etc.), open/save common dialogs, shortcuts (.lnk), Start:Run and invocations from cmd.exe (call ..\myapp.exe etc).

Ideally you should fix your application by using a full path to the files you are loading. I assume Electron is able to tell you where its own .exe is located.

If you can't do that, you can use some sort of launcher that forces the current directory.

A batch file:

@echo off
setlocal ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
REM This will only work on Win2000 and later probably
cd /D "%~0\..\" 2>nul
pushd "%~0\..\"
"%~0\..\mybrokenapp.exe" %*

or a NSIS app:

OutFile applauncher.exe
RequestExecutionLevel user
SilentInstall silent
AutoCloseWindow true
!include "FileFunc.nsh"
Section
${GetParameters} $1
SetOutPath $EXEDIR
ExecWait '"$EXEDIR\mybrokenapp.exe" $1' $0
SetErrorLevel $0
Quit
SectionEnd

The problem with a launcher that changes the current directory is that it breaks callers that pass a relative path on the command line: applauncher.exe .\somefile.ext.