How to flag messages that have a particular word in the body of the message?

How can I flag all messages that contain the word “Override” as follows:

set myReply to "Override"

set theSender to "Me <me@gmail.com>"

tell application "Mail"
    set theSelection to selection
    if theSelection is {} then return
    activate
    set myList to {}
    repeat with thisMessage in theSelection
        if (extract address from sender of thisMessage) is "watson@gmail.com" then
            set theOutgoingMessage to reply thisMessage with properties {visible:true, sender:theSender}
            delay 1
            tell application "System Events"
                keystroke myReply
                delay 1
            end tell
            send theOutgoingMessage
            delay 1
            set myList to myList
            delay 1
        end if
    end repeat
end tell

As you can see I only reply to those messages. How can I flag them too within this applescript?

It isn’t quite clear what you’re asking for.

You say you want to ‘flag messages’ that have a particular keyword.

Are you talking about scanning your existing mailboxes to find said messages?
Are you looking to trigger incoming messages via a Mail Rule?
The existing script focussed on selected messages - is that how you want to identify messages to check? How are you invoking this script?

Hi. I think this is what you want, but the question is open to interpretation, and selections are best relegated to testing.

tell application "Mail" to repeat with thisMessage in (get selection)
tell thisMessage to if (extract address from sender) is "watson@gmail.com" then
	if content contains "Override" then set flag index to 1
end if

end repeat

2 Likes

This is what I meant with flagging emails. Marc_Anthony’s answer worked! Thank you so much.

Screenshot 2023-04-18 at 18.13.39

I wasn’t questioning the flagging part - that’s easy. It was more how you wanted to invoke the script.

Generally, ‘selection’ is always the best way to do it, hence my question regarding Rules (to flag the messages as they come in).

In either case, this can be done via the standard Mail rules, without needing AppleScript at all: