import AppKit func fail(_ message: String) -> Never { FileHandle.standardError.write(Data("\(message)\n".utf8)) exit(1) } guard CommandLine.arguments.count == 2 else { fail("Usage: swift Scripts/run.swift ") } let target = URL(fileURLWithPath: CommandLine.arguments[1]).standardizedFileURL.resolvingSymlinksInPath() let running = NSRunningApplication.runningApplications(withBundleIdentifier: "dev.conjure.launcher") .filter { $0.bundleURL?.resolvingSymlinksInPath().path == target.path } for app in running { guard app.terminate() else { fail("Could not quit the existing Conjure instance.") } } for _ in 0..<100 { if running.allSatisfy(\.isTerminated) { break } // AppKit refreshes running-application state on the main run loop. RunLoop.main.run(until: Date(timeIntervalSinceNow: 0.05)) } guard running.allSatisfy(\.isTerminated) else { fail("Conjure did not quit; the rebuilt app was not launched. Quit it and try again.") } let configuration = NSWorkspace.OpenConfiguration() configuration.createsNewApplicationInstance = true NSWorkspace.shared.openApplication(at: target, configuration: configuration) { app, error in if let error { fail("Could not launch Conjure: \(error.localizedDescription)") } guard let app else { fail("Launch Services did not return a running application.") } print("Opened rebuilt Conjure (PID \(app.processIdentifier)).") exit(0) } RunLoop.main.run()