【问题标题】:electronJS / Detect if foreign app is running (MacOS) (Sandbox) (MAS)electronJS / 检测外部应用程序是否正在运行 (MacOS) (沙盒) (MAS)
【发布时间】:2023-03-26 07:59:01
【问题描述】:

是否可以在 electronJS 中检测特定应用是否在 MacOS 上运行?

我想检查一下,如果 Mail.app 已打开且它很重要,代码是否在 Mac Apple Store (MAS) 中的 沙盒模式 中工作。 p>

在 AppKit 中可以使用 var runningApplications: [NSRunningApplication] { get } https://developer.apple.com/documentation/appkit/nsworkspace/1534059-runningapplications

我写了一段代码,可以用,但在沙盒中不行,终端命令是不可能的:

 import { exec } from "child_process";
 ...

 isRunning(query: string): Promise<unknown> {
    return new Promise((resolve, reject) => {
        const cmd = `ps aux | grep "${query}" | grep -v grep`;


        exec(cmd, (err, stdout, stderr) => {

            if (stderr) {
                reject(stderr);
                return false;
            }

            const lines = stdout.split("\n");
            let active = null;
            let pid = null;

            for (let index = 0; index < lines.length; index++) {
                const line = lines[index];

                if (
                    line.indexOf(query) !== -1 &&
                    line.indexOf(USERNAME) !== -1

                ) {
                    active = line;
                    break;
                }
            }

            if (active) {
                const activeData = active.replace(/(\s+)/g, '\t').split('\t');
                pid = activeData.length > 2 ? activeData[1] : null;
            }

            resolve(pid);
        });
    });
}

this.isRunning('Mail.app/Contents/MacOS/Mail').then(pid => {
    if (pid) {
       // kill the app? ???? 
    } else {
        resolve(null);
    }
}, reject);

有没有人为在 MAS 沙箱中工作的 electronJS 提供更好的解决方案?

【问题讨论】:

    标签: javascript macos electron appstore-sandbox


    【解决方案1】:

    我找到了解决办法:

    const script = `
    if application "Mail" is running then
        copy "true" to stdout
    else
        copy "false" to stdout
    end if
    `;
    
    exec(`osascript -e '${script}'`,  (err, stdout, stderr) => {
        if (stderr) {console.log("error", stderr); return;}
    
        console.log("stdout", stdout);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-25
      • 1970-01-01
      • 2019-07-08
      • 2015-06-08
      • 1970-01-01
      • 2019-11-22
      • 2020-05-17
      相关资源
      最近更新 更多