天天看點

node-webkit教程(11)Platform Service之shellnode-webkit教程(11)Platform Service之shell

文/玄魂

目錄

node-webkit教程(10)Platform Service之shell

前言

11.1  Shell是什麼

11.2  示例

幾個月前,要開發一個簡易的展示應用,要求支援離線播放(桌面應用)和線上播放(web應用)。

node-webkit解決了我通過html和js來編寫桌面應用的難題。

至于node-webkit的定義,按照作者的說法:

“ 基于node.js和chromium的應用程式實時運作環境,可運作通過HTML(5)、CSS(3)、Javascript來編寫的本地應用程式。node.js和webkit的結合體,webkit提供DOM操作,node.js提供本地化操作;且将二者的context完全整合,可在HTML代碼中直接使用node.js的API。”

node-webkit教程(11)Platform Service之shellnode-webkit教程(11)Platform Service之shell

從本篇文章開始,為您介紹Platform Services些列的API,本系列由以下類别:

·             App – 每個應用運作時全局api

·             Clipboard – 剪貼闆

·             Tray – 狀态欄圖示,消息通知

·             File dialogs-檔案選擇對話框

·             Shell – 桌面相關

·             Handling files and arguments-處理檔案和相關參數

Shell是和桌面系統相關的一組API。通常在作業系統中,我們有“核”和“殼”的區分,“核”是作業系統的核心,“殼”是一個操作界面,提供給使用者輸入指令,解析并執行指令(調用“核”),這個使用者界面被稱作Shell(“殼”)。最常見的shell就是指令行(如windows下的CMD)。

Node-Webkit提供的shell功能很有限,現在能看到的隻有三個api:

l  openExternal(URI)

node-webkit教程(11)Platform Service之shellnode-webkit教程(11)Platform Service之shell

l  openItem(file_path)

以作業系統預設方式打開指定路徑。

l  showItemInFolder(file_path)

在檔案管理器中顯示“file_path”指定的檔案。

建立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.

node-webkit教程(11)Platform Service之shellnode-webkit教程(11)Platform Service之shell
node-webkit教程(11)Platform Service之shellnode-webkit教程(11)Platform Service之shell

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

node-webkit教程(11)Platform Service之shellnode-webkit教程(11)Platform Service之shell

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

node-webkit教程(11)Platform Service之shellnode-webkit教程(11)Platform Service之shell

ps:nw.js,electron交流群 313717550 

本文轉自玄魂部落格園部落格,原文連結:http://www.cnblogs.com/xuanhun/p/3685100.html,如需轉載請自行聯系原作者