8
10

More than 3 years have passed since last update.

NW.jsでHTMLをWindowsデスクトップアプリにする

Last updated at Posted at 2020-02-20

NW.jsとは?

ざっくりとした理解としては、Chromiumエンジン使ってhtmlを表示するデスクトップアプリを作れるフレームワークです。

Electronとの違いなど、詳しい記事は、こちら
NW.jsでデスクトップアプリの夢を見る!

1分でWindowsデスクトップアプリをつくる

いろいろ複雑なことやっている記事が多いですが、本当に簡単なので、1分でアプリ起動できます!

手順

  1. 公式サイト( https://nwjs.io/ )から、左側の「NORMAL」をダウンロードします。
    image.png

  2. zip展開し、src というフォルダを作成。そこに起動するHTMLファイル一式を格納します。

  3. package.json を作成して同フォルダに入れます。最低限の設定は以下。

package.json
{
  "name":"test.application",
  "version":"1.0.0",
  "main":"src/index.html"
}

4.nw.exeを実行!!
image.png

以上。超簡単です。

NW.js Tips

package.json

ウインドウの最低サイズとアイコンの指定

package.json
{
  "name":"test.application",
  "version":"1.0.0",
  "main":"src/index.html"
  "window":{
    "min_width":1200,
    "min_height":760,
    "icon":"assets/icon32.png"
  }
}

JavaScript

いろいろな情報の寄せ集めですが、動いたコードを紹介。

exeをキックする

    var spawn = require('child_process').spawn,
    child    = spawn('C:\\windows\\notepad.exe', ["C:/Windows/System32/Drivers/etc/hosts"]);

    child.stdout.on('data', function (data) {
        console.log('stdout: ' + data);
    });

    child.stderr.on('data', function (data) {
        console.log('stderr: ' + data);
    });

    child.on('close', function (code) {
        console.log('child process exited with code ' + code);
    });

エクスプローラーでフォルダーを開く

    require('child_process').exec('start "" "c:\\windows"');

htmlと同フォルダーにあるファイルを関連付けられたアプリケーションで開く

    var path = require('path');
    var startDir = path.dirname(process.execPath);

    var gui = window.require( 'nw.gui' );
    gui.Shell.openItem( startDir+'\\src\\test.pdf' );

標準のブラウザでURLを開く

    var gui = window.require( 'nw.gui' );
    gui.Shell.openExternal( 'https://google.com/' );
8
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
10