Can't open file user granted access to.

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?

I’m assuming that this is on the Mac.

When someone drags a file into your app, you’re given a security scoped URL. If you want to preserve access to that file, you create a security-scoped bookmark for it. See the Persist access to files with security-scoped URL bookmarks section of Accessing files from the macOS App Sandbox.

So far, so straightforward. Where things get weird is when you try to open the URL. Normally when you have a security-scoped bookmark for a file you:

  1. Resolve the bookmark to a security-scoped URL.

  2. Call startAccessingSecurityScopedResource().

  3. Read the file.

  4. Call stopAccessingSecurityScopedResource().

It’s possible that the just opening the URL in step 3 will be sufficient to resolve this issue, but I must admit that I’ve never actually tried this.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Can't open file user granted access to.
 
 
Q