0

I am trying to save a selected email from Apple Mail using ScriptBridge.

I have already created the Mail.h file and in my program I have successfully done other things with the Apple Mail ScriptBridge (like forwarding messages etc.)

Here is my current code. I get no error messages and the code is running fine; only the file never gets created.

I am using Xcode 4.6. on Mountain Lion 10.8.2. The deployment target of my app is 10.8.

- (void)saveEmail {
    MailApplication *mailApp = [SBApplication applicationWithBundleIdentifier:@"com.apple.Mail"];

    SBElementArray *viewers = [mailApp messageViewers];

    for (MailMessageViewer *viewer in viewers) {

        NSArray *selectedMessages = [viewer selectedMessages];

        @try {
            for (MailMessage *selectedMessage in selectedMessages) {

                NSString *filePath = [NSString stringWithFormat:@"%@%@",@"/Users/patrick/Documents/",@"tmp.rtf"];
                NSURL *fileUrl = [NSURL fileURLWithPath:filePath];

                [selectedMessage saveIn:fileUrl as:MailSaveableFileFormatNativeFormat];
            }
        }
        @catch (NSException *exception) {
            NSLog(@"Exception:%@", exception);
        }
    }
}
Palladion
  • 1
  • 1

1 Answers1

0

Have exactly the same problem. Seems saveIn isn't implemented in Mail. I have a workaround with this code:

[message.source writeToURL:mailUrl
                            atomically:YES
                              encoding:NSUTF8StringEncoding
                                 error:nil];

Where message is an instance of MailMessage This seems to work correctly most of the time. But sometimes the attachments are empty in the saved mail. So if anyone has a better solution...

Marius
  • 258
  • 1
  • 9