Sandboxing and revealing folders

Dear friends,

I am working my way thru apple’s sandbox requirements.

I have an app that used to write documents in the user’s documents folder, it sounds very reasonable to me.

But apple no longer wants me to do this.

I am obliged to write the users documents in a difficult to access by the ordinary user. LION hides the users documents folder.

I want to be elegant with my user, and so I have a menu that once clicked will “reveal” to the user where his documents are located, in case he wants to.

This is the code I used, before sandboxing…

on revealSelectedFolder_(sender)
        (*** this handler will reveal the selected folder in Finder: ****)
        set selectedFolder to theFolderArrayController's selectedObjects()'s lastObject()
        set folderExists to  filePresenter's validateFolderData_(theFolderArrayController's selectionIndex())
        if folderExists as integer is 1 then
            set chosen_directory to selectedFolder's valueForKey_("folderURL")
            set chosen_directory to chosen_directory's |path|()
            set chosen_directory to chosen_directory as string
            tell application "Finder"
                set chosen_directory to chosen_directory as POSIX file 
                --reveal chosen_directory
                open chosen_directory
            end tell
            log "error on revealing selected folder..."
        end if
    end revealSelectedFolder_

To sandbox my app, I can no longer talk to “Finder”.

I am studying File Manager Reference, and it seems that it is not capable of doing this.

I can read, count, manipulate the contents, but opening a folder to the user is difficult for me.

Can you please post me your suggestions?


You can’t talk to the Finder, but you can use something like this in Obj-C:

[[NSWorkspace sharedWorkspace] selectFile:pathOfDestinationFolder.path inFileViewerRootedAtPath:@""];

Which in ASOC would be (untested):

tell current application's NSWorkspace's sharedWorkspace() to selectFile_inFileViewerRootedAtPath_(pathOfDestinationFolder's |path|()), "")

You need to provide a string path from an NSURL, or a POSIX-style path. Like I said, untested, but this should point you in the right direction.


And you should use NSOpenPanels and NSSavePanels as much as possible to comply with sandboxing.

Basically, you can access any folder and file the user himself/herself selected or dragged and dropped onto a Ui element of some kind (like an NSPathControl), and everything inside it (if it’s a folder). You cannot go back to the parent folder yourself, the user has to do it. I think you can go back to folders that have been used by the user before until the app quits, but I don’t see the point.

This is sad but necessary. You have to design your app with these limitations in mind. But it’s not so bad.

Saving URLs in the shared user defaults for use later when your app is relaunched is a bit tricky but possible.

Dear Leon,

Sorry for the late reply.

It worked. Thanks!

I don’t know why really, but I had to change it a little bit to this:

set sandBoxPath to sandBoxURL's |path|()

tell current application's NSWorkspace's sharedWorkspace() to selectFile_inFileViewerRootedAtPath_(sandBoxPath, "")

Yeah, it’s possible. ASOC is picky like this sometimes… :slight_smile:

Glad to know it worked!