Reputation: 11
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.
Upvotes: 1
Views: 685
Reputation: 101756
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
.
Upvotes: 1