Skip navigation

Applescript to forward and change subject?

2010 Views 10 Replies Latest reply: Dec 10, 2012 11:12 AM by Pierre L. RSS
germano.correa Calculating status...
Currently Being Moderated
May 4, 2012 11:38 AM

Hi,

 

I'm striving to make an Applescript to forward and change the subject of some messages filtered by some rules at Apple Mail.

 

This is what I'm using:

 

using terms from application "Mail"

          on perform mail action with messages theSelectedMessages for rule RRRR

                    repeat with eachSelectedMessage in theSelectedMessages

  forward eachSelectedMessage

                              set subject of eachSelectedMessage to yyy

                              set recipients to "aaa.bbb@xxx.com"

                    end repeat

          end perform mail action with messages

end using terms from

 

The script is correctly forwarding the messages, but it isn't changing the subject of them.

 

Any tips?

 

 

Thanks.

 

Germano

Mac OS X (10.7.3), Applescript
  • Camelot Level 8 Level 8 (45,670 points)
    Currently Being Moderated
    May 4, 2012 11:48 AM (in response to germano.correa)

    You're doing it wrong

     

    There are two messages at play here. The first is eachSelectedMessage - the original, incoming message, and the forwarded message.

     

    If you look at the definition of forward in Mail.app's dictionary you'll see that it takes a message and returns a new outgoing message. It's this new outgoing message that you need to target, but your script it trying to change the subject and recipients of the original message.

     

    You're also creating the recipients in the wrong way. Recipients are elements of a message and can't be set in this way.

     

    Try:

     

    using terms from application "Mail"

              on perform mail action with messages theSelectedMessages for rule RRRR

                        repeat with eachSelectedMessage in theSelectedMessages

                                  set newMessage to forward eachSelectedMessage

                                  set subject of newMessage to yyy

      make new to recipient at end of recipients of newMessage with properties {address:"aaa.bbb@xxx.com"}

                        end repeat

              end perform mail action with messages

    end using terms from


    You'll also need to include a 'send newMessage' command if you want to send the message, otherwise it'll sit in your drafts box.

  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    May 5, 2012 10:32 PM (in response to germano.correa)

    You still have to direct the commands to Mail with a Mail tell block, even inside a rule action, like so (note that I also corrected a typo - yours should have said 'at end of to recipients'):

     

    using terms from application "Mail"

              on perform mail action with messages theSelectedMessages for rule RRRR

                        repeat with eachSelectedMessage in theSelectedMessages

                                  tell application "Mail"

                                            set newMessage to forward eachSelectedMessage with opening window

                                            tell newMessage

                                                      set subject to yyy

                                                      make new to recipient at end of to recipients with properties {address:"aaa.bbb@xxx.com"}

                                            end tell

                                  end tell

                        end repeat

              end perform mail action with messages

    end using terms from

  • Jacques Rioux Level 4 Level 4 (1,925 points)
    Currently Being Moderated
    May 6, 2012 4:08 PM (in response to germano.correa)

    germano.correa wrote:

     

    This suggestion hasn't worked either. =/

    Does the variable yyy is defined?

     

    If not, use this :

    set subject to  "the subject of your choice"

  • Braveheart1954 Calculating status...
    Currently Being Moderated
    Nov 26, 2012 3:19 PM (in response to germano.correa)

    Hello,

     

    I've got a similar problem and hope someone can help even though germano.correa's problem doesn't seem to have been solved. I'm tring to foward selected messages with the subject line changed so that the application receiving the forwarded email can created a trigger response from specific subject text. The AppleScript below works perfectly except the forwarded message contains 2 copies of the original message. I tried moving the "set subject line" higher and it creates 4 copies of the original message when I do that. Any help will be appreciated.

     

    tell application "Mail"

              set theSelection to selection

              set theForwardedMessage to forward (item 1 of theSelection) with opening window

              tell theForwardedMessage

      make new to recipient at end of to recipients with properties {address:"support@xxxyyy.com"}

     

                        set subject to "new subject text goes here"

     

              end tell

    end tell

  • Pierre L. Level 4 Level 4 (3,860 points)
    Currently Being Moderated
    Nov 26, 2012 6:49 PM (in response to Braveheart1954)

    Try replacing

           

            set subject to "new subject text goes here"

     

    with these lines:

     

            activate

            tell application "System Events" to tell process "Mail"

                set value of text field 1 of window 1 to "new subject text goes here"

            end tell

     

    (Before using the new code, you must enable access for assistive devices in the Accessibility System Preference pane.)


  • Braveheart1954 Level 1 Level 1 (0 points)
    Currently Being Moderated
    Dec 9, 2012 8:40 PM (in response to Pierre L.)

    Pierre, thanks for the script. The subject field of the fowarded message shows the new subject text in the foward message window but after it is sent it shows the text of the original message in both the "Sent" message folder and in the forwarded message received by the recipient. I enabled "access for assistive devices" as you suggested. It's very puzzling to me that the original script I posted works great except that it duplicates the content of the forwarded message. Any more ideas?

  • Pierre L. Level 4 Level 4 (3,860 points)
    Currently Being Moderated
    Dec 10, 2012 11:12 AM (in response to Braveheart1954)

    While testing your script, I have noticed the forwarded message contained two copies of the original message most of the time, but not always, whithout understanding why it sometimes worked as expected.

     

    Nevertheless, here's the workaround I've finally found out for those times when it doesn't work as expected:

     

    tell application "Mail"

        set theSelection to selection

        set theForwardedMessage to forward (item 1 of theSelection) with opening window

        tell theForwardedMessage

            make new to recipient at end of to recipients with properties {address:"support@xxxyyy.com"}

            activate

            tell application "System Events"

                keystroke "a" using command down -- A (select all)

                key code 51 -- (erase to the left)

            end tell

            delay 1

            set subject to "new subject text goes here"

            tell application "System Events"

                key code 117 -- (erase to the right)

            end tell

            send

        end tell

    end tell

     

    And no need to enable access for assistive devices this time!

     

    (I hope the script is self-explanatory.)


     



     

    Message was edited by: Pierre L.

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.