Our app has a note section that allows you to drag and drop an email to it. Our app then adds a link to the note, that when clicked, will open the email again.
Currently andy email dragged from the mail.app doesn't have a problem. Works fine. The issue is with other mail clients at least with Outlook. When a user drags and drops an email from Outlook, we add the link, and if you click the link it will open fine. The problem is that this only works as long as you don't close our app. If you close our app, re-open and click the Outlook email link, we get a permission error.
The application “DragAndDropEmails” does not have permission to open “Re- Customize Login- Registration Form.eml.”
“NSCocoaErrorDomain” - code: 256
“NSURL” : “file:///Applications/Microsoft%200utlook.app/”
“NSLocalizedDescription” : “The application “Microsoft Outlook” could not be launched because a miscellaneous error occurred.”
“NSUnderlyingError” : domain: “NSOSStatusErrorDomain” - code: 18446744073709551562
When a drop is made to the text view we check for an email drop like this:
private func checkForEmailDrop(_ pasteboardItems: [NSPasteboardItem]?) -> Bool {
for item in pasteboardItems ?? [] {
for type in item.types {
switch type.rawValue {
// When using Apple Mail, the following type is returned.
case "com.apple.mail.PasteboardTypeMessageTransfer":
if let itemURL = item.string(forType: .URL) {
if let url = URL(string: itemURL){
// Get the email subject.
let subjectName = item.string(forType: .string)
self.textStorage?.op1InsertMailMessageLink(withURL: url, subjectText: subjectName)
return true
}
}
// When using Outlook, the file promise type is returned and then we must also check to make sure the file is a .eml file (email file).
case "com.apple.NSFilePromiseItemMetaData":
if let itemFileURL = item.string(forType: .fileURL) {
if let itemURL = URL(string: itemFileURL) {
if itemURL.pathExtension == "eml" {
// Get the email subject.
let subjectName = itemURL.deletingPathExtension().lastPathComponent
self.textStorage?.op1InsertMailMessageLink(withURL: itemURL, subjectText: subjectName)
return true
}
}
}
default:
break
}
}
}
return false
}
When the user clicks the link, we open it like this:
let configuration = NSWorkspace.OpenConfiguration()
let urlWithScheme = url.psoAddingSchemeIfNeeded()
let appURLWithScheme = appURL.psoAddingSchemeIfNeeded()
print(urlWithScheme)
print(appURLWithScheme)
NSWorkspace.shared.open([urlWithScheme], withApplicationAt: appURLWithScheme, configuration: configuration) { app, error in
if let error = error {
print("Failed to open link in specific app: \(error.localizedDescription)")
}
}
contents of urlWithScheme: "file:///Users/joseines/Library/Containers/com.microsoft.Outlook/Data/tmp/FB810D5A-D544-46D9-9BD0-65067A3D7DE4/Re-%20Customize%20Login-%20Registration%20Form.eml"
contents of appURLWithScheme:
file:///Applications/Microsoft%20Outlook.app/
How can we let the system know that the user wants access to this file? How can we open the file after a restart?