Mac Sandbox issues with opening embedded RTF files.

My Mac programs usually ship with some internal Rich Text files containing legal details.  I use the NSWorkspace openFile call to open the files within TextEdit.



The code looks something like this: 



guard let aPath = Bundle.main.path(forResource: “Legal.rtf”, ofType: nil) else { return }

NSWorkspace.shared.openFile(aPath, withApplication: nil)



This has always worked, until recently when this code returns “The application can’t be opened. -50”.  Is that a Sandbox issue?  Accessing files within your bundle should be allowed. We do it for images and such.



What do I have set wrong?



Thank you!
There are some low-level subtleties involved when opening documents via the sandbox. I don't know the exact details to tell you why this is happening, but I can give you some suggestions on how to solve and/or work around it.

First of all, if you are doing this, never ever specify nil as the application. There is no way for you to know what the user has done on their computer. Always specify the exact, Apple-supplied system app that you want to use when opening the document.

Next, don't do this at all. You can't really guarantee the existence of any specific Apple app. As Apple would say, the existence of Apple apps should not be considered "API". And on a general user-experience level, this is bad. It dumps the user out of your app. You don't ever want to do that.

What you want to do instead is have your own document viewer. All you need is a window with a rich text editing view. You can read the RTF, convert it to attributed string, and stuff it into the view. Takes 5 minutes. If you want to get extra fancy, there are lots of opportunities for that too. I have lots of RTF files in my app and this is how I display them. For the license agreement, I even have logic to make sure they scroll all the way to the bottom before they can agree. In some cases, I adjust the background colours so it doesn't look like a text view. I always turn editing off. For short snippets of text, I use the width of the window and get the ideal height. Then I set an auto layout fixed height. This makes sure that the window automatically sizes to adjust for the content. This is helpful for localization. Don't forget to check in dark mode. You might have to do a little extra work there.
Mac Sandbox issues with opening embedded RTF files.
 
 
Q