6 Replies Latest reply: Aug 8, 2013 5:21 PM by twtwtw
James Brown15 Level 1 Level 1 (45 points)

I'm trying to write an AppleScript to Forward or Redirect an email. It works fine but doubles up the content when I change any of the new emails details. Eg the subject, 'to recipient', etc.

 

My code is:

 

 

tell application "Mail"

          set theSelection to selection

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

          tell theForwardedMessage

                         set subject to "{plain} " & subject

          end tell

end tell

 

This is under Mountain Lion, 10.8.2, Mail.app is version 6.2 (1499).

 

Any help would be much appreciated.

 

TIA, James.

  • 1. Re: AppleScript, Mail.app - Forward command doubles content - Mountain Lion
    Pierre L. Level 4 Level 4 (3,860 points)

    Your problem seems to be similar to the one discussed here. I've no idea why your script doesn't work as expected though. However, GUI scripting might be a solution. For example:

     

    tell application "Mail"

        set theSelection to selection

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

        set theSubject to subject of theForwardedMessage

        activate

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

            tell text field "Subject:" of window 1

                set focused to true

                set value to "{plain} " & theSubject

            end tell

        end tell

    end tell


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

  • 2. Re: AppleScript, Mail.app - Forward command doubles content - Mountain Lion
    James Brown15 Level 1 Level 1 (45 points)

    Thanks Pierre. That's really helpful.

     

    On its own that didn't work, but if I put in a 'display dialog' command it does. Ie this works:

     

    tell application "Mail"

              set theSelection to selection

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

              set theSubject to subject of theForwardedMessage

      activate

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

      display dialog "1"

                        tell text field "Subject:" of window 1

                                  set focused to true

                                  set value to "{plain} " & theSubject

                        end tell

              end tell

    end tell

     

     

    I also want the recipient filled in, but it doesn't work for me. In this code it displays the 'A' dialog then stops:

     

    tell application "Mail"

              set theSelection to selection

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

              set theSubject to subject of theForwardedMessage

      activate

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

      display dialog "1"

                        tell text field "Subject:" of window 1

                                  set focused to true

                                  set value to "{plain} " & theSubject

                        end tell

                        tell text field "To:" of window 1

      display dialog "A"

                                  set focused to true

      display dialog "B"

                                  set value to "jlbrown@bordo.com.au"

                        end tell

              end tell

    end tell

     

    Ie it never gets to the 'B' dialog.

     

    So perhaps that field is called something else? How do you find out what the 'To:' field is called?

     

    Thanks,

     

    James.

     


  • 3. Re: AppleScript, Mail.app - Forward command doubles content - Mountain Lion
    Pierre L. Level 4 Level 4 (3,860 points)

    Hi James,

    On its own that didn't work, but if I put in a 'display dialog' command it does. Ie this works:

     

    What do you mean by “on its own”? (That “display dialog” command doesn't seem to be needed when I run the script from the AppleScript Editor window. Does the script work if you replace “display dialog "1"” with “delay 1”?)

     

    The following script should do what you are asking for:

     

    tell application "Mail"

        set theSelection to selection

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

        set theSubject to subject of theForwardedMessage

        activate

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

            tell text field "Subject:" of window 1

                set focused to true

                set value to "{plain} " & theSubject

            end tell

            repeat with thisScrollArea in scroll areas of window 1

                if (text field "To:" of thisScrollArea) exists then

                    tell text field "To:" of thisScrollArea

                        set focused to true

                        set value to "jlbrown@bordo.com.au"

                    end tell

                    exit repeat

                end if

            end repeat

        end tell

    end tell

     

    Message was edited by: Pierre L.

  • 4. Re: AppleScript, Mail.app - Forward command doubles content - Mountain Lion
    James Brown15 Level 1 Level 1 (45 points)

    Thanks for your help Pierre.

     

    By 'on its own' I meant the your script without any changes.

     

    The same thing happens with the new script you wrote. It creates the new Forwarded email, but then stops. Ie the System Events stuff doesn't get executed.

     

    But if I add "dispaly dialog "A"' after the 'tell application "System Eventes" to tell process "Mail" line, it works.

     

    Putting in a 'delay' instead of a 'display dialog' verb didn't work.

     

    James.

  • 5. Re: AppleScript, Mail.app - Forward command doubles content - Mountain Lion
    MileHiGuy66 Level 1 Level 1 (0 points)

    Hi Pierre,

     

    I'm encountering the same issue as James did (i.e. when adding "display dialog '1'" the scripts works fine...but without it the script doesn't work).

     

    I am not an applescript guru but after performing various tests, I noticed everything works fine when the script is part of a script bundle (with extension .scptd). When the script is saved with extension .scpt and invoked by Mail Rules, James' issue occurs...which again is resolved by adding "display dialog '1'" to code.

     

    Why is this?

  • 6. Re: AppleScript, Mail.app - Forward command doubles content - Mountain Lion
    twtwtw Level 5 Level 5 (4,690 points)

    This is an old thread, I know, but I'm going to suggest that you not use the forward command at all.  Just make a new outgoing message and copy the relevant text you need into it.  easier than struggling with it.