如何从沙盒 macOS 应用程序调用 shell 脚本来管理 LaunchAgents?

How to call shell scripts to manage LaunchAgents from a sandboxed macOS application?

我有一个非沙盒 macOS 应用程序,它使用以下方法愉快地调用其资源包中的 shell 脚本:

class func runShell(launchPath: String, arguments: [String] = [], waitUntilExit: Bool) -> Void {
    let task = Process()
    task.launchPath = launchPath
    task.arguments = arguments

    let pipe = Pipe()
    task.standardOutput = pipe
    task.launch()

    if waitUntilExit {
        task.waitUntilExit()
    }
}
ABCProcessManager.runShell(launchPath: scriptPath.path, arguments: ["-workingdirectory", path], waitUntilExit: true)

我正在尝试将应用程序转换为沙盒应用程序(用于 App Store 上传),但它在调用脚本时冻结。

脚本管理启动代理。加载、卸载、启动、停止。

我需要如何更改我的代码才能使其在启用沙箱的情况下工作?也许有一个 "sandboxed way" 管理启动代理?

我在 Apple 支持论坛上找到了 Apple 工作人员发布的答案:

Is is possible to have applications in the Mac App Store that work with LaunchAgents?

No.

Why not:

  1. XPC Services included within an app are only available to that app.
  2. Mac App Store apps are not allowed to include a launchd daemon or agent.

他们还提到技术上有一种未记录的方法,但强烈建议不要走这条路:

As an accident of the implementation, the service registered by a sandbox-compatible login item is visible to other processes running in the user’s session. So on current systems you could make this work by implementing a sandbox-compatible login item (as illustrated by the AppSandboxLoginItemXPCDemo sample code) and have your command-line tools talk to it. The problem with this approach is that it’s an accident of the implementation rather than a documented feature. Moreover, it runs counter to the general App Sandbox goal that a sandboxed app should be isolated from the rest of the system.

所以我想我会坚持使用直接分发给客户的非沙盒应用程序。

Apple support post link