Copying Mail Contents

Hi everyone,

I am trying to copy the contents of a bill that comes in, use Regex to copy the specific cost, and then insert that into a variable. So far I am having trouble finding a way to get the content of a Mac mail message. I tried AppleScript but it's a little over my head. There is probably a slow and clunky way to do it with hotkey commands but wondering if anyone knows a way to copy the contents directly?

Thanks,
Jordan

This AppleScript should do the trick:

tell application "Mail"
  set {oMsg} to the selection
  
  tell oMsg
    set bodyStr to its content
  end tell
  
end tell

return bodyStr

Questions?

1 Like

Awesome! Works great. I even played around a little to copy it to clipboard (a big feat, I know).

The only thing is I would like to direct the bill message to a specific mailbox and have the script run when the message hits the mailbox. I know there are rules that can trigger an AppleScript, but is there a way to set the message to be copied if it is not directly selected? Tried a few things but no such luck so far.

Thank you so much for the help.

Please create a new topic for this request.
You can easily do that by clicking on the link icon/button at the bottom of your reply.
This will create a new topic with a lnk back to this topic.

Thanks.

Hey Jordan,

Unfortunately Apple broke AppleScripts in Mail Rules about 10 years ago, and they've never fixed it. (Bad Apple!)

Please take the time to complain:

Feedback - Mail - Apple

Manually processing messages in a given mailbox with AppleScript is relatively simple.

-Chris

Chris,

This is terrible news! Bad, bad Apple...

On the upside, after this I learned quite a bit of AppleScript and it's been really gratifying. This particular project didn't end up exactly as I wanted to but of course AppleScript has been useful in other ways.

Jordan

Yep.

Here's a possible workaround.

You used to be able to have a rule that worked on a per message basis, but that has been soundly broken for many years.

However – you can run an AppleScript in a rule – so you can filter all your desired mail to a process-me mailbox and then use a follow-on rule to tell Keyboard Maestro to run a macro to process those emails.

I was able to run this in a rule with no problem:

display notification "APPLESCRIPT MAIL RULE RAN!" with title "EUREKA!" sound name "Tink"

AppleScripts that run in Mail Rules must be in this folder:

~/Library/Application Scripts/com.apple.mail/

-Chris

Many thanks! Works well

1 Like