Reputation: 1822
How can I get the absolute path of a directory (not the current working directory) in node webkit application?
Example (Mac OS) - I have created a folder named A
in my Documents
. When I do getDirectory
with file system directory entry, I can only get dir.fullPath
which returns A
app.workspace.getDirectory(self.folderpath, {}, function(dir){
if(dir) console.log('Dir: ', dir);
});
But I need: ~/Documents/A/
|| c:\Users\Username\Documents
In my app users can choose/create a directory where ever they want and I store/read data from that folder.
Absolute path may not be what I need but I want to open files (PDF, doc...) with default desktop applications:
function getCommandLine() {
switch (process.platform) {
case 'darwin' : return 'open';
case 'win32' : return 'start';
case 'win64' : return 'start';
default : return 'xdg-open';
}
}
var exec = require('child_process').exec;
var filepath = '...';
//dir.fullPath will throw: The file /A/example.pdf does not exist.
var child = exec(getCommandLine() + ' ' + filepath, function (error, stdout, stderr) {
if (error) {
console.error(`exec error: ${error}`);
return;
}
});
child.on('close', function (e) {
console.log('E: ', e);
});
Upvotes: 3
Views: 2682
Reputation: 1822
Actually I recently found out (I can't believe I missed it out in the first place but), with chrome file system you can get "more readable" path info for display purposes.
Depending on what you are trying to do, you might not access the absolute location but at least you can see what it is...
fileSystem.getDirectory(path, {}, function(dir){
if(dir) console.log('This should return full path: ', dir.getDisplayPath());
var fullpath = dir.getDisplayPath();
//now with this, I can open any file I want via child process :)
var child = exec(getCommandLine() + ' ' + fullpath, function (error, stdout, stderr)
//.......
});
More info here regarding chrome file system.
Upvotes: 0
Reputation: 736
You cannot access the absolute path of a system's directory-file-everything from a web page for security reasons. Even if you try to access a specific file directory from a javascript script (for example), the browser will immediately block you.
A solution that might work for you is to retrieve the file that the user wants to open with a simple <input type="file">
, so that the user can select a file without giving to you its absolute path;after that you can save the file onto a server-host, or even the project's local directory, and then use the remote path to open it with the exec command.
Upvotes: 2
Reputation: 3077
Not sure I've understood thequestion. You can user __dirname to get the directory of the script you are executing. See: https://nodejs.org/docs/latest/api/globals.html#globals_dirname
And from there you can use relative paths to reference other files and folders.
Example folder structure:
/home/filippo/works/test/
├── subfolder
│ └── index.js
└── test.js
subfolder/index.js:
module.exports = {'folder': __dirname};
test.js:
var m = require("./subfolder/");
console.log("test.js running in: ", __dirname);
console.log("m module running in: ", m.folder);
Running the test:
$ node test.js
test.js running in: /home/filippo/works/test
m module running in: /home/filippo/works/test/subfolder
Is this what you are looking for?
Upvotes: 1