community,
I need some advice about macOS sandboxed app. I've already spent couple of days fighting this issue, and so far no result. I suspect that my case will not work anymore in further macOS updates.
What my app does: My app takes a list of Evernote notes from Evernote.app directly through a Scripting Bridge, then it encrypts those notes, and then uploads them back.
The problem:
I cannot write attachment data to my app sandboxed folder since Sierra update.
Entitlement file of my app is like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.temporary-exception.apple-events</key>
<string>com.evernote.Evernote</string>
<key>com.apple.security.app-sandbox</key>
<true/>
</dict>
</plist>
I am using a scripting bridge to work with Evernote app from my sandboxed app:
EvernoteApplication *evernote = [SBApplication applicationWithBundleIdentifier:@"com.evernote.Evernote"];
Then I take a list of notes in a local notebook:
return [evernote findNotes:[NSString stringWithFormat:@"notebook:\"%@\"",notebookName]];
Then I go through the returned array of notes saving each of them attachments to NSTemporaryDirectory() + filename.
[notes enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
EvernoteNote *note = obj;
NSArray *localURLs = [self saveLocalAttachments:note.attachments];
[localURLs enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSURL *localURL = obj;
NSLog(@"URL for resource: %@", [localURL absoluteString]);
}];
}];
Function to save files is like this:
-(NSArray *)saveLocalAttachments:(NSArray *)attachments {
NSLog(@"Started converting resources to files: Total attachments:%lu",[attachments count]);
//
NSMutableArray *resourcesURLs = [[NSMutableArray alloc] initWithCapacity:[attachments count]];
[attachments enumerateObjectsUsingBlock:^(EvernoteAttachment * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
EvernoteAttachment *attach = obj;
//First we write data to local file (inside our sandbox)
NSString *filename = [attach.filename get];
NSURL *localURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:filename]];
NSLog(@"Local URL is:%@",[localURL absoluteString]);
@try {
[attach writeTo:localURL];
} @catch (NSException *exception) {
NSLog(@"Exception writing to local file. Exception: %@",exception);
//[self showAlertWithTitle:@"Writing Data" andMessage:[NSString stringWithFormat:@"Exception in writing temporary data. Exception name: %@, \n description: %@",exception.name, exception.description] andStyle:NSCriticalAlertStyle];
}
//NSLog(@"Attachment saved to: %@",[localURL absoluteString]);
//adding url to the array
@try {
[resourcesURLs addObject:localURL];
}
@catch (NSException *exception) {
NSLog(@"Exception adding URL to array of resourceURLs:%@",exception);
//[self showAlertWithTitle:@"Resource URL" andMessage:exception.description andStyle:NSCriticalAlertStyle];
}
}];
return resourcesURLs;
}
Function [attach writeTo:(URL)] gives me an error in console log:
SandboxViolation: Evernote(20296) deny file-write-create /private/var/folders/h8/1q71ktcd6fv_rbcd9ygkv6c80000gn/T/com.test.TestEvernoteBridge/example.pdf
Violation: deny file-write-create /private/var/folders/h8/1q71ktcd6fv_rbcd9ygkv6c80000gn/T/com.test.TestEvernoteBridge/example.pdf
Process: Evernote [20296]
What I've tried: - tested on El Capitan - everything is fine. - tried changing the entitlement file tags from string to array and adding there an exception of com.evernote.Evernote. - tried adding scripting entitlement - Evernote sdef doesn't support it. - create new project from scratch to test any update bugs - all the same. - removed the sandbox entitlement - the issue stays. - remove security exception - I don't get a list of notes (which is correct behaviour). - tried to allow to write to Downloads folder by adding another exception - the issue stays.
I can't figure out why it is not working and how can I get Evernote note attachment data. I suppose it is related to some mechanics of protecting sandboxed app NSTemporaryDirectory. I am doing something incorrectly here, and i need some advice.
Here is the test project - Test Project
Thanks in advance.
Apple gave an answer to my bug report - literally: "Duplicate of 29717097 (Closed)".
I don't see 29717097 report (SW release with correction) but it is still not working.
---UPDATE 08.04.2017 ----
Recent macOS Sierra 10.12.4 update solved the problem.