Creating "Application Scripts" subfolder in sandboxed environment

I'm developing a sandboxed application, which should run some scripts and following sandboxing rules, those should be located in "~/Library/Application Scripts/com.mydomain.myapp". The application also tries to be friendly to users and offer them to "install" predefined scripts (bundled in the application package) into that folder. To gain access to the application script folder, I use standard method of the NSFileManager, so the code looks something like:


NSError *error;
NSURL *scriptsFolder = [[NSFileManager defaultManager] URLForDirectory:NSApplicationScriptsDirectory
                                                              inDomain:NSUserDomainMask
                                                     appropriateForURL:nil
                                                                create:YES
                                                                 error:&error]


This works fine if the scripts subfolder already exists. However, it if doesn't, the application can't create it. If I pass "NO" as fourth parameter I get back the url, but it's useless (it contains only the path), since the folder is nonexistent. If I pass "YES" as fourth parameter, so that the folder is created if necessary, I get the following error:


Error Domain=NSCocoaErrorDomain Code=513 "You don’t have permission to save the file “com.mydomain.myapp”
in the folder “Application Scripts”."
UserInfo={NSFilePath=/Users/milke/Library/Application Scripts/com.mydomain.myapp,
NSUnderlyingError=0x608000259bf0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}


Is there any way to create "com.mydomain.myapp" subfolder inside "Application Scripts" folder without defining temporary exception entitlement or explicitly prompting user to create it him/herself? If not, should this be considered a bug?


On a related note, when I started implementing and testing this feature, the folder "com.mydomain.myapp" was already in the "Application Scripts". When do those applications specific script subfolders get created and by which process? Once I've deleted it I can't seem to make the system to create it again.


Thanks for any info.

-- Dragan

I also have a sandboxed app with the ability to add user scripts, and noticed the same behaviour you described.


Though it's definitely not an ideal solution, it seems like manually removing the container corresponding to your application's bundle identifier in ~/Library/Containers, and then relaunching your app causes the corresponding Application Scripts subfolder to get created.


I would probably submit a bug report for this, as I would expect the Application Scripts subfolder to get created the next time the app launches if it is missing, regardless of the existence of the app's container.

I'm also not sure if an API to programatically allow creating the subfolder would be consistent, as the app sandbox only allows filesystem changes within its container?

You are correct: removing the container in "~/Library/Containers" and relaunching the application resulted in the "Application Scripts" subfolder being created again. Thanks for this clarification.


I realise the API for creating the subfolder would probably not be consistent, especially since the application is granted just read-only access to the scripts subfolder, but it would really be useful if, like you've pointed out, the subfolder is created (if it doesn't exist) the next time the application launches regardless of anything.


For now I can only hope users won't manually mess with folders "hidden" inside "~/Library", so this won't be a big issue. If the fourth parameter is "NO", only the plain file-path URL is returned, which can't be revealed (as the subfolder doesn't exist) and the application does nothing as a result of user action. Therefore I will set the fourth parameter to "YES". That way, users will be presented with an error at least, and if they report it I'll know what the issue is and how to fix it.

Creating "Application Scripts" subfolder in sandboxed environment
 
 
Q