【发布时间】: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