In Electron app how do you reference the temp path?
Asked Answered
T

1

7

Learning Electron I'd like to do some file processing after a drag and drop. On a Mac the equivalent for tmp is a $TMPDIR. Referencing the API documentation of app I was able to locate the app.getAppPath() which shows my path from a simple console log from main.js. Below app.getAppPath() there is getPath() but when I try app.getPath(temp):

let foobar = app.getAppPath("temp")
console.log(foobar)

I get an error in the console of:

ReferenceError: temp is not defined

Through my research I've read:

In Electron is there a built-in for the temp directory to work on all operating systems or a process to reference?

Note:

Even after referencing the string of:

console.log(`The temp path is: ${app.getAppPath("temp")}`)

it returns the same response as:

console.log(`The AppPath is: ${app.getAppPath()}`)

which is:

The temp path is: /Users/Grim/Documents/GitHub/electron-quick-start-boilerplate
The AppPath is: /Users/Grim/Documents/GitHub/electron-quick-start-boilerplate

and the above console.log tests have been added after letWindow.

Thisbe answered 5/3, 2019 at 14:34 Comment(0)
H
15

app.getAppPath() doesn't take an argument.

For app.getPath(name), the argument should be the string "temp": app.getPath("temp").

Heriberto answered 5/3, 2019 at 15:26 Comment(7)
So why does console.log(`The temp path is: ${app.getAppPath("temp")}`) return the same information as app.getAppPath() when added to main.js?Roer
I guess that getAppPath doesn't use any arguments you might provide to it, since its documentation says it takes no arguments. getAppPath() would return the same as getAppPath('temp'), same as getAppPath({something: 'useless'}), same as getAppPath(thisVariableCanHoldAnything).Heriberto
I'm a little confused then by the documentation of You can request the following paths by the name I was expecting the same behavior as $TMPDIR or $USERRoer
The method you need is getPath, not getAppPath. Then you pass it the name you want, in your case it's "temp" (as a string): console.log("tmp path is:", app.getPath("temp"))Heriberto
ah crap, you're right. Shouldnt code before coffee. +1 and accepted. I see what I was doing wrong now.Roer
Good morning and happy coffee, then! ;)Heriberto
Side note: when distributing your app, files will be put in an asar archive by default. This will make them inaccessible to shell scripts that take a file path, for instance wkhtmltopdf.Hygeia

© 2022 - 2024 — McMap. All rights reserved.