Apple scripts

Hello, 


Need a helping hand to write a scripts to :-


Extract certain line of data from multiple emails.


Email receives in standard format as below



Dear (Name),


Please be advised that the voucher has been approved by SF01A-(Section Name) for the following invoice and will be submitted to Treasury for final approval.


Lot ID. Invoice. Voucher No. Voucher Date. Voucher Amount. Business Unit-Name

0000337553. UTCP3001771. 00821861. 13/03/2023. $1xxx.xx. SF01A-(Section Name) 👈🏼


Thank you,


Admin


Treasury Department,

(Section Name)


*** This is an automatically generated email, please do not reply ***


This message has been analyzed by Deep Discovery Email Inspector.

===============================================================







Any one can help to write an apple scripts that would allow me to extract only the info (with emoji 👈🏼) from Lot ID to Business Unit-Name to a Numbers (spreadsheet) ??

Mac mini, macOS 10.13

Posted on Mar 14, 2023 6:36 PM

Reply
2 replies

Mar 20, 2023 12:20 PM in response to Rlun64

There's lots to unpack here.


Ostensibly, the ask isn't hard - extract some data from the email message and insert it into a Numbers sheet.


However, as is common with these kinds of requests, there are many gotchas.


Starting with the source - since this looks like a standard email form, it should be consistent, so that helps, but there is the question of how to identify the content in question.

My initial instinct would be to look for the first line that begins 'Lot ID', and assume the next line contains the data. Assumptions can always fail, though.

Alternatively, you could assume it's always the 6th line in the email, but email formats may change.


Ultimately, there needs to be some agreement/compromise on the method, so that you know what to change when things fail :)


Secondly, getting the data into Numbers.

The first issue is identifying where to put the data. Is the spreadsheet you want already open? Are you looking at the right sheet/table? (Note: you don't have to have the table visible at all in order to add data to it, but the script needs to know where to put the data.

Also, it's easy to append the data to the end of a table, but is there a footer row that needs to be taken account of?


Like I said, lots of gotchas.


Here's a skeleton script that should get you going. Applied as a Mail rule (so it sees the messages as they arrive in your inbox), it grabs the data and appends it to the bottom of the first table in the frontmost Numbers document.

This may need work if the columns in the table don't match the data (e.g. there are other columns, or they're out of order), and there is little error checking (to make sure the data exists, that Numbers is running with the spreadsheet open, etc.), but this can be added if the base functionality is valid (and based on the answers to the above questions).


The script should be saved in ~/Library/Application Scripts/com.apple.mail and use Mail preferences to run this script when the incoming triggers fire (presumably there's some identifying mark such as the sender's address, etc.)


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

-- standard preamble for invoking AppleScripts via mail rules
using terms from application "Mail"
	on perform mail action with messages these_messages for rule this_rule
		-- iterate through the new messages
		repeat with each_message in these_messages
			-- get the message body
			set theInputText to content of each_message
			-- and break it into paragraphs
			set inputData to paragraphs of theInputText
			-- count how many paragraphs there are
			set num_paras to count of inputData
			-- iterate through them
			repeat with lineNum from 1 to num_params
				-- find the header line
				if word 1 of item lineNum of inputData = "Lot" then
					-- found it, so assume the next line has the data we want
					set dataLine to item (1 + lineNum) of inputData
					-- and get out of the loop
					exit repeat
				end if
				-- and add the data to Numbers
			end repeat
			my addRow(dataLine)
		end repeat
		
	end perform mail action with messages
end using terms from


on addRow(dataLine)
	-- assume the data is tab-delimited, so break it into fields
	set {oldTID, my text item delimiters} to {my text item delimiters, tab}
	set cellValues to text items of (dataLine as text)
	set my text item delimiters to oldTID
	tell application "Numbers"
		-- find the first table on the active document
		set theTable to table 1 of active sheet of front document
		-- add a new row
		set newRow to add row below last row of theTable
		-- iterate through the input data
		repeat with colNum from 1 to count cellValues
			-- and set the value of the cells
			set value of (cell colNum of row (address of newRow) of theTable) to item colNum of cellValues
		end repeat
	end tell
end addRow


Mar 20, 2023 12:59 PM in response to Camelot

I didn't have the full solution so I withheld the post, but here is how I identifed the Voucher string in the email body. The my VoucherID(apStr) handler splits the Voucher data into list elements, and concatentates the last field whose whitespace causes extra elements that need to be recombined.


(*0000337553., UTCP3001771., 00821861., 13/03/2023., $1xxx.xx., SF01A-(Section Name) 👈🏼*)


	tell theMsg to set theContent to its content
end tell
-- splits the content by linefeeds into a list
set paraList to (ca's NSString's stringWithString:theContent)'s componentsSeparatedByString:linefeed

repeat with ap in paraList
	try
		-- extract just the Lot ID. thru Business Unit name data line.
		-- assumption: there is never more than one line begining with a number in the email body
		if class of ((first word of (ap as text)) + 1) is integer then set apStr to (ap as text)
	end try
end repeat
log (my voucherID(apStr)) as list
return


😎



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.

Apple scripts

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