App Scoped Bookmark Entitlements Applescript

I am pretty new with Xcode, but have gotten pretty far on an applescript application. I am hitting a roadblock on finding out how to have a user select a folder that my app can read/write to, then when the app relaunches, to still have those read/write options. I found that the Seurity scoped bookmarks is what I am supposed to use, but can't figure out how, or what the script is, everything I have found is in cocoa.


this is what I have now



     tell application "Finder"         

          set backupfolder to (POSIX path of (choose folder with prompt "Choose where to backup" default location (((POSIX path of (path to home folder)) & "Dropbox") as string)) as string)      

     end tell                  

tell standardUserDefaults() of current application's NSUserDefaults             

          setObject_forKey_(backupfolder, "backupfolder")        

end tell


So what I'm wondering is, how can I make a user selected folder always available for read/write privileges, even after quitting the app, and that the user can also change the folder if needed.


Any help is greatly appreciated!

Xcode-OTHER, OS X Mavericks (10.9), Bookmark Entitlements Applescript

Posted on Nov 24, 2013 11:45 AM

Reply
3 replies

Nov 24, 2013 1:45 PM in response to BinEP

Pretty much every example you find will be in Objective-C, but usually this can be converted in order to use Cocoa with AppleScript in Xcode. Note that a bookmark is not the same as a filepath, and the filepath you get from choose folder is not an NSURL, either.


Although choose folder ultimately uses it, you should just use NSOpenPanel directly to get the URL. After getting the user's selection, you will then need to create a security scoped bookmark by using NSURL's bookmarkDataWithOptions:includingResourceValuesForKeys:relativeToURL:error: method. The resulting bookmark can be saved wherever, and later converted back to an URL with the same security scope. See the App Sandbox Design Guide for more information (and don't forget to set the appropriate entitlements).


FYI, there are also Mac OS X Technologies and Developer Forums communities, so similar topics in the future won't get lost in the more general purpose Mavericks community.

Nov 24, 2013 7:44 PM in response to red_menace

I tried the developer forums, but haven't gotten any resonses


I found something online that gave me this


I used the NSOpenPanel with this code


set backupfolder to current application's NSOpenPanel's openPanel()

backupfolder's setCanChooseFiles_(0)

backupfolder's setCanChooseDirectories_(1)

backupfolder's setAllowsMultipleSelection_(0)

backupfolder's runModal()



set backupfolderURL to backupfolder's URLs()


How would I get a file path from this?


Then the next command to convert the URL to a bookmark was


set aURL to backupfolderURL's objectAtIndex_(0)

set bookmarkData to aURL's bookmarkDataWithOptions_includingResourceValuesForKeys_relativeToURL_error_(Boo kmarkResolutionWithSecurityScope, missing value, missing value, missing value)


Except that it gets an error and says that NSURLBookmarkResolutionWithSecurityScope variable is undefined


Then, if I can get the previous command to work, How do I save the bookmark and recall it later and convert it to a file path to use for read and write access?

Nov 24, 2013 8:44 PM in response to BinEP

To get a file path you can use various NSURL methods such as |path| (the pipes are needed since path is an AppleScript term), but for this you will be using file URLs.


The various Cocoa classes and constants are defined in the AppleScriptObjC runtime, so if there isn't an object to determine the class you need to target the current application, for example:

current application's NSURLBookmarkCreationWithSecurityScope


The bookmark will be an NSData object, so you can stash it wherever using object:forKey:, and extract the file URL later with NSURLs URLByResolvingBookmarkData:options:relativeToURL:bookmarkDataIsStale:error: method.


You will also need to bracket the use of the security scoped bookmark with the startAccessingSecurityScopedResource and stopAccessingSecurityScopedResource methods.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

App Scoped Bookmark Entitlements Applescript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.