One click forward marked messages with AppleScript

Hello everyone!

I have an AppleScript whose task is to forward flagged messages to another email (in the standard Apple Mail application), but it does not work as I need. I added this script to the Automator as a service.

If I want to use this script, I have to:

  1. flag the message.

  2. go to the services and select the “Forward marked messages” service that I need, and only then they are forwarded all at once.

My idea is automatically forward incoming message to the assistant immediately after marked the message with a flag.

Could someone correct this AppleScript?

Or am I mistaken in introducing it into services?

Help me please!

set toAddress to “alex@example.com
set toName to “Alex”

tell application “Mail”
repeat with _acct in imap accounts
–Look For Flagged Messages in the INBOX
set _acct_name to name of _acct
set _inbox to _acct’s mailbox “INBOX”
set _msgs_to_capture to (a reference to ¬
(every message of _inbox ¬
whose flagged status is true))

    repeat with _msg in _msgs_to_capture
        set _fwdmsg to forward _msg with opening window

        tell _fwdmsg
            make new to recipient at end of ¬
                to recipients with properties {name:toName, address:toAddress}
        end tell

        activate

        send _fwdmsg
    end repeat
end repeat

end tell

each message is already one reference, so when you tell a reference to message, you get error, since you try get a reference to reference. Try this:


set toAddress to "alex@example.com"
set toName to "Alex"

tell application "Mail"
	repeat with _acct in imap accounts
		--Look For Flagged Messages in the INBOX
		set _acct_name to name of _acct
		set _inbox to _acct's mailbox "INBOX"
		set _msgs_to_capture to (every message of _inbox whose flagged status is true)
		-- Send Flagged Messages to indicated recipient
		repeat with _msg in _msgs_to_capture
			set _fwdmsg to forward _msg with opening window
			tell _fwdmsg to make new to recipient at end of ¬
				to recipients with properties {name:toName, address:toAddress}
			activate
			send _fwdmsg
		end repeat
	end repeat
end tell

Thank you for reply!
How can I automate your script? I added it as a service and saved. Assigned hotkeys to the script. But the problem is that if I have 5 letters marked with a flag and if I want to mark and send 6 letters, then the previously highlighted messages will be sent too. Is there a solution for the letter to be sent automatically as soon as I flag it?

It is easy to add 2 code lines which removes flag immediately after sending flagged message. This will solve your problem:


set toAddress to "alex@example.com"
set toName to "Alex"

tell application "Mail"
	repeat with _acct in imap accounts
		--Look For Flagged Messages in the INBOX
		set theMessages to every message of every mailbox of every account
		set _acct_name to name of _acct
		set _inbox to _acct's mailbox "INBOX"
		set _msgs_to_capture to (every message of _inbox whose flagged status is true)
		-- Send Flagged Messages to indicated recipient
		repeat with _msg in _msgs_to_capture
			set _fwdmsg to forward _msg with opening window
			tell _fwdmsg to make new to recipient at end of ¬
				to recipients with properties {name:toName, address:toAddress}
			activate
			send _fwdmsg
			set _msgContents to contents of _msg
			set flagged status of _msgContents to false
		end repeat
	end repeat
end tell

Is it possible to send an email with one click on the flag?
I have this setting on Windows 10 in MS Outlook, but in MS Outlook for Mac this setting is not. Therefore, I would like to configure the same Apple Mail.
Thank you for answering and helping.

If this is possible, then you can do the same thing that you did in the last message, remove the flag after sending. One click on the flag, the message is sent, the flag is removed.

I’m not sure for sure, but it seems hard to do.

But you can do otherwise: create in Automator custom service (using the script bellow). You select a message (any), right-clicking on it choose your service and if the message is flagged, the service sends it. If the message is not flagged, then the service doesn’t send it.

The other solution is to make the script above stay-open application (using on idle handler, which will run every 0.1 seconds, check existing of flagged message in Mail.app and send it, if one exists). But why overload the computer’s operating system?

Script for Automator variant:

  1. Open the Automator.
  2. Choose create Quick Action
  3. In actions’s Library section choose Utilities. Opens list of available actions (on right side)
  4. Drag action Run AppleScript to empty window on right side
  5. In the textfield on top of window replace any application with Mail.app
  6. Replace the template Apple-script with the following
  7. Save File as service, giving it name.
  8. Use your new custom service (it will work only in Mail.app. Select the message or messages, right-click, run service)

set toAddress to "alex@example.com"
set toName to "Alex"

tell application "Mail"
	set theMessages to selection
	repeat with i from 1 to count of theMessages
		set aMessage to item i of theMessages
		if flagged status of aMessage then tell aMessage to ¬
			make new to recipient at end of to recipients ¬
				with properties {name:toName, address:toAddress}
		send aMessage
		set flagged status of aMessage to false
	end repeat
end tell

Thank you very much! I will try to do as you said!

Syntax error
Mail got an error: AppleEvent handler failed

Ops, It was 1 error: need forwarding to create outgoing message:


on run {input, parameters}
	set toAddress to "alex@example.com"
	set toName to "Alex"
	tell application "Mail"
		set theMessages to selection
		repeat with i from 1 to count of theMessages
			set aMessage to item i of theMessages
			if flagged status of aMessage then
				set outgoingMessage to forward aMessage
				tell outgoingMessage to ¬
					make new to recipient at end of to recipients ¬
						with properties {name:toName, address:toAddress}
				send outgoingMessage
				set flagged status of aMessage to false
			end if
		end repeat
	end tell
end run

I checked this last update (by sending to me)