1

I have an electron app, that also operates on files in the same directory. These are not files the user selects, or files bundled inside the electron application, but I do need to reference them

for example:

  • /Users/test/Documents/myapp.app
  • /Users/test/Documents/example.zip

I need to know where that application is located, e.g.:

  • /Users/test/Documents

But instead when test.app is ran I get:

  • /Users/test/Documents/myapp.app/Contents/Resources/app/

Resulting in errors when it tries to access /Users/test/Documents/myapp.app/Contents/Resources/app/test.zip, or if I use a relative path, /test.zip.

How would I reliably access example.zip in this scenario? Note that I cannot bundle it with my app, it's being placed there by somebody else, so moving it is not an option, and it's a file I'm expecting to be there.

Tom J Nowell
  • 9,588
  • 17
  • 63
  • 91

4 Answers4

2

A friend and I looked up how Atom does it and arrived at this solution:

function getAppRoot() {
  if ( process.platform === 'win32' ) {
    return path.join( app.getAppPath(), '/../../../' );
  }  else {
    return path.join( app.getAppPath(), '/../../../../' );
  }
}
Tom J Nowell
  • 9,588
  • 17
  • 63
  • 91
0

I think either app.getPath('exe') or process.execPath is what you are looking for. They both give you the path of the currently used executable.

Edit: Look at the docs for app.getPath() to get the paths of all the different folder (e.g. home, appdata, documents, etc.)

RoyalBingBong
  • 1,106
  • 7
  • 15
  • process.execPath gives me the `...../test/test.app/Contents/Resources/app/` folder, as does `app.getPath('exe')`. If I were in my project folder and ran `electron .` it would give `..../test/` which is what i want, but I don't want any of this `test.app/Contents/Resources/app` stuff – Tom J Nowell Jun 08 '17 at 15:22
  • app.getPath( 'exe' ) returns the executable when on Windows with the .exe filename intact, and `process.execPath` is undefined – Tom J Nowell Jun 08 '17 at 16:04
  • I think you have to work around that output and use [path.join](https://nodejs.org/api/path.html#path_path_join_paths) to get exactly what you want. Only macOS has the slightly deeper app-structure, so you can just check for `process.platform == 'darwin'` and go up the directory four times. E.g. `path.join(app.getPath('exe'), '..', '..', '..', '..')`. It's not beautiful, but it works. – RoyalBingBong Jun 08 '17 at 16:20
0

Where are you running the Electron App? If it is running under 'Documents' (I am assuming Windows), you can pass in the documents to app.getPath like so: app.getPath('documents').

If you are calling this from the render process, you might have to import remote and do the following if you are in a render process remote.app.getPath('documents');.

This electron doc will be better at explaining the path options you can pass in.

unseen_damage
  • 1,346
  • 1
  • 14
  • 32
  • It could be anywhere, the goal of the question is to find that out, if I copy test.app to a folder X, how do I discover X from inside my app? – Tom J Nowell Jun 08 '17 at 20:09
  • So, based on what you are saying, you need to place the Electron app in ANY folder, and be able to access files in that current directory? Is this app running as an executable? Or are you simply running it as `npm run electron .`, since it will make a big difference in whether or not you can access files with a 'bundled/packaged' application. – unseen_damage Jun 09 '17 at 13:06
  • as an executable, I have no interest in the files inside the package/bundle, see the `getAppRoot` function in the answer I posted which does the job fine – Tom J Nowell Jun 09 '17 at 13:09
-2

You can use the fs.readdir function to list all the files that are in a particular directory (could be your app's or anywhere). You can make a directory from the code to put all the new files so it would be easier for you to list them out properly to process.

const testFolder = './tests/';
const fs         = require('fs');

fs.readdir(testFolder, (err, files) => {
    files.forEach(file => {
        console.log(file);
    });
});

How do you get a list of the names of all files present in a directory in Node.js?

Joshua
  • 5,032
  • 2
  • 29
  • 45
Aakash Uniyal
  • 1,519
  • 4
  • 17
  • 32
  • But I don't know which folder I'm in, which is what I'm trying to figure out in my question, where is my .app/.exe application located? – Tom J Nowell Jun 08 '17 at 11:49