Mirza Bilal
Mirza Bilal

Reputation: 1050

Reading a file in Mac OSX Sandbox

I am trying to read a file in sandbox mode outside application sandbox. I went through this objColumnist post, and definitely Apple's App Sandbox Design Guide documentation.

Documentation clearly states that you need a NSOpenPanel or NSSavePanel to read/write files in Security-Scoped Bookmarks.

If your app employs a download or processing folder that is outside of the app container, obtain initial access by presenting an NSOpenPanel dialog to obtain the user’s intent to use a specific folder

My use case is a bit different, I want to read a file from finder. Like when user triggers some hotkey from finder, i will get the selection from finder (using scripting bridge to get file selection) and then read that file. Hence i can not create a Security-Scoped Bookmarks.

So is there an alternative way to read a bookmark file. Or is there other way other than scripting bridge to get finder selection that fits in with app sandbox as well.

Upvotes: 1

Views: 1275

Answers (2)

mahal tertin
mahal tertin

Reputation: 3414

There is a way to get this done: Use NSService to publish a File-Service in the Finder > Services Menu https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/SysServices/Articles/overview.html#//apple_ref/doc/uid/20000850-BCIDHJJA

The user selects a file in the Finder, calls the service thru either the context menu, a shortcut/hotkey or the Finder menu. You'll receive a security scoped bookmark on which you then can access the file.

The way with AppleScript will not be allowed in the Mac App Store as you cannot get an entitlement to script Finder.

Upvotes: 1

Rob Sanders
Rob Sanders

Reputation: 5387

Except for Downloads, Pictures, Movies and Music, you definitely need the user's consent to read a file.

Your app can register to read certain types of file using UTI's and then you can implement the appropriate app delegate method to open a file that the user double clicks in the finder. Like how pages opens when the user double clicks a pages document.

Apart from that, (unless you don't use sandboxing) your app is restricted for security reasons. The only way through the sandbox is by using powerbox, which is handled by the NSOpenPanel and NSSavePanel transparently and cannot be accessed directly by you, the developer.

Upvotes: 0

Related Questions