-- Prompt the user for a custom search string set findString to text returned of (display dialog "Enter the text to search for (including message header):" with title "Full Message Search" default answer "") -- Prompt the user for a custom flag (1-7, -1 = remove the flag) set customFlag to text returned of (display dialog "Enter a flag (0-6):" with title "Full Message Search" default answer "6") tell application "Mail" -- Get all messages in the mailbox set theMessages to selection repeat with aMessage in theMessages -- Get the raw source of the message set messageSource to source of aMessage -- Check if the custom header is in the source (adjust the header name and value as needed) if messageSource contains findString then -- Set message flag set flagged status of aMessage to true -- display (true) or hide (false) flag set flag index of aMessage to customFlag -- set color for the flag (0-6 or -1 for none) end if end repeat end tell