我可以在 electron 中使用 OSX native API 吗

Can i use OSX native API's in electron

有没有办法利用OSX原生API的使用Electron. Let say i want to use System Configuration Framework,有没有办法利用原生的力量OSXAPI?

谢谢

我知道这是一个迟到的答案,但你有一些选择:

  • native node modules 允许您使用 C++ 和 ObjectiveC(或 Swift)编写,并使用 v8 将 API 暴露给 node.js。这为您提供了很大的灵活性和功能,但需要最多的时间来开发。

  • NodObjC is a native node module that allows you to interact with the ObjC runtime. I've never used it but it seems like a solid project and would simplify whatever you're trying to do. Another option similar to this is node-ffi。不过,您引用的框架看起来像 ObjC API,因此不确定它是否适用于您的特定用例。

  • 对于简单的东西,您可以在幕后使用 node-applescript. I've only played with applescript a little bit but I was surprised at some of the things you can do with it. For example, you can use it to set your app to start on login (see node-auto-launch). While limited compared to the above, if it does what you want, it's simpler and requires no compiling, which is nice. Note that this is going to use child_process 为 运行 中的 applescript 生成一个新进程。无论您做什么,这都可能需要考虑一些后果正在尝试做。

  • 如果 OSX API 您也可以只使用节点 child_process 模块 您尝试使用的 CLI 很好。这里的好处是简单,缺点是您可能必须将它输出的字符串解析为有意义的数据结构,根据我的经验,这可能有点困难。

您还可以使用 child_process 的 exec 函数从您的电子应用程序执行脚本或终端命令。

您还可以轻松查看您的应用在哪个平台上运行:

var isWin = /^win/.test(process.platform);
  if(isWin){
    //windows
  }else{
    //macos
  }

您还可以在 swift 中执行为 OSX 使用 OSX 本机 API 的应用创建的脚本:

 exec(path.join(__dirname, './build-scripts/Network -agr1 ' + value1 + ' -agr2 ' + value2),
      function (error, stdout, stderr) {
        console.log('stdout: ' + stdout);
        console.log('stderr: ' + stderr);
        if (error !== null) {
          console.log("Fail to execute script"+ error);
        } else {
          console.log("ok")
        }
      }); 

您还可以使用 ffi 直接从 javascript 调用本机库。