Elron
Elron

Reputation: 1455

Windows Folder Shortcut of an Electron App - get the path of that folder?

TL;DR

I want to open my Electron App through a Windows Folder Shortcut (right-click on any folder and click Run In My Electron App), and get the folder path that initiated the app.

I managed to create the shortcut using windows Registry, but I can't find a way to get the path of the folder that initiated the app. (in the shortcut, "Temporary")

Run in my electron app enter image description here

So in the above shortcut, the goal is to know the path of F:\ePC\Libraries\Videos\Loom\Temporary inside the electron app. (and the path should always be matching the folder that initiated the app).

A real-world working example: Just like when using WinRAR, you can right-click on a folder and click "Add to archive..." - the WinRAR app knows the path of the folder that has been right-clicked on. (Kind of like "open file with", but "open folder with").

Another real-world example that works with Electron is Visual Studio Code, you can right-click on a folder and click "Open With Code". How did they do that?

My Attempt

I tried to look over the documentation of Electron but only found a way to get the path of the EXE file, but not the folder that excecated that file using the Windows folder shortcut.

I tried also process.argv but it gives me the path of the exe file, and not the initiating folder.

Steps to reproduce the my attempt and test it yourself:

  1. Get the repository and clone it. (It only has 3 files: index.js, index.html and package.json).

  2. Open that folder in the command line and run npm install

  3. Export the electron app by running npm run package-win. this should export the exe file to [relative app path]/rlease-builds/windows-folder-electron-shortcut-win32-ia32/windows-folder-electron-shortcut.exe.

  4. To add the windows shortcut for the app: In the windows, registry create this:

HKEY_CLASSES_ROOT\Folder\shell\Run In My Electron App\command

Here's a step by step:

(In my case the default value is: C:\Users\elron\apps\windows-folder-electron-shortcut\rlease-builds\Paste Folder Icon-win32-ia32\Paste Folder Icon.exe. Make sure that the path is YOUR path of .exe file of the exported app.)

When done with step 4, It should look something like this:

enter image description here

Result:

When running the app through a folder shortcut, it will give you the path of the EXE as expected, but not the path that initiated the app as shown in this screenshot:

Run in my electron app

And now you can test it.

Any help will be appreciated!

Upvotes: 4

Views: 1831

Answers (1)

Nayeem Jahan Rafi
Nayeem Jahan Rafi

Reputation: 304

In the registry add this to your command value:

"C:\Users\elron\apps\windows-folder-electron-shortcut\rlease-builds\Paste Folder Icon-win32-ia32\Paste Folder Icon.exe"  "%1"

The "%1" will pass the initiating directory to your electron application.

You can extract the directory path in your electron application like this:

const exePath = process.argv[0];
const folderPath = process.argv[1];

Upvotes: 3

Related Questions