2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Node.jsとJXAでMacのアプリが起動中かどうか調べる

Last updated at Posted at 2019-07-11

AppleScriptでもいいですが、JXAだとワンライナーで完結していい感じです。

isAppRunning.js
const promisify = require("util").promisify;
const exec = require("child_process").exec;
const execp = promisify(exec);

const appName = "Slack";

(async () => {
  const { stdout } = await execp(
    `osascript -l JavaScript -e "Application('${appName}').running()"`
  );
  console.log({ stdout });
})();

注意点としては、返り値がBooleanっぽいけどStringだと言うことです。
上の書き方だと {stdout: 'true\n'}のような感じになります。
返り値を return stdout === "true\n" にして、Promise<boolean>として扱うのがいいかもですね。

他にもいい方法があったらぜひ教えて下さい。
調べてもあんまり見つからないんですよね。

たぶんSwiftが使えればもっとつおくなれる。

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?