-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDefaultMailApp.swift
executable file
·235 lines (181 loc) · 8.3 KB
/
DefaultMailApp.swift
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/swift
import Cocoa
struct DefaultApp {
// Globals
static let utiHandlerMicrosoft = [
"com.apple.mail.email": "com.microsoft.outlook",
"com.microsoft.outlook16.email-message": "com.microsoft.outlook",
"public.vcard": "com.microsoft.outlook",
"com.apple.ical.ics": "com.microsoft.outlook",
"com.microsoft.outlook16.icalendar": "com.microsoft.outlook"
]
static let utiHandlerApple = [
"com.apple.mail.email": "com.apple.mail",
"public.vcard": "com.apple.AddressBook",
"com.apple.ical.ics": "com.apple.CalendarFileHandler",
]
static let urlSchemeApple = ["mailto": "com.apple.mail"]
static let urlSchemeMicrosoft = ["mailto": "com.microsoft.outlook"]
// Funcs
func getDefaultApp(handler: Dictionary<String, String>) {
for (contentType, _) in handler {
if let appHandler = LSCopyDefaultRoleHandlerForContentType(contentType as CFString, LSRolesMask.editor) {
let bundleId = appHandler.takeRetainedValue()
print("\(contentType): \(bundleId)")
}
}
}
func getDefaultScheme(handler: Dictionary<String, String>) {
for (scheme, _) in handler {
if let appScheme = LSCopyDefaultHandlerForURLScheme(scheme as CFString) {
let bundleId = appScheme.takeRetainedValue()
print("\(scheme): \(bundleId)")
}
}
}
func setDefaultApp(handler: Dictionary<String, String>) {
for (contentType, lsHandler) in handler {
LSSetDefaultRoleHandlerForContentType(contentType as CFString, LSRolesMask.editor, lsHandler as CFString)
}
}
func setDefaultScheme(handler: Dictionary<String, String>) {
for (scheme, lsHandler) in handler {
LSSetDefaultHandlerForURLScheme(scheme as CFString, lsHandler as CFString)
}
}
}
/////////////////
class AppDelegate: NSObject, NSApplicationDelegate {
let appName = "Demo App"
let defaultApp = DefaultApp()
// UI elements
let window = NSWindow(contentRect: NSMakeRect(0, 0, 600, 400),
styleMask: .titled,
backing: .buffered,
defer: true)
let label = NSTextField(frame: NSRect(x: 0, y: 300, width: 600, height: 60))
let mailView = NSImageView(frame: NSRect(x: 120, y: 200, width: 80, height: 80))
let outlookView = NSImageView(frame: NSRect(x: 120, y: 80, width: 80, height: 80))
let mailButton = NSButton(frame: NSRect(x: 280, y: 210, width: 200, height: 60))
let outlookButton = NSButton(frame: NSRect(x: 280, y: 90, width: 200, height: 60))
let mailButtonClickedIcon = NSImageView(frame: NSRect(x: 500, y: 220, width: 40, height: 40))
let outlookButtonClickedIcon = NSImageView(frame: NSRect(x: 500, y: 100, width: 40, height: 40))
// Methods
func setupUI() {
// Menu
setupMenu(appName: appName)
// Window
setupWindow()
// "Normal" window presence (activation & exit)
window.orderFrontRegardless()
NSApp.activate(ignoringOtherApps: true)
// Setup window's content view
let contentView = window.contentView!
// Dock icon
setupDockIcon(path: "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/ToolbarCustomizeIcon.icns")
///////////////
// Label
setupLabel(title: "Choose default email app")
contentView.addSubview(label)
// Icons
setupIcon(appPath: "/Applications/Mail.app", imageView: mailView)
contentView.addSubview(mailView)
setupIcon(appPath: "/Applications/Microsoft Outlook.app", imageView: outlookView)
contentView.addSubview(outlookView)
// Buttons
setupButton(title: "macOS Mail", button: mailButton)
contentView.addSubview(mailButton)
mailButton.action = #selector(mailButtonClicked)
setupButton(title: "Outlook", button: outlookButton)
contentView.addSubview(outlookButton)
outlookButton.action = #selector(outlookButtonClicked)
// Button clicked => OK icons
setupHiddenIcon(path: "/System/Library/PrivateFrameworks/CommerceKit.framework/Versions/A/Resources/OSBadge.icns", imageView: mailButtonClickedIcon)
contentView.addSubview(mailButtonClickedIcon)
setupHiddenIcon(path: "/System/Library/PrivateFrameworks/CommerceKit.framework/Versions/A/Resources/OSBadge.icns", imageView: outlookButtonClickedIcon)
contentView.addSubview(outlookButtonClickedIcon)
}
// Helper methods
func setupMenu(appName: String) {
let mainMenu = NSMenu()
let subMenu = NSMenu()
let menuItem = mainMenu.addItem(withTitle: "Application", action: nil, keyEquivalent: "")
let titleQuit = NSLocalizedString("Quit", comment: "Quit menu item") + " " + appName
let menuItemQuit = subMenu.addItem(withTitle: titleQuit, action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q")
menuItemQuit.target = NSApp
mainMenu.setSubmenu(subMenu, for: menuItem)
NSApp.mainMenu = mainMenu
}
func setupWindow() {
window.center()
window.styleMask.insert(.closable)
window.styleMask.insert(.miniaturizable)
window.backgroundColor = NSColor.controlBackgroundColor
window.title = appName
}
func setupDockIcon(path: String) {
let icon = NSImage(byReferencingFile: path)!
icon.size = CGSize(width: 128, height: 128)
NSApp.applicationIconImage = icon
}
func setupLabel(title: String) {
label.isBezeled = false
label.isEditable = false
label.font = NSFont.systemFont(ofSize: 40)
label.alignment = .center
label.stringValue = title
}
func setupIcon(appPath: String, imageView: NSImageView) {
let appIcon: NSImage = NSWorkspace.shared.icon(forFile: appPath)
appIcon.size = NSSize(width: 80.0, height: 80.0)
imageView.image = appIcon
}
func setupButton(title: String, button: NSButton) {
button.bezelStyle = .regularSquare
button.font = NSFont.systemFont(ofSize: 20)
button.title = title
}
func setupHiddenIcon(path: String, imageView: NSImageView) {
let icon: NSImage = NSImage(byReferencingFile: path)!
icon.size = NSSize(width: 30.0, height: 30.0)
imageView.image = icon
imageView.isHidden = true
}
// Button actions
@objc func mailButtonClicked(sender: AnyObject) {
print("macOS Mail selected")
print("Current settings:")
defaultApp.getDefaultApp(handler: DefaultApp.utiHandlerMicrosoft)
defaultApp.getDefaultScheme(handler: DefaultApp.urlSchemeMicrosoft)
defaultApp.setDefaultApp(handler: DefaultApp.utiHandlerApple)
defaultApp.setDefaultScheme(handler: DefaultApp.urlSchemeApple)
// Handle icon visibility
mailButtonClickedIcon.isHidden = false
outlookButtonClickedIcon.isHidden = true
}
@objc func outlookButtonClicked(sender: AnyObject) {
print("Outlook selected")
print("Current settings:")
defaultApp.getDefaultApp(handler: DefaultApp.utiHandlerMicrosoft)
defaultApp.getDefaultScheme(handler: DefaultApp.urlSchemeMicrosoft)
defaultApp.setDefaultApp(handler: DefaultApp.utiHandlerMicrosoft)
defaultApp.setDefaultScheme(handler: DefaultApp.urlSchemeMicrosoft)
// Handle icon visibility
outlookButtonClickedIcon.isHidden = false
mailButtonClickedIcon.isHidden = true
}
// Required app delegate method
func applicationDidFinishLaunching(_ aNotification: Notification) {
setupUI()
}
// Close app when toolbar red button is pushed
func applicationShouldTerminateAfterLastWindowClosed(_ app: NSApplication) -> Bool {
return true
}
}
// Setup app delegate and run the app
let thisApp = NSApplication.shared
NSApp.setActivationPolicy(.regular)
let appDelegate = AppDelegate()
thisApp.delegate = appDelegate
thisApp.run()