I'm trying to retrieve attachments from Mail.app through scriptingbridge. I'm using the following code to get access to my inbox messages.
MailApplication *mailApp = [SBApplication applicationWithBundleIdentifier:@"com.apple.mail"];
MailMailbox *inbox = [mailApp inbox];
SBElementArray *messages = [inbox messages];
By iterating over the SBElementArray
, I can access an individual MailMessage
object.
Each MailMessage
object has a getter that retrieves another SBElementArray
, this time filled with MailMailAttachment
objects.
It's working, because if I put a NSLog(@"count:%lu",mailitem.attachments.count);
it will print the correct amount of attachments. File name and size are also correct printed.
But when I iterate over the attachments array and save it, nothing happens.
NSURL *url = [[NSURL new] initFileURLWithPath:@"/Users/usr/Documents/tmp/"];
NSLog(@"count:%lu",mailitem.attachments.count);
for (MailMailAttachment *attachment in mailitem.attachments) {
NSLog(@"name:%@",attachment.name);
NSLog(@"size:%ld",attachment.fileSize);
[attachment saveIn:url as:MailSaveableFileFormatNativeFormat];
}
I didn't found any documentation about this method. From header file, it says:
- (void) saveIn:(NSURL *)in_ as:(MailSaveableFileFormat)as; // Save a document.
I'm assuming that the NSURL is the place in which I want to store the file, but I have no idea about MailSaveableFileFormat
. I'm passing MailSaveableFileFormatNativeFormat
, that I found on header file, but as I said, nothing happens. Not even a simple error message.
I also tried to initialize the NSURL with a directory and a complete filepath (path + filename). Same result.
Does anyone has an example? How do I save it?
TIA,
Bob
I managed to solve the problem. I was initializing NSURL wrong... The code that worked is:
for (MailMailAttachment *attachment in mailitem.attachments) {
NSString *filePath = [NSString stringWithFormat:@"%@%@",@"/Users/usr/Documents/tmp/",attachment.name];
NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
[attachment saveIn:fileUrl as:MailSaveableFileFormatNativeFormat];
}