r/macosprogramming • u/B8edbreth • Jan 12 '24
Programmatically opening documnents
In my app I have a window similar to Xcode's recent projects window. When a user selects a recent item they worked on however, unlike with excode, the project comes up locked. So if they try to save it they get the "are you sure you want to over write the file" message.
In my tableview selection changed notification I have this code to open the file
BOOL success = [retString startAccessingSecurityScopedResource];
NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] init];
[fileCoordinator coordinateReadingItemAtURL:retString options:NSFileCoordinatorReadingForUploading error:&error byAccessor:^(NSURL *newURL) {
NSError * outError;
[[NSDocumentController sharedDocumentController]openDocumentWithContentsOfURL:retString display:YES error:NULL];
NSLog(@"Error %@",error);
if (success) {
[retString stopAccessingSecurityScopedResource];
}
}];If I used the method to open the file that isn't deprecated the file simply doesn't open and I get an error that I cannot access it.
Do I have to have a developer account and code sign the app to make this work?
Edit to add: I'm storing the recent items in my apps defaults file like this:
NSData * dta = [self.fileURL bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope includingResourceValuesForKeys:nil relativeToURL:nil error:&error];
View all comments
Show parent comment
1
u/B8edbreth Jan 13 '24
That's what I've been doing. I implemented the code you linked in my app. I mean the objective-c version of it
I guess I shouldn't call it an error. When I got to file->save rather than it just saving in place like a normal file you get through file->open. It asks where you want to save it, and if you don't change the name it tells you a file already exists at that path.