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

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

本文介绍了macOS-无法启动具有本地化名称和空间的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个登录时启动的macOS应用程序。该应用程序的名称为 My App.app。

I have this macOS app that is launched at login. This app has a space in the name "My App.app".

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

For that matter, I have created a helper app that loads at login and launches the main app.

我在此助手中,试图启动主应用程序。

I am inside this helper, trying to launch the main app.

因此,我需要获取主应用程序路径。因此,我这样做:

For this reason I need to get the main app path. So, I do:

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: [:])

我得到一个错误找不到应用程序。

I get an error "APPLICATION NOT FOUND".

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

One of the problems is that the App contains a space in the name "My App.app". I remove the space in the app name and this command successfully launches the app.

但是我需要名称中的空格。

But I need the space in the name.

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

Then I try to launch the app using the bundle identifier.

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

然后出现另一个错误

问题在于该应用程序已经 >在应用程序文件夹上。

The problem is that the app is already on the Applications folder.

然后我用它来检查

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

,我得到了这个

WTF !! ??

WTF!!??

有两点错误:


  1. 这是电子邮件的链接

  2. 链接中提到的应用是MyAppMac.app,它是Xcode目标名称,没有本地化。

我如何在天堂的名字中从另一个应用程序启动一个应用程序该应用程序的名称中包含空格并已本地化吗?

How in the heavens name do I launch an app from another app when the app contains a space in the name and is localized?

如何获取目标名称?

推荐答案

启动主应用程序的通常方法是首先检查它是否已经在运行,是否不运行可执行文件并传递一个变量以通知主应用程序

The usual way to launch the main application is first to check if it's already running and if not to run the executable and pass a variable to inform the main application that it was launched by the helper

例如

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)
}

这篇关于macOS-无法启动具有本地化名称和空间的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:01