4

I have a function that should open a directory after it was created,

setTimeout(function()
{
    var fs = require('fs');
    console.log(newPath);
    var open = fs.opensync(newPath, 'r');
}, 2500);

But this doesn't seem to work. I am getting the following errors

first is,

TypeError: undefined is not a function at eval (eval at <anonymous> (file:///Users/proslav/Library/Developer/Xcode/DerivedData/trackingCore-ecxfviftqracjxhimcuhhhvyddso/Build/Products/Debug/trackingCore.app/Contents/Resources/timeBroFront.app/Contents/Resources/app.nw/js/jquery-1.10.2.min.js:3:4994), :43:18)

and second is,

Uncaught ReferenceError: require is not defined

I was thinking that it could be that my variable newpath is undefinded but the log shows me the right link. The creation of the directory with var fs = require('fs'); works fine.

What am I doing wrong here?

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
Silve2611
  • 2,198
  • 2
  • 34
  • 55

2 Answers2

5

I found out how it has to be done. Node-webkit offers a function for that. It is working on MAC and should also work on windows. The function below is an example function. nw.gui and gui.Shell.showItemInFolder did the thing for me. Thx for the input.

/*---------
Open Folder
---------*/
function openFolder(path){
    var gui = require('nw.gui');
    gui.Shell.showItemInFolder(path);
}
Silve2611
  • 2,198
  • 2
  • 34
  • 55
1

In nw.js version 0.13 or later, use:

nw.Shell.showItemInFolder(fullpath);

Version < 0.13:

var gui = require('nw.gui');
gui.Shell.showItemInFolder(fullpath);

Note that the full path name is required. If it doesn't exist, it will fail silently.

If the path is something like c:\foo\bar.txt, it will open the folder foo and highlight the file bar.txt.

If the path is c:\foo\foo2, it will open the folder foo and highlight the folder foo2 (I expected it to open the folder foo2, but it will open the parent).


To find the fullpath of the running app, as we can't use node functions in our front-end (that's why you had the error trying to load the fs module), I've created a node module (utils.js) with the following:

exports.getFullPath = function(fileName) {
    var path = require('path');
    return path.resolve(__dirname, fileName);
}

In the front-end:

function openFolder(path) {
    var utils = require('./utils');
    var fullpath = utils.getFullPath(path);
    nw.Shell.showItemInFolder(fullpath);
}
Zanon
  • 29,231
  • 20
  • 113
  • 126