Extracting numbers from an email using automator

Hi there,


I'm new to automator and am creating a workflow to "scan" an email (in the mac mail client) for a dollar value and then copy that number to the clip board.


Pretty simple stuff, I know.


Can anyone help me achieve this?


So far I have

>Get New Mail

>Find Mail Messages (which gets the email i want to scan)


Now I need an apple script that grabs the number value from in the email and copies it to the clip board.


Help! Please and thank you!

MacBook Pro 13″, macOS 10.15

Posted on Feb 4, 2023 8:02 PM

Reply
Question marked as Top-ranking reply

Posted on Feb 7, 2023 2:18 AM

You did not respond to my question about the format of that 34 character string in an email body. I have revised the original script that I posted by adding a regular expression pattern for that 34-character string, and made the AppleScript handler generic so that passing the email body text variable and the regular expression pattern will return the captured content (if found).


AppleScript


(*
  Search a selected email body for a financial value, and a 34-character
  string. Captured content is returned to their resepective variables.
  Coded to handle $,£, and € currencies. Text string is case-insensitive
  alphanumeric and may contain known punctuation characters. It should
  be surrounded by white space.
 
  Includes Unicode ICU Regular Expressions
  https://unicode-org.githubhttps://unicode-org.github.io/icu/userguide/strings/regexp.html

  Tested: Ventura 13.2, Apple Mail 16.0
  VikingOSX, 2023-02-06, Apple Support Communities, No warranty. 
*)

use framework "Foundation"
use AppleScript version "2.4" -- Yosemite or later
use scripting additions

property ca : current application

-- ICU Regular Expression to match numbers up to 999p999p999p99
-- where p is the currency punctuation of either comma or dot
property ICU_Monetary : "([$£€]?\\d{1,3}(?:[.,]?\\d{3})*(?:[.,]?\\d{2}))"

-- get 34 contiguous case-insensitive characters surrounded by white-space
-- (e.g. Abu_dec-12xy_4A23BmDEFgnxyffz-ce5n )
-- uses positive lookbehind and lookahead for surrounding word boundaries
property ICU_Text34 : "(?<=\\b)([[:alnum:][:punct:]]{34})(?=\\b)"

tell application "Mail"
	if not it is running then activate
	
	if not (get selection) is {} then
		set theMsg to item 1 of (get selection)
	else
		return
	end if
	
	tell theMsg
		set theBody to its content
	end tell
end tell

set money to my string_extraction(theBody, ICU_Monetary)
set text34 to my string_extraction(theBody, ICU_Text34)
log (money) as text
log (text34) as text

-- note only one item at a time may exist on the clipboard
if not money is equal to "Not Found" then
	ignoring numeric strings
		set the clipboard to money
	end ignoring
end if
return

on string_extraction(atxt, regex)
	set hstr to ca's NSString's alloc()'s initWithString:atxt
	set regex to ca's NSRegularExpression's regularExpressionWithPattern:regex options:0 |error|:0
	set hrange to ca's NSMakeRange(0, hstr's |length|())
	set matches to (regex's firstMatchInString:hstr options:0 range:hrange)
	
	if matches = "" or matches = missing value then return "Not Found"
	
	set matchrange to matches's rangeAtIndex:1
	return (hstr's substringWithRange:matchrange) as text
end string_extraction


7 replies
Question marked as Top-ranking reply

Feb 7, 2023 2:18 AM in response to VikingOSX

You did not respond to my question about the format of that 34 character string in an email body. I have revised the original script that I posted by adding a regular expression pattern for that 34-character string, and made the AppleScript handler generic so that passing the email body text variable and the regular expression pattern will return the captured content (if found).


AppleScript


(*
  Search a selected email body for a financial value, and a 34-character
  string. Captured content is returned to their resepective variables.
  Coded to handle $,£, and € currencies. Text string is case-insensitive
  alphanumeric and may contain known punctuation characters. It should
  be surrounded by white space.
 
  Includes Unicode ICU Regular Expressions
  https://unicode-org.githubhttps://unicode-org.github.io/icu/userguide/strings/regexp.html

  Tested: Ventura 13.2, Apple Mail 16.0
  VikingOSX, 2023-02-06, Apple Support Communities, No warranty. 
*)

use framework "Foundation"
use AppleScript version "2.4" -- Yosemite or later
use scripting additions

property ca : current application

-- ICU Regular Expression to match numbers up to 999p999p999p99
-- where p is the currency punctuation of either comma or dot
property ICU_Monetary : "([$£€]?\\d{1,3}(?:[.,]?\\d{3})*(?:[.,]?\\d{2}))"

-- get 34 contiguous case-insensitive characters surrounded by white-space
-- (e.g. Abu_dec-12xy_4A23BmDEFgnxyffz-ce5n )
-- uses positive lookbehind and lookahead for surrounding word boundaries
property ICU_Text34 : "(?<=\\b)([[:alnum:][:punct:]]{34})(?=\\b)"

tell application "Mail"
	if not it is running then activate
	
	if not (get selection) is {} then
		set theMsg to item 1 of (get selection)
	else
		return
	end if
	
	tell theMsg
		set theBody to its content
	end tell
end tell

set money to my string_extraction(theBody, ICU_Monetary)
set text34 to my string_extraction(theBody, ICU_Text34)
log (money) as text
log (text34) as text

-- note only one item at a time may exist on the clipboard
if not money is equal to "Not Found" then
	ignoring numeric strings
		set the clipboard to money
	end ignoring
end if
return

on string_extraction(atxt, regex)
	set hstr to ca's NSString's alloc()'s initWithString:atxt
	set regex to ca's NSRegularExpression's regularExpressionWithPattern:regex options:0 |error|:0
	set hrange to ca's NSMakeRange(0, hstr's |length|())
	set matches to (regex's firstMatchInString:hstr options:0 range:hrange)
	
	if matches = "" or matches = missing value then return "Not Found"
	
	set matchrange to matches's rangeAtIndex:1
	return (hstr's substringWithRange:matchrange) as text
end string_extraction


Feb 5, 2023 7:29 AM in response to SamKlass

I have an AppleScript that will extract that financial such as €999.999.999,99 from the body of the email. It handles dollar, euro, and pound currencies with dot or comma numeric punctuation. The code only handles a single occurrence, and if your email body has multiple financial values, this is not the correct script for that alternative.


The script assumes that a single Mail message is selected beforehand in Apple Mail.


AppleScript


use framework "Foundation"
use AppleScript version "2.4" -- Yosemite or later
use scripting additions

property ca : current application
-- ICU Regular Expression to match numbers up to 999p999p999p99
-- where p is the currency punctuation of either comma or dot
property ICU_PAT : "([$£€]?\\d{1,3}(?:[.,]?\\d{3})*(?:[.,]?\\d{2}))"

tell application "Mail"
	if not it is running then activate
	
	if not (get selection) is {} then
		set theMsg to item 1 of (get selection)
	else
		return
	end if
	
	tell theMsg
		set theBody to its content
	end tell
end tell

set money to my financial(theBody)
display dialog money
if not money is equal to "Not Found" then
	ignoring numeric strings
		set the clipboard to money
	end ignoring
end if
return

on financial(atxt)
	set hstr to ca's NSString's alloc()'s initWithString:atxt
	set regex to ca's NSRegularExpression's regularExpressionWithPattern:ICU_PAT options:0 |error|:0
	set hrange to ca's NSMakeRange(0, hstr's |length|())
	set matches to (regex's firstMatchInString:hstr options:0 range:hrange)
	
	if matches = "" or matches = missing value then return "Not Found"
	
	set matchrange to matches's rangeAtIndex:1
	return (hstr's substringWithRange:matchrange) as text
end financial



Feb 5, 2023 2:54 PM in response to VikingOSX

Wow thank you so much. That will help me with exactly what I want to do.


Right now in automator I have

> Get New Messages

> Find Mail Messages (Messages that have interac in the subject)

> [The script you posted]

> Copy to clipboard


Your script is working beautifully but ends with a prompt for the amount, but I need it to copy to the clipboard. I can't seem to get automator to do that with the "copy to clipboard" function.


Is there a way I can get that value copied? Thank you again so much for your time, you've been a huge help already.

Feb 5, 2023 6:01 PM in response to VikingOSX

I very much appreciate it, I got it to work perfectly. Very grateful for your assistance. There's one other thing I'm trying to do, and that is, the exact same script, except in place of the of a dollar value that the previous script was look for, this one just looks for a 34 character string in the e-mail and copies it to clipboard. If you know an easy way to do this, please let me know.


Thanks again for your time.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Extracting numbers from an email using automator

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.