Friday, August 14, 2020

#1 2019-12-12 04:30:37 am

t.muhammedov
Member
Registered: 2019-12-11
Posts: 6

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

Offline

 

#2 2019-12-12 09:17:15 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1265

Re: One click forward marked messages with AppleScript

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:

Applescript:


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

Last edited by KniazidisR (2019-12-12 09:36:51 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#3 2019-12-12 11:05:41 pm

t.muhammedov
Member
Registered: 2019-12-11
Posts: 6

Re: One click forward marked messages with AppleScript

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?


Filed under: applescript

Offline

 

#4 2019-12-12 11:32:59 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 1265

Re: One click forward marked messages with AppleScript

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

Applescript:


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

Last edited by KniazidisR (2019-12-12 11:33:31 pm)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#5 2019-12-13 12:57:31 am

t.muhammedov
Member
Registered: 2019-12-11
Posts: 6

Re: One click forward marked messages with AppleScript

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.

Offline

 

#6 2019-12-13 01:01:30 am

t.muhammedov
Member
Registered: 2019-12-11
Posts: 6

Re: One click forward marked messages with AppleScript

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.

Offline

 

#7 2019-12-13 01:13:19 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1265

Re: One click forward marked messages with AppleScript

t.muhammedov wrote:

Is it possible to send an email with one click on the flag?


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)

Applescript:


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

Last edited by KniazidisR (2019-12-13 03:10:31 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#8 2019-12-13 02:25:31 am

t.muhammedov
Member
Registered: 2019-12-11
Posts: 6

Re: One click forward marked messages with AppleScript

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

Offline

 

#9 2019-12-13 02:34:42 am

t.muhammedov
Member
Registered: 2019-12-11
Posts: 6

Re: One click forward marked messages with AppleScript

KniazidisR wrote:
t.muhammedov wrote:

Is it possible to send an email with one click on the flag?


                make new to recipient at end of to recipients ¬
                    with properties {name:toName, address:toAddress}
end run[/applescript]




Syntax error
Mail got an error: AppleEvent handler failed

Offline

 

#10 2019-12-13 03:01:08 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1265

Re: One click forward marked messages with AppleScript

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

Applescript:


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)

Last edited by KniazidisR (2019-12-13 03:32:52 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)