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) #if targetEnvironment(macCatalyst)
mac.configure() mac.configure()
mac.menu.install() mac.menu.install()
if ProcessInfo.processInfo.arguments.contains(Constants.Global.appArgumentBackground) {
mac.utils.sendAppToBackground()
}
#endif #endif
return true return true
} }

View File

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

View File

@ -37,18 +37,18 @@ class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject {
func applicationDidFinishLaunching(_ notification: Notification) { func applicationDidFinishLaunching(_ notification: Notification) {
guard !isAppRunning else { guard !isAppRunning else {
NSApp.terminate(nil) NSApp.terminate(self)
return return
} }
Task { let cfg = NSWorkspace.OpenConfiguration()
do { cfg.hides = true
let cfg = NSWorkspace.OpenConfiguration() cfg.arguments = [Constants.Global.appArgumentBackground]
cfg.hides = true NSWorkspace.shared.openApplication(at: appURL, configuration: cfg) { app, error in
try await NSWorkspace.shared.openApplication(at: appURL, configuration: cfg) if let error = error {
} catch {
NSLog("Unable to launch main app: \(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"> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0"> <plist version="1.0">
<dict> <dict>
<key>com.apple.security.app-sandbox</key> <key>com.apple.security.app-sandbox</key>
<true/> <false/>
<key>com.apple.security.files.user-selected.read-only</key>
<true/>
</dict> </dict>
</plist> </plist>