企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# node-webkit教程(11)Platform Service之shell > 作者:玄魂 > 来源:[node-webkit教程(11)Platform Service之shell](http://www.cnblogs.com/xuanhun/p/3685100.html) ## 目录 + 11.1 Shell是什么 + 11.2 示例 + 11.3 小结 ## 11.1 Shell是什么 Shell是和桌面系统相关的一组API。通常在操作系统中,我们有“核”和“壳”的区分,“核”是操作系统的内核,“壳”是一个操作界面,提供给用户输入命令,解析并执行命令(调用“核”),这个用户界面被称作Shell(“壳”)。最常见的shell就是命令行(如windows下的CMD)。 Node-Webkit提供的shell功能很有限,现在能看到的只有三个api: + openExternal(URI) 用桌面系统默认的行为,在应用外部打开URI。这和我们在浏览器中打开一个[mailto:](http://www.cnblogs.com/mailto:)链接是一样的,控制器会转到桌面系统默认的邮件客户端。 ![](img/241135319201514.png) + openItem(file_path) 以操作系统默认方式打开指定路径。 + showItemInFolder(file_path) 在文件管理器中显示“file_path”指定的文件。 ## 11.2 示例 新建shell.html和package.json文件。 shell.html 内容如下: ``` <html> <head> <title>shellDemo</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body > <h1>shell 测试</h1> <button onclick="openInexplorer()">在默认浏览器中打开玄魂的电子书</button> <button onclick="openPdf()">打开pdf</button> <button onclick="showPdfInFloder()">打开pdf所在的文件夹</button> <script> // Load native UI library. var gui = require('nw.gui'); var shell = gui.Shell; function openInexplorer() { shell.openExternal('http://ebook.xuanhun521.com'); } function openPdf() { shell.openItem('D:\\101.pdf'); } function showPdfInFloder() { shell.showItemInFolder('D:\\学习资料\\技术类教程\\操作系统\\101-深入理解Linux内核(第三版 英文版)-1030页-pdf-免费下载.pdf'); } </script> </body> </html> ``` package.json内容如下: ``` { "name": "shell-demo", "main": "shell.html", "nodejs":true, "window": { "title": "shellDemo", "toolbar": true, "width": 800, "height": 600, "resizable":true, "show_in_taskbar":true, "frame":true, "kiosk":false, "icon": "2655716405282662783.png" }, "webkit":{ "plugin":true } } ``` 在上面的代码中,我们首先获取shell对象, ``` // Load native UI library. var gui = require('nw.gui'); var shell = gui.Shell; ``` ![](img/241135394203223.png) 函数openInexplorer中,调用shell.openExternal方法,在默认浏览器中打开“[玄魂的电子书站点](http://ebook.xuanhun521.com/)”。运行效果如下: ![](img/241135444352889.jpg) 在函数openPdf中调用shell.openItem('D:\\101.pdf'),在系统默认的paf阅读器中打开pdf文档,效果如下: ![](img/241135498103214.png) 在函数showPdfInFloder中,调用shell.showItemInFolder方法,在文件夹中显示并选中该文件。 ![](img/241135559981125.jpg) ## 11.3 小结 本文内容主要参考node-webkit的官方英文文档,做了适当的调整([https://github.com/rogerwang/node-webkit/wiki/Shell](https://github.com/rogerwang/node-webkit/wiki/Shell))。