Launch application in background from launcher

Requires disabling sandbox for launcher to receive arguments.

https://developer.apple.com/documentation/appkit/nsworkspace/openconfiguration/3172708-arguments

Refine some minor things about launcher:

- Pass invoker to NSApp.terminate() calls

- Use legacy version of .openApplication() to drop Concurrency
This commit is contained in:
Davide De Rosa 2022-06-26 09:50:25 +02:00
parent 3f4b55a003
commit f33380b4e2
4 changed files with 15 additions and 12 deletions

View File

@ -33,6 +33,9 @@ class AppDelegate: NSObject, UIApplicationDelegate, ObservableObject {
#if targetEnvironment(macCatalyst)
mac.configure()
mac.menu.install()
if ProcessInfo.processInfo.arguments.contains(Constants.Global.appArgumentBackground) {
mac.utils.sendAppToBackground()
}
#endif
return true
}

View File

@ -44,5 +44,7 @@ enum Constants {
static let appBuildNumber = Int(Bundle.main.infoDictionary![kCFBundleVersionKey as String] as! String)!
static let appVersionString = "\(appVersionNumber) (\(appBuildNumber))"
static let appArgumentBackground = "-background"
}
}

View File

@ -37,18 +37,18 @@ class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject {
func applicationDidFinishLaunching(_ notification: Notification) {
guard !isAppRunning else {
NSApp.terminate(nil)
NSApp.terminate(self)
return
}
Task {
do {
let cfg = NSWorkspace.OpenConfiguration()
cfg.hides = true
try await NSWorkspace.shared.openApplication(at: appURL, configuration: cfg)
} catch {
let cfg = NSWorkspace.OpenConfiguration()
cfg.hides = true
cfg.arguments = [Constants.Global.appArgumentBackground]
NSWorkspace.shared.openApplication(at: appURL, configuration: cfg) { app, error in
if let error = error {
NSLog("Unable to launch main app: \(error)")
return
}
await NSApp.terminate(nil)
NSApp.terminate(self)
}
}
}

View File

@ -2,9 +2,7 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.files.user-selected.read-only</key>
<true/>
<key>com.apple.security.app-sandbox</key>
<false/>
</dict>
</plist>