19
19
import AppKit
20
20
import Foundation
21
21
22
- public protocol AppInfoRetrieveing {
22
+ /// Protocol to provide a mechanism to query information about installed Applications.
23
+ ///
24
+ public protocol AppInfoRetrieving {
23
25
24
- /// Provides a structure featuring commonly-used app info.
26
+ /// Provides a structure featuring commonly-used app info given the Application's bundleID .
25
27
///
26
- /// It's also possible to retrieve the individual information directly by calling other methods in this class.
28
+ /// - Parameters:
29
+ /// - bundleID: the bundleID of the target Application.
27
30
///
28
31
func getAppInfo( bundleID: String ) -> AppInfo ?
32
+
33
+ /// Provides a structure featuring commonly-used app info, given the Application's URL.
34
+ ///
35
+ /// - Parameters:
36
+ /// - appURL: the URL where the target Application is installed.
37
+ ///
29
38
func getAppInfo( appURL: URL ) -> AppInfo ?
39
+
40
+ /// Obtains the icon for a specified application.
41
+ ///
42
+ /// - Parameters:
43
+ /// - bundleID: the bundleID of the target Application.
44
+ ///
30
45
func getAppIcon( bundleID: String ) -> NSImage ?
46
+
47
+ /// Obtains the URL for a specified application.
48
+ ///
49
+ /// - Parameters:
50
+ /// - bundleID: the bundleID of the target Application.
51
+ ///
52
+ func getAppURL( bundleID: String ) -> URL ?
53
+
54
+ /// Obtains the visible name for a specified application.
55
+ ///
56
+ /// - Parameters:
57
+ /// - bundleID: the bundleID of the target Application.
58
+ ///
31
59
func getAppName( bundleID: String ) -> String ?
60
+
61
+ /// Obtains the bundleID for a specified application.
62
+ ///
63
+ /// - Parameters:
64
+ /// - appURL: the URL where the target Application is installed.
65
+ ///
32
66
func getBundleID( appURL: URL ) -> String ?
33
67
68
+ /// Obtains the bundleIDs for all Applications embedded within a speciried application.
69
+ ///
70
+ /// - Parameters:
71
+ /// - bundleURL: the URL where the parent Application is installed.
72
+ ///
73
+ func findEmbeddedBundleIDs( in bundleURL: URL ) -> Set < String >
34
74
}
35
75
36
- public class AppInfoRetriever : AppInfoRetrieveing {
76
+ /// Provides a mechanism to query information about installed Applications.
77
+ ///
78
+ public class AppInfoRetriever : AppInfoRetrieving {
37
79
38
80
public init ( ) { }
39
81
82
+ /// Provides a structure featuring commonly-used app info given the Application's bundleID.
83
+ ///
84
+ /// - Parameters:
85
+ /// - bundleID: the bundleID of the target Application.
86
+ ///
40
87
public func getAppInfo( bundleID: String ) -> AppInfo ? {
41
88
guard let appName = getAppName ( bundleID: bundleID) else {
42
89
return nil
@@ -46,6 +93,11 @@ public class AppInfoRetriever: AppInfoRetrieveing {
46
93
return AppInfo ( bundleID: bundleID, name: appName, icon: appIcon)
47
94
}
48
95
96
+ /// Provides a structure featuring commonly-used app info, given the Application's URL.
97
+ ///
98
+ /// - Parameters:
99
+ /// - appURL: the URL where the target Application is installed.
100
+ ///
49
101
public func getAppInfo( appURL: URL ) -> AppInfo ? {
50
102
guard let bundleID = getBundleID ( appURL: appURL) else {
51
103
return nil
@@ -54,6 +106,11 @@ public class AppInfoRetriever: AppInfoRetrieveing {
54
106
return getAppInfo ( bundleID: bundleID)
55
107
}
56
108
109
+ /// Obtains the icon for a specified application.
110
+ ///
111
+ /// - Parameters:
112
+ /// - bundleID: the bundleID of the target Application.
113
+ ///
57
114
public func getAppIcon( bundleID: String ) -> NSImage ? {
58
115
guard let appURL = NSWorkspace . shared. urlForApplication ( withBundleIdentifier: bundleID) else {
59
116
return nil
@@ -72,6 +129,11 @@ public class AppInfoRetriever: AppInfoRetrieveing {
72
129
return NSImage ( contentsOf: iconURL)
73
130
}
74
131
132
+ /// Obtains the visible name for a specified application.
133
+ ///
134
+ /// - Parameters:
135
+ /// - bundleID: the bundleID of the target Application.
136
+ ///
75
137
public func getAppName( bundleID: String ) -> String ? {
76
138
if let appURL = NSWorkspace . shared. urlForApplication ( withBundleIdentifier: bundleID) {
77
139
// Try reading from Info.plist
@@ -86,6 +148,20 @@ public class AppInfoRetriever: AppInfoRetrieveing {
86
148
return nil
87
149
}
88
150
151
+ /// Obtains the URL for a specified application.
152
+ ///
153
+ /// - Parameters:
154
+ /// - bundleID: the bundleID of the target Application.
155
+ ///
156
+ public func getAppURL( bundleID: String ) -> URL ? {
157
+ NSWorkspace . shared. urlForApplication ( withBundleIdentifier: bundleID)
158
+ }
159
+
160
+ /// Obtains the bundleID for a specified application.
161
+ ///
162
+ /// - Parameters:
163
+ /// - appURL: the URL where the target Application is installed.
164
+ ///
89
165
public func getBundleID( appURL: URL ) -> String ? {
90
166
let infoPlistURL = appURL. appendingPathComponent ( " Contents/Info.plist " )
91
167
if let plist = NSDictionary ( contentsOf: infoPlistURL) ,
@@ -94,4 +170,32 @@ public class AppInfoRetriever: AppInfoRetrieveing {
94
170
}
95
171
return nil
96
172
}
173
+
174
+ // MARK: - Embedded Bundle IDs
175
+
176
+ /// Obtains the bundleIDs for all Applications embedded within a speciried application.
177
+ ///
178
+ /// - Parameters:
179
+ /// - bundleURL: the URL where the parent Application is installed.
180
+ ///
181
+ public func findEmbeddedBundleIDs( in bundleURL: URL ) -> Set < String > {
182
+ var bundleIDs : [ String ] = [ ]
183
+ let fileManager = FileManager . default
184
+
185
+ guard let enumerator = fileManager. enumerator ( at: bundleURL,
186
+ includingPropertiesForKeys: nil ,
187
+ options: [ . skipsHiddenFiles] ,
188
+ errorHandler: nil ) else {
189
+ return [ ]
190
+ }
191
+
192
+ for case let fileURL as URL in enumerator where fileURL. pathExtension == " app " {
193
+ let embeddedBundle = Bundle ( url: fileURL)
194
+ if let bundleID = embeddedBundle? . bundleIdentifier {
195
+ bundleIDs. append ( bundleID)
196
+ }
197
+ }
198
+
199
+ return Set ( bundleIDs)
200
+ }
97
201
}
0 commit comments