0

So i've been searching around for a way to do the following, sequentially in Mail.app, using AppleScript:

  1. intercept an outgoing message (let's call this "A")
  2. make and exact duplicate message (let's call this "B") of the outgoing message "A"
  3. format the text/content etc of this newly made message ("B")
  4. send this newly formatted message ("B")
  5. disregard the original one ("A").

Why would i want to do something that appears to be so retarded?

  • Apple's Mail.app amazingly doesn't format the outgoing content of your messages.
  • By doing the basic Message > Preference > etc etc it only changes the font-style of the message that you see through Mail.app. When your recipient gets the message, the font chosen is that of the client's default (which can be an abhorring Times New Roman for clients like Outlook).

So why not just ask for an Applescript that formats your outgoing message directly?

Still learning the ropes with Applescript, so would greatly appreciate it if anyone could cook up an AppleScript that does the requested.

My intention is to trigger the Applescript when i hit "Send Message" (through the lovely Keyboard Maestro).

Here's the skeleton applescript code i could come up with:

set theFont to "Lucida Sans, Lucida Sans Unicode, Lucida Grande, Verdana, Arial"
set theSize to 12
tell application "Mail"
    -- 1. intercept an outgoing message
    set outMsg to front outgoing message


    -- 2. make and exact duplicate of this outgoing message
    -- need a little help with extracting...
            set fmtMsg to make new outgoing message with properties 
        {   sender:,
            subject:”Convert”, 
            content:”Please convert and send”
            message signature:,
            to recipient:,
            cc recipient:,
            bcc recipient:
        }


    tell fmtMsg
                    -- 3. format the text/content etc of this newly made message
        set font of content to theFont
        set size of content to (theSize)
        -- set visible to true

                    -- 4. send this newly formatted message
        send

                    -- 5. disregard the original one.
                    -- help?
    end tell


end tell

Cheers.

KG -
  • 7,130
  • 12
  • 56
  • 72
  • Please can you give Answer of this question http://stackoverflow.com/questions/13890369/not-able-to-execute-applescript-within-mail-application – Muhammad Rizwan Dec 15 '12 at 08:19

1 Answers1

1

I haven't worked with Mail that much, but I've composed a script that should do the job...

set the mail_recipient to the text returned of (display dialog "To:" default answer "")
set the mail_subject to the text returned of (display dialog "Subject:" default answer "")
set the mail_content to the text returned of (display dialog "Enter your message here (for a new line type \"\\n\"):" default answer "")
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to "\\" & "n"
set this_text to every text item of the mail_content
set AppleScript's text item delimiters to return
set the mail_content to this_text as string
tell application "Mail"
    tell (make new outgoing message with properties {visible:true, subject:mail_subject, content:mail_content})
        make new to recipient at the end of to recipients with properties {address:mail_recipient}
        send
    end tell
end tell

I haven't added error checking in the script (check for a valid address for the recipient).

As you said, you can't change the recipient's font; that might be considered harassment.

If this script isn't satisfactory, just let me know and I'll change it for you. :)

UPDATE: I just read up on using AppleScript with Mail, and just realized that Mail has huge limitations regarding AppleScript. I think your best bet would be to use GUI Scripting. This script is kind of hacky, but it should work.

tell application "System Events" to tell process "Mail" to set the mail_subject to (get the value of the first text box)
tell application "Mail" to set this_message to the content of the first message of mailbox "Sent" whose subject is the mail_subject

UPDATE 2:

tell application "System Events"
    tell process "Mail"
        set the mail_recipient to (get the value of the first text field)
        set the mail_subject to (get the value of the fourth text field)
        set the mail_content to (get the value of the last text field)
        my create_new_message(mail_recipient, mail_subject, mail_content)
    end tell
end tell

on create_new_message(recipient, subject, content)
    tell application "Mail"
         quit saving no
         activate
         tell (make new outgoing message with properties {subject:|subject|, content:|content|)
             make new recipient at the end of to recipients with properties {address:|recipient|}
             send
         end tell
    end tell
end create_new_message
fireshadow52
  • 6,298
  • 2
  • 30
  • 46
  • hey thanks!while definitely useful,i don't think it covers my exact requirement.I wanted to create a mail message "from an existing outgoing message".Essentially,I've already composed my message, added the recipients, subject etc etc.Right after hitting Send,I want the Applescript to recreate the whole message > set the formatting and then send.I take it ur script would prompt for To,Subject etc.I want the script to pick this up from the existing Send Messg window that's already filled.(Another advantage:even if i'm replying/forwarding my message, the recreating process would pick it all up). – KG - Sep 04 '11 at 18:43
  • yeah figured i might have to resort to some sort of GUI scripting as the second link in my post suggests. You're altered solution though: picks a mail from the "sent" box, which means I've already sent the message? I want to be able to create a new message with the same properties of the "going to be sent" one, set the formatting on the new one(which i can do with AppleScript as put in my qn), "discard" the "going to be sent" one and only send the new one. so essentially i should land up with only one sent message. does that make sense? (Updated my Question in an attempt to make it clearer) – KG - Sep 04 '11 at 20:35
  • yup i think this should satisfy exactly what i want. getting some compilation errors on the script though, **Expected "," but found identifier** at the line : set the mail_recipients to (get the value of the first text **box**) – KG - Sep 05 '11 at 08:43
  • @Kaushik Gopal Oops! That's my fault; I forgot that it's `text field` not `text box`. Sorry! – fireshadow52 Sep 05 '11 at 11:21
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3186/discussion-between-fireshadow52-and-kaushik-gopal) – fireshadow52 Sep 05 '11 at 17:48
  • Please Answer this question http://stackoverflow.com/questions/13890369/not-able-to-execute-applescript-within-mail-application – Muhammad Rizwan Dec 15 '12 at 08:20