-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathNSWorkspace+Extension.swift
More file actions
51 lines (44 loc) · 1.7 KB
/
NSWorkspace+Extension.swift
File metadata and controls
51 lines (44 loc) · 1.7 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import AppKit
import Logger
extension NSWorkspace {
public static func getXcodeBundleURL() -> URL? {
var xcodeBundleURL: URL?
// Get currently running Xcode application URL
if let xcodeApp = NSWorkspace.shared.runningApplications.first(where: { $0.bundleIdentifier == "com.apple.dt.Xcode" }) {
xcodeBundleURL = xcodeApp.bundleURL
}
// Fallback to standard path if we couldn't get the running instance
if xcodeBundleURL == nil {
let standardPath = "/Applications/Xcode.app"
if FileManager.default.fileExists(atPath: standardPath) {
xcodeBundleURL = URL(fileURLWithPath: standardPath)
}
}
return xcodeBundleURL
}
public static func openFileInXcode(
fileURL: URL,
completion: ((NSRunningApplication?, Error?) -> Void)? = nil
) {
guard let xcodeBundleURL = Self.getXcodeBundleURL() else {
if let completion = completion {
completion(nil, NSError(domain: "The Xcode app is not found.", code: 0))
}
return
}
let configuration = NSWorkspace.OpenConfiguration()
configuration.activates = true
configuration.promptsUserIfNeeded = false
Self.shared.open(
[fileURL],
withApplicationAt: xcodeBundleURL,
configuration: configuration
) { app, error in
if let completion = completion {
completion(app, error)
} else if let error = error {
Logger.client.error("Failed to open file \(String(describing: error))")
}
}
}
}