简体   繁体   English

在Node.js和node-webkit中打开目录

[英]Open Directory in Node.js and node-webkit

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) TypeError:undefined不是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 未捕获的ReferenceError:未定义require

I was thinking that it could be that my variable newpath is undefinded but the log shows me the right link. 我想可能是我的变量newpath未定,但日志显示我正确的链接。 The creation of the directory with var fs = require('fs'); var fs = require('fs');创建目录var fs = require('fs'); works fine. 工作良好。

What am I doing wrong here? 我在这做错了什么?

I found out how it has to be done. 我发现了它是如何完成的。 Node-webkit offers a function for that. Node-webkit为此提供了一个功能。 It is working on MAC and should also work on windows. 它正在研究MAC,也应该在Windows上运行。 The function below is an example function. 以下功能是一个示例功能。 nw.gui and gui.Shell.showItemInFolder did the thing for me. nw.guigui.Shell.showItemInFolder为我做了这件事。 Thx for the input. 输入的Thx。

/*---------
Open Folder
---------*/
function openFolder(path){
    var gui = require('nw.gui');
    gui.Shell.showItemInFolder(path);
}

In nw.js version 0.13 or later, use: 在nw.js版本0.13或更高版本中,使用:

nw.Shell.showItemInFolder(fullpath);

Version < 0.13: 版本<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 . 如果路径类似于c:\\ foo \\ bar.txt ,它将打开文件夹foo并突出显示文件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). 如果路径是c:\\ foo \\ foo2 ,它将打开文件夹foo并突出显示文件夹foo2 (我希望它打开文件夹foo2 ,但它会打开父文件夹)。


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: 要找到正在运行的应用程序的完整路径,因为我们不能在前端使用节点函数(这就是为什么你试图加载fs模块时出错),我创建了一个节点模块(utils.js)下列:

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM