LoginSignup
32
32

More than 5 years have passed since last update.

Electronのアプリ内部に配置されたファイルを実行する

Last updated at Posted at 2016-06-10

色々なアプリを作るときに、Electronではnpmのモジュールが使えるのであまり困ることはないのだけれど、どうしても実行速度や作る時間の問題で、既存のexe等を使いたいときがある。

外部に配置したexeを指定することは、パスの指定諸々とてもめんどくさいので、アプリケーション内部に配置して実行できるかなと思って、exeファイルをchild_processexec()で動かしてみたらうまく実行できた。

安心して進めていたら、パッケージ化した後にどうにも動かなくなって困った。

ぐぐってみたら、パッケージングを行うときにasar形式でアーカイブしていたため、exec()spawn()では動かないみたいだった。(参照「Executing a script as a child_process.exec inside an asar archive #3512 - GitHub/electron」、「アプリケーションのパッケージ化 - GitHub/electron」)

execFile()を使ったら動いた。

const remote = require('electron').remote;
const app = remote.app;

// アプリ内部のパスを取得する
const path:string = app.getAppPath();

console.log(path);

// ファイルを実行する
const exec = require('child_process').execFile;
exec( path + "/実行ファイルまでのパス", ["オプション"],
  function(err:any, stdout:any, stderr:any){

 }
);

※ OSX El Capitan 10.11.5、Electron 1.2.1で試しました。

以上。

32
32
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
32
32