0

I try to add a service to my application. This application has a service called, let's say, "MyService". I have a file in the Finder and I would like to copy that file with a new name at the same location. The user would right-click on the file and select MyService. A new file would appear with a new filename next to the original one. The problem is that my application is sandboxed. I can get the url of the file selected with :

- (NSPasteboardItem*)merge:(NSPasteboard *)pboard
 userData:(NSString *)userData error:(NSString **)error {
    NSArray* fileArray=[pboard propertyListForType:NSFilenamesPboardType];
}

My fileArray would contain the filepath. I can then change this filepath with the new filename. If I just create a new file using :

NSString *filePath = [filesArray objectAtIndex:0];
PDFDocument *PDF = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:filePath]];
NSURL *newFileUrl =[NSURL fileURLWithPath:newfilepath];
[PDF writeToURL:newFileURL];

I get an error! I know what I am doing is wrong but how to tell the Finder that the User did commit the action and so this action is secured in a way. How to tell this newFileURL is allowed? Thank you for your help

mahal tertin
  • 3,239
  • 24
  • 41
  • You get an error. Could you specify what's the error message? – Yuchen Jan 25 '15 at 19:36
  • Use NSSavePanel to let the user specify the path where to save the file. – El Tomato Jan 25 '15 at 22:54
  • NSSavePanel would work but you need go back in the application. I would like something like 'the unarchiver'. When you right click on a file, you simply uncompress a folder at the same location. You do not have a Save Panel that appears. This is the easiest solution for the user – AllezLesBleus Jan 26 '15 at 10:03

1 Answers1

1

Sorry to say but not possible in sandbox. You'll have to ask the user for permission to create the new file. It would only work in the very specific case of just changing the file extension as described in Related items and here.

Alternatively you could ask the user once for permission to write in the whole directory, store a Security Scoped Bookmark (SSB) and regain access on each launch of your app.

EDIT: The Unarchiver from Mac App Store actually does it exactly this way, just tested. It publishes three NSServices called "Unarchive To Current Folder", "Unarchive To Desktop" and "Unarchive To...". They all ask for permission at least once.

To ask for permission for a whole directory use a NSOpenPanel as in this answer and the appropriate properties setCanChooseDirectories:YES and setCanChooseFiles:NO then read and store URL with SSB.

Community
  • 1
  • 1
mahal tertin
  • 3,239
  • 24
  • 41
  • Yes, it is something like that that I was looking for! How do I ask the user for permission on the whole directory? I don't think that The Unarchiver asks the permission to the User? I can unarchive files wherever I want on my system! – AllezLesBleus Apr 20 '15 at 15:52