Created
July 29, 2017 04:23
-
-
Save wtfaremyinitials/2fb27910da98bbd52c6d2ebfb53f47d5 to your computer and use it in GitHub Desktop.
Accept deep-links to Mail.app messages through macOS drag and drop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Accept deep-links to Mail.app messages through macOS drag and drop | |
class MailURLDropView: NSView { | |
var filePath: String? | |
required init?(coder: NSCoder) { | |
super.init(coder: coder) | |
self.wantsLayer = true | |
self.layer?.backgroundColor = NSColor.gray.cgColor | |
let draggedType = NSPasteboard.PasteboardType(kUTTypeURL as String) | |
registerForDraggedTypes([draggedType]) | |
} | |
override func draw(_ dirtyRect: NSRect) { | |
super.draw(dirtyRect) | |
} | |
override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation { | |
if extractURL(sender.draggingPasteboard()) == nil { | |
return NSDragOperation() | |
} | |
self.layer?.backgroundColor = NSColor.blue.cgColor | |
return .copy | |
} | |
override func draggingExited(_ sender: NSDraggingInfo?) { | |
self.layer?.backgroundColor = NSColor.gray.cgColor | |
} | |
override func draggingEnded(_ sender: NSDraggingInfo?) { | |
self.layer?.backgroundColor = NSColor.gray.cgColor | |
} | |
override func performDragOperation(_ sender: NSDraggingInfo) -> Bool { | |
guard let url = extractURL(sender.draggingPasteboard()) else { | |
return false | |
} | |
print(url) | |
return true | |
} | |
func extractURL(_ pasteboard: NSPasteboard) -> URL? { | |
guard let data = pasteboard.data(forType: NSPasteboard.PasteboardType(rawValue: "public.url")) else { | |
return nil | |
} | |
guard let string = String(data: data, encoding: String.Encoding.utf8) else { | |
return nil | |
} | |
guard let url = URL(string: string) else { | |
return nil | |
} | |
if url.scheme != "message" { | |
return nil | |
} | |
return url | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment