# 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:)鏈接是一樣的,控制器會轉到桌面系統默認的郵件客戶端。

+ 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;
```

函數openInexplorer中,調用shell.openExternal方法,在默認瀏覽器中打開“[玄魂的電子書站點](http://ebook.xuanhun521.com/)”。運行效果如下:

在函數openPdf中調用shell.openItem('D:\\101.pdf'),在系統默認的paf閱讀器中打開pdf文檔,效果如下:

在函數showPdfInFloder中,調用shell.showItemInFolder方法,在文件夾中顯示并選中該文件。

## 11.3 小結
本文內容主要參考node-webkit的官方英文文檔,做了適當的調整([https://github.com/rogerwang/node-webkit/wiki/Shell](https://github.com/rogerwang/node-webkit/wiki/Shell))。
- 中文 Wiki
- 支持列表
- 開始nw.js
- package.json
- 中文教程
- node-webkit學習(1)hello world
- node-webkit學習(2)基本結構和配置
- node-webkit學習(3)Native UI API概覽
- node-webkit學習(4)Native UI API 之window
- node-webkit教程(5)Native UI API 之Frameless window
- node-webkit教程(6)Native UI API 之Menu(菜單)
- node-webkit教程(7)Platform Service之APP
- node-webkit教程(8)Platform Service之Clipboard
- node-webkit教程(9)native api 之Tray(托盤)
- node-webkit教程(10)Platform Service之File dialogs
- node-webkit教程(11)Platform Service之shell
- node-webkit教程(12)全屏
- node-webkit教程(13)gpu支持信息查看
- node-webkit教程(14)禁用緩存
- node-webkit教程(15)當圖片加載失敗的時候
- node-webkit教程(16)調試typescript