Scripting Bridge adds unwanted HTML code when addi

2019-08-29 20:27发布

问题:

Ok, so this one is a bit strange....

I have an HTML page in Safari which I want to send as an email. If I go the FILE menu and select "Mail Contents of this Page", it transfers as expected and looks correct.

However, if I use Scripting Bridge add recipients to the outgoing message, it adds the following code to the top of the message content area for EACH recipient added. In one example, with 24 recipients added, the resulting email contains 24 sequential repeats of the following code:

<div style=3D"font-family: = Helvetica; font-size: 12px; color: black; text-align: left;">
<br =class=3D"webkit-block-placeholder"></div>

which because of the way the div tag is rendered, creates 24 line breaks at the beginning of the email so that my HTML begins further down the page.

Anyone know why this code is getting inserted?

Here is the code I'm using for Scripting Bridge:

SafariDocument *safariDoc = [[[[safari classForScriptingClass:@"document"] alloc]
initWithProperties:[NSDictionary dictionaryWithObject:@"//private/var/tmp/mail.html" forKey: @"URL"]] autorelease];
    [[safari documents] addObject:safariDoc];

SafariWindow *safariWindow = [[safari windows] objectAtIndex:0];
        [safariWindow emailContentsOf:safariWindow.currentTab];
        mailMessage = [[[mail outgoingMessages] objectAtIndex:0] autorelease];

Here is the code I use to add recipients:

MailToRecipient *recipient = [[[[mail classForScriptingClass:@"to recipient"] alloc] 
                                   initWithProperties:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       [preferencesData bandName], @"name",
                                                       [preferencesData bandEmail], @"address",
                                                       nil]] autorelease];
    [[mailMessage toRecipients] addObject:recipient];

* EDIT *

I have found a work around for the problem, but I would still be interested in knowing WHY this code is getting inserted into the body when I add a recipient... The work around is as follows: I inserted the following into the CSS section of my HTML template...

div {
display:none;
}

Thus, the display ignores all of the div tags in the html template. I still have 24 copies of the above code, but they are essentially ignored which solves my visual layout problem.

If anyone has a better solution, please let me know!

回答1:

It seems that Apple does not allow you to access outgoing mail messages which you do not "own" or have created from within your program.

In the code above, I was trying to work around Apple's limitation of not letting you set your outgoing email to HTML (something else they don't like you to do) by having Safari "email contents of web page", thereby creating a new outgoing mail message in Apple Mail.

I then was attempting to take control of that message by assigning a reference to a variable as follows:

mailMessage = [[[mail outgoingMessages] objectAtIndex:0] autorelease];

A bit of research revealed that mailMessage was showing as "nil" even though the following code:

[[mail outgoingMessages] count];

was returning the correct value of 1.

So in theory, I shouldn't be able to add recipients to the HTML email created by Safari at all.

Why it works sometimes and doesn't work other times is beyond me. But when it does work, it also adds formatting tags into the body of the code as well. I suspect it's just a bug that hasn't been caught since it shouldn't be working at all.