代码之家  ›  专栏  ›  技术社区  ›  Duck

macOS-无法启动具有本地化名称和空间的应用程序

  •  0
  • Duck  · 技术社区  · 5 年前

    为此,我创建了一个助手应用程序,在登录时加载并启动主应用程序。

    因此,我需要获得主应用程序路径。所以,我想:

    let appName = "My App.app"
    let path = "/Applications/" + appName 
    let newURL = NSURL.fileURL(withPath: path)
    

    let app = try NSWorkspace.shared.launchApplication(at: newURL,
                                                       options:[.withErrorPresentation, .inhibitingBackgroundOnly, .async],
                                                       configuration: [:])
    

    其中一个问题是,该应用程序在名称“My App.App”中包含一个空格。我删除了应用程序名称中的空格,此命令成功启动了应用程序。

    但我需要名字里的空格。

    然后我尝试使用包标识符启动应用程序。

    NSWorkspace.shared.launchApplication(withBundleIdentifier: mainAppBundleIdentifier,
    options: [.withErrorPresentation, .inhibitingBackgroundOnly, .async],
    additionalEventParamDescriptor: nil,
    launchIdentifier: nil)
    

    然后我又犯了一个错误,

    无法打开MyApp。将我的应用程序移到“应用程序”文件夹,然后重试。

    在“应用程序”文件夹中。

    let path = NSWorkspace.shared.absolutePathForApplication(withBundleIdentifier: mainBundleId)
    

    “/Users/myself/Library/Mail/V6/0982138471-EA33/[Gmail].mbox/All” Mail.mbox/871628745618726547816A/Data/2/8/5/Attachments/582584/2/MyAppMac.app“

    世界跆拳道联盟!!??

    有两件事不对:

    1. 这是我昨天发给测试人员的一封电子邮件的链接,该邮件向他发送了应用程序。
    2. 链接中提到的应用程序是MyAppMac.app,它是Xcode目标名称,没有本地化。

    如何获取目标名称?

    0 回复  |  直到 5 年前
        1
  •  1
  •   vadian    5 年前

    通常启动主应用程序的方法是首先检查它是否已经运行,如果没有运行 并传递一个变量来通知主应用程序它是由助手启动的

    例如

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        let appName = "Foo"
        let bundleIdentifier = "com.spacedog.foo"
    
        if NSRunningApplication.runningApplications(withBundleIdentifier: bundleIdentifier).isEmpty {
            let bundleURL = Bundle.main.bundleURL
            var pathComponents = bundleURL.pathComponents
            pathComponents.removeLast(3)
            pathComponents.append(contentsOf: ["MacOS", appName])
            let mainAppURL = NSURL.fileURL(withPathComponents:pathComponents)!
            let options = [NSWorkspace.LaunchConfigurationKey.arguments : ["launchedAtLogin"]]
            _ = try? NSWorkspace.shared.launchApplication(at: mainAppURL as URL, options: .withoutActivation, configuration: options)
        }
        NSApp.terminate(nil)
    }