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
I have an array of file urls from the user defaults that I get with this:
Then in the tableview I just show the file name from the URL.
The problem is I don't really understand swift and in the code you linked there's this
with the exception of that opendatabasecommand I have implemented all of the rest of the code show in the thread you linked, which I had also started. But I still end up with the warning that I am going to overwrite an existing file when I use File->save of command-s. So either way unlike the recent documents menu item, the documents opened via my custom recent documents are not recognized correctly.