10 Replies Latest reply: Dec 10, 2012 11:12 AM by Pierre L.
germano.correa Level 1 Level 1 (0 points)

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
  • 1. Re: Applescript to forward and change subject?
    Camelot Level 8 Level 8 (45,790 points)

    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.

  • 2. Re: Applescript to forward and change subject?
    germano.correa Level 1 Level 1 (0 points)

    Thank's for the tip, but it hasn't worked. I've tried this:  

     

    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 convert

     

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

     

      send newMessage

     

                        end repeat

     

              end perform mail action with messages

     

    end using terms from

     

    Nothing happened, no drafts, no sent messages. Any other suggestions?

  • 3. Re: Applescript to forward and change subject?
    twtwtw Level 5 Level 5 (4,690 points)

    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

  • 4. Re: Applescript to forward and change subject?
    germano.correa Level 1 Level 1 (0 points)

    This suggestion hasn't worked either. =/

  • 5. Re: Applescript to forward and change subject?
    Jacques Rioux Level 4 Level 4 (1,960 points)

    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"

  • 6. Re: Applescript to forward and change subject?
    germano.correa Level 1 Level 1 (0 points)

    Yes, the variable yyy is defined. :/

  • 7. Re: Applescript to forward and change subject?
    Braveheart1954 Level 1 Level 1 (0 points)

    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

  • 8. Re: Applescript to forward and change subject?
    Pierre L. Level 4 Level 4 (3,860 points)

    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.)


  • 9. Re: Applescript to forward and change subject?
    Braveheart1954 Level 1 Level 1 (0 points)

    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?

  • 10. Re: Applescript to forward and change subject?
    Pierre L. Level 4 Level 4 (3,860 points)

    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.