@@ -65,27 +65,21 @@ struct LSKit {
6565 - scheme: url scheme (excluding the `:` or `/`, e.g. `http`)
6666 - Returns: OSStatus (discardable)
6767 */
68- @discardableResult static func setDefaultApp( identifier: String , forScheme scheme: String ) -> OSStatus {
68+ @discardableResult static func setDefaultApp( identifier: String , forScheme scheme: String ) async -> OSStatus {
6969 if #available( macOS 12 , * ) {
7070 // print("running on macOS 12, using NSWorkspace")
71- let ws = NSWorkspace . shared
72-
73- // since the new NSWorkspace function is asynchronous we have to use semaphores here
74- let semaphore = DispatchSemaphore ( value: 0 )
75- var errCode : OSStatus = 0
76-
77- guard let appURL = ws. urlForApplication ( withBundleIdentifier: identifier) else { return 1 }
78- ws. setDefaultApplication ( at: appURL, toOpenURLsWithScheme: scheme) { err in
79- // err is an NSError wrapped in a CocoaError
80- if let err = err as? CocoaError {
81- if let underlyingError = err. errorUserInfo [ " NSUnderlyingError " ] as? NSError {
82- errCode = OSStatus ( clamping: underlyingError. code)
83- }
71+ do {
72+ let ws = NSWorkspace . shared
73+ guard let appURL = ws. urlForApplication ( withBundleIdentifier: identifier) else { return 1 }
74+ try await ws. setDefaultApplication ( at: appURL, toOpenURLsWithScheme: scheme)
75+ return 0
76+ } catch {
77+ if let err = error as? CocoaError , let underlyingError = err. errorUserInfo [ " NSUnderlyingError " ] as? NSError {
78+ return OSStatus ( clamping: underlyingError. code)
79+ } else {
80+ return 1
8481 }
85- semaphore. signal ( )
8682 }
87- semaphore. wait ( )
88- return errCode
8983 } else {
9084 return LSSetDefaultHandlerForURLScheme ( scheme as CFString , identifier as CFString )
9185 }
@@ -147,31 +141,26 @@ struct LSKit {
147141 - forTypeIdentifier: uniform type identifier ( e.g. `public.html`)
148142 - Returns: OSStatus (discardable)
149143 */
150- @discardableResult static func setDefaultApp( identifier: String , forTypeIdentifier utidentifier: String ) -> OSStatus {
144+ @discardableResult static func setDefaultApp( identifier: String , forTypeIdentifier utidentifier: String ) async -> OSStatus {
151145 if #available( macOS 12 , * ) {
152146 // print("running on macOS 12, using NSWorkspace")
153147 guard let utype = UTType ( utidentifier) else {
154148 return 1
155149 }
156-
157- let ws = NSWorkspace . shared
158150
159- // since the new NSWorkspace function is asynchronous we have to use semaphores here
160- let semaphore = DispatchSemaphore ( value : 0 )
161- var errCode : OSStatus = 0
162-
163- guard let appURL = ws . urlForApplication ( withBundleIdentifier : identifier ) else { return 1 }
164- ws . setDefaultApplication ( at : appURL , toOpen : utype ) { err in
151+ do {
152+ let ws = NSWorkspace . shared
153+ guard let appURL = ws . urlForApplication ( withBundleIdentifier : identifier ) else { return 1 }
154+ try await ws . setDefaultApplication ( at : appURL , toOpen : utype )
155+ return 0
156+ } catch {
165157 // err is an NSError wrapped in a CocoaError
166- if let err = err as? CocoaError {
167- if let underlyingError = err . errorUserInfo [ " NSUnderlyingError " ] as? NSError {
168- errCode = OSStatus ( clamping : underlyingError . code )
169- }
158+ if let err = error as? CocoaError , let underlyingError = err . errorUserInfo [ " NSUnderlyingError " ] as? NSError {
159+ return OSStatus ( clamping : underlyingError. code )
160+ } else {
161+ return 1
170162 }
171- semaphore. signal ( )
172163 }
173- semaphore. wait ( )
174- return errCode
175164 } else {
176165 return LSSetDefaultRoleHandlerForContentType ( utidentifier as CFString , . all, identifier as CFString )
177166 }
0 commit comments