summaryrefslogtreecommitdiff
path: root/macos/Sources/Helpers/Extensions/NSWorkspace+Extension.swift
blob: bc2d028b530775c8254a118fa46b79202f98ce07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import AppKit
import UniformTypeIdentifiers

extension NSWorkspace {
    /// Returns the URL of the default text editor application.
    /// - Returns: The URL of the default text editor, or nil if no default text editor is found.
    var defaultTextEditor: URL? {
        defaultApplicationURL(forContentType: UTType.plainText.identifier)
    }
    
    /// Returns the URL of the default application for opening files with the specified content type.
    /// - Parameter contentType: The content type identifier (UTI) to find the default application for.
    /// - Returns: The URL of the default application, or nil if no default application is found.
    func defaultApplicationURL(forContentType contentType: String) -> URL? {
        return LSCopyDefaultApplicationURLForContentType(
            contentType as CFString,
            .all,
            nil
        )?.takeRetainedValue() as? URL
    }
    
    /// Returns the URL of the default application for opening files with the specified file extension.
    /// - Parameter ext: The file extension to find the default application for.
    /// - Returns: The URL of the default application, or nil if no default application is found.
    func defaultApplicationURL(forExtension ext: String) -> URL? {
        guard let uti = UTType(filenameExtension: ext) else { return nil}
        return defaultApplicationURL(forContentType: uti.identifier)
    }
}