2013-07-10 10 views
7

Próbuję wysłać pocztę html z aplikacji Cocoa, przez Mail.app. Chcę otworzyć nową wiadomość w Mail.app, dołączyć temat, odbiorcę i dodać treść HTML z linkami i inną treścią. Ale nie może znaleźć sposobu, aby to zrobić. Próbowałem już mostu skryptowego, ale klasa MailOutgoingMessage nie ma typu zawartości i może dodawać zawartość w postaci zwykłego tekstu.Wysyłaj pocztę HTML z kakao za pomocą Mail.app

próbował AppleScript, coś takiego:

set htmlContent to read "/Path/index.html" 
set recipientList to {"[email protected]"} 

tell application "Mail" 
    set newMessage to make new outgoing message with properties {subject:"qwerty", visible:true} 
    tell newMessage 
     make new to recipient at end of to recipients with properties {address:"[email protected]"} 
      set html content to htmlContent 
     --send 
    end tell 
end tell 

ten kod wyślij e-mail z html, tylko jeśli mam zmieniając --send wysłać. Ale muszę wysłać list później, po wprowadzeniu przez użytkownika pewnych zmian.

Odpowiedz

1

Nie jest jasne, czego szukasz, ale dołożę wszelkich starań, aby zaoferować pomoc.

Jeśli zostawisz komentarz send, to wiadomość powinna już być otwarta w Mail.app, czekając na dalszą edycję i wysyłkę.

Po dodaniu wiersza save newMessage zostanie on zapisany w folderze wersji roboczych. Użytkownik może go otworzyć i kontynuować edycję, kiedy tylko zechce. Jeśli chcesz wysłać projekt ze swojej aplikacji, użyj:

set sendMessage to first message of drafts mailbox 
send sendMessage 

Powodzenia!

1

Nie widziałem potrzeby edytowania wiadomości przed wysłaniem, więc moja poprzednia odpowiedź była błędna. Tym razem powinno być poprawne.

Zasadniczo

  • zajmuje preformatowany plik RTF,
  • czyni go & umieszcza je w schowku,
  • tworzy nową wiadomość,
  • wypełnia pola,
  • ruchów fokus do treści wiadomości,
  • wkleja sformatowany schowek

Oto kod:

set textSubject to "HTML Test" 
set toAddress to "[email protected]" 
set toName to "John Doe" 

tell application "Mail" 
    do shell script "cat ~/Documents/RTF\\ File.rtf | textutil -stdin -stdout -convert rtf | pbcopy" 

    set refMessage to make new outgoing message with properties {name:toName, address:toAddress, subject:textSubject, visible:true} 
    tell refMessage 
     make new to recipient at end of to recipients with properties {name:toName, address:toAddress} 
    end tell 
end tell 

tell application "System Events" 
    tell application process "Mail" 
     set frontmost to true 
     set value of attribute "AXFocused" of scroll area 4 of window textSubject to true 
    end tell 
    keystroke "v" using {command down} 
end tell 

Ponownie, to działało w porządku na Snow Leopard

nadzieję, że pomogło.

+0

Zwykle [Link tylko odpowiedzi] (http://meta.stackexchange.com/questions/65277/are-link-only-answers-poor-practice) jest mile widziana. – Walls

+0

Ack. Thx dla heads up. – Vic

+0

uzyskanie błędu nie może ustawić wiadomości wychodzącej w kodzie. Czy możesz potwierdzić? –

5

Aby podsumowanie problem: Korzystanie AppleScript aby utworzyć wiadomość z treści HTML dla interaktywnej edycji nie działa (od OS X 10.9.2): formularz nowej wiadomości podchodzi z puste ciało.

ten należy uznać za bug i zachęcam wszystkich, aby dać jabłko wiedzieć na http://bugreport.apple.com - zastrzeżeniem: własność html contentmessage klasa jest nie zdefiniowane w Mail.sdef, słownik AppleScript Mail.app „s, więc przypisywanie HTML maja nie być oficjalnie wspieranym.

Jest obejście, ale to nie jest całkiem:

  • Utwórz wiadomość niewidocznie.
  • Zapisz jako jako wersję roboczą.
  • Otwórz wiadomość o roboczym, w którym to momencie zostanie wyświetlona treść HTML .

Wdrożenie tego jest trudne, ponieważ konieczne jest kilka obejść. Poniższy kod próbuje jej najtrudniejszy, ale:

Uwaga: Ponieważ kod wykorzystuje GUI włączony, dostęp do urządzeń pomocniczych musi być włączony (przez System Preferences > Security & Privacy > Accessibility) dla uruchomionej aplikacji tego kodu (np AppleScript Editor lub, jeśli prowadzony przez osascript, Terminal.app).

# Example values; use `read someFile` to read HTML from a file. 
set htmlContent to "<html><body><h1>Hello,</h1><p>world.</p></body></html>" 
set recipientList to {"[email protected]", "[email protected]"} 
set msgSubject to "qwerty" 

tell application "Mail" 

    # Create the message *invisibly*, and assign subject text 
    # as well as the HTML content. 
    set newMessage to make new outgoing message with properties ¬ 
     {visible:false, subject:msgSubject, html content:htmlContent} 

    # Add recipients. 
    # !! Given the workaround below, this is currently pointless. 
    tell newMessage 
     repeat with toRcpt in recipientList 
      make new to recipient at end of to recipients with properties {address:toRcpt} 
     end repeat 
    end tell 

    # Save the current number of drafts messages. 
    set draftCountBefore to count messages of drafts mailbox 

    # !! Save the new message as a *draft* - this is necessary 
    # for the HTML content to actually appear in the message 
    # body when we open the message interactively later. 
    save newMessage 

    # !! Sadly, it takes a little while for the new message 
    # !! to appear in the drafts mailbox, so we must WAIT. 
    set newMessageAsDraft to missing value 
    repeat with i from 1 to 30 # give up after n * 0.1 secs. 
     set draftCountNow to (count messages of drafts mailbox) 
     if draftCountNow > draftCountBefore then 
      set newMessageAsDraft to message 1 of drafts mailbox 
      exit repeat 
     end if 
     delay 0.1 # sleep a little 
    end repeat 

    # Abort, if the draft never appeared. 
    if newMessageAsDraft is missing value then error "New message failed to appear in the drafts mailbox within the timeout period." 

    # Open the new message as a *draft* message - this ensures that 
    # the HTML content is displayed and editable in the message body. 
    # !! The ONLY solution I found is to use `redirect`, which, unfortunately, 
    # !! *wipes out the recipients*. 
    # !! It does, however, ensure that the draft is deleted once the message is sent. 
    redirect newMessageAsDraft with opening window 

    # Activate Mail.app and thus the draft message's window. 
    activate 

    # !! Since the recipients have been wiped out, we need to 
    # !! add them again - unfortunately, the only way we can do that is to 
    # !! *GUI scripting* - simulating invocation of a menu command or 
    # !! sending keystrokes. 
    tell application "System Events" 

     # We must make sure that the target window is active before 
     # we can perform GUI scripting on it. 
     set newMessageWindow to missing value 
     repeat with i from 1 to 30 # give up after n * 0.1 secs. 
      tell (first window of (first process whose frontmost is true) whose subrole is not "AXFloatingWindow") 
       if name is msgSubject then 
        set newMessageWindow to it 
        exit repeat 
       end if 
      end tell 
      delay 0.1 # sleep a little 
     end repeat 
     if newMessageWindow is missing value then error "New message failed to become the active window within the timeout period." 

     # Turn the list of recipients into comma-delimited *string* for pasting into the To field. 
     set {orgTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {","}} 
     set recipientListString to (recipientList as text) 
     set AppleScript's text item delimiters to orgTIDs 

     # Save the current clipboard content. 
     set prevClipboardContents to the clipboard 

     # Cursor is in the "To:" field, so use GUI scripting to send the Edit > Paste command now. 
     # NOTE: Access for assistive devices must be enabled via System Preferences > Security & Privacy > Accessibility. 
     set the clipboard to recipientListString 
     my pasteFromClipboard("") 

     # Restore the previous clipboard content. 
     # !! We mustn't do this instantly, as our temporary content may not have 
     # !! finished pasting yet. It would be non-trivial to determine 
     # !! when pasting has finished (examining `count of to recipients` doesn't work), 
     # !! so we take our chances with a fixed, small delay. 
     delay 0.1 
     set the clipboard to prevClipboardContents 

     # Place the cursor in the message *body*. 
     # !! This works as of Mail.app on OS X 10.9.2, but may break in the future. 
     try 
      tell newMessageWindow 
       tell UI element 1 of scroll area 1 
        set value of attribute "AXFocused" to true 
       end tell 
      end tell 
     end try 

    end tell 

end tell 

(* 
Pastes form the clipboard into the active window of the specified application (process) using GUI scripting 
rather than keyboard shortcuts so as to avoid conflicts with keyboard shortcuts used to invoke this handler. 
Specify "" or `missing value` to paste into the currently active (frontmost) application. 
The target process may be specified by either name or as a process object. 

CAVEAT: While this subroutine IS portable across *UI languages*, it does make an assumption that will hopefully hold for 
all applications: that the "Edit" menu is the *4th* menu from the left (Apple menu, app menu, File, Edit). 

Examples: 
    my pasteFromClipboard("") # paste into frontmost app 
    my pasteFromClipboard("TextEdit") 
*) 
on pasteFromClipboard(targetProcess) 
    tell application "System Events" 
     if targetProcess is missing value or targetProcess = "" then 
      set targetProcess to first process whose frontmost is true 
     else 
      if class of targetProcess is text then 
       set targetProcess to process targetProcess 
      end if 
      -- Activate the application (make it frontmost), otherwise pasting will not work. 
      set frontmost of targetProcess to true 
     end if 
     tell menu 1 of menu bar item 4 of menu bar 1 of targetProcess 
      -- Find the menu item whose keyboard shortcut is Cmd-V 
      set miPaste to first menu item whose value of attribute "AXMenuItemCmdChar" is "V" and value of attribute "AXMenuItemCmdModifiers" is 0 
      click miPaste 
     end tell 
    end tell 
end pasteFromClipboard 
Powiązane problemy