Scripting Mail to iCal events, counting days

I receive an email notice from my Credit Card processor of the day’s transactions. I’m trying to script that into an iCal event 3 days later so I can easily see what’s coming in. Works OK on Mon and Tues, but I’m trying to get iCal to count days: if the mail comes on Wed, Thurs or Fri, set the event day to Mon, Tues, or Wed, not Sat/Sun.

I’m also trying to change the TIME of the new event to 09:00:00 without success.

Any insight from you brainiacs is much appreciated.

I’ve managed to work myself into this hole right here by patching together snippets from here and there:


tell application "Mail"
    set the_messages to selection
    repeat with i from 1 to count of the_messages
        set the_message to (a reference to item i of the_messages)
        set the_content to (content of the_message)
        set the_date_sent to (date sent of the_message)
        set DepositDate to the_date_sent - 12 * hours + 3 * days

        -- the day the mail is sent sometimes comes after midnight, so the above calculation insures that the day in question is actually the day of the deposit.

        tell application "iCal"
            set FollowUpEvent to (make new event at end of every event of calendar 3)
            
            tell FollowUpEvent
                if DepositDate contains "Saturday" then
                    set NewDepositDate to DepositDate + 2 * days
                    if DepositDate contains "Sunday" then
                        set NewDepositDate to DepositDate + 2 * days
                    end if
                else
                    set NewDepositDate to DepositDate
                end if
                --set time of NewDepositDate to "09:00:00 AM"
                set start date to NewDepositDate
                set end date to NewDepositDate + 5 * minutes
                set summary to "Today's Deposit"
                set description to the_content
            end tell
        end tell
    end repeat
end tell

Bonus Question: I’m also trying to pull the relevant data from the guts of the message, but can’t figure out how to script this part. Here’s the relevant data from the email that’s sent, less anything identifying. The mail comes in as plain text with the lines broken as shown. I only want the “Net Batch Total” line, or better yet, just the total, to use as the title of the iCal entry. :smiley:

[code] not really code:

Your Authorize.Net ID is: 000000000
Dear “User”,

The following is your Credit Card settlement report for Thursday, October
20, 2005.

Transaction Volume Statistics for Settlement Batch dated 20-Oct-2005
22:31:40:
Net Batch Total: 462.43
Number of Charge Transactions: 4
Amount of Charge Transactions: 462.43
Number of Refund Transactions: 0
Amount of Refund Transactions: 0.00

And then there’s more junk below…[/code]
Model: PB G4 667
AppleScript: 1.9.3
Browser: Safari 125.12
Operating System: Mac OS X (10.3.9)

Hi,

Is this the sort of thing?


-- (not tested)

tell application "Mail"
	set the_messages to selection
	repeat with i from 1 to count of the_messages
		set the_message to (item i of the_messages)
		set the_content to (content of the_message)
		repeat with myPara in paragraphs of the_content
			if myPara starts with "Net Batch Total: " then
				set MyTitle to (characters 18 thru -1 of myPara) as text
				exit repeat
			end if
		end repeat
		-- Loop through paragraphs of message text to find "Net Batch Total: " and grab number.
		
		set the_date_sent to (date sent of the_message)
		set DepositDate to the_date_sent - 12 * hours + 3 * days
		-- the day the mail is sent sometimes comes after midnight, so the above calculation insures that the day in question is actually the day of the deposit.
		
		if weekday of DepositDate is Saturday or weekday of DepositDate is Sunday then
			set DepositDate to DepositDate + 2 * days
		end if
		-- Move to Mon/Tues from Sat/Sun.
		
		set time of DepositDate to 0
		set hours of DepositDate to 9
		-- Set time to 9am
		
		tell application "iCal"
			set FollowUpEvent to (make new event at end of every event of calendar 3 with properties {start date:DepositDate, end date:(DepositDate + 5 * minutes), summary:MyTitle, description:the_content})
			-- Add the event with properties
		end tell
	end repeat
end tell

Best wishes

John M

Works like a champ! - muchas gracias! I had to make a few mods to counting business days, my logic was not quite right in counting business days from email days. Also this line:

   set hours of DepositDate to 9

gave me an error, but it was easily fixed by making the previous line a calculation

	set time of DepositDate to 0 + 9 * hours

And then I set that time to 7 so that it appears as the first entry of the day.

Very nice, thanks again, John. Here’s the final for posterity, heavily commented:


-- This script processes email notifications of pending deposits and posts them  
-- as iCal events for an "at-a-glance" view of those deposits and the
-- day they're supposed to occur

-- First we loop through paragraphs of message text to find the 
-- phrase " Net Batch Total: " (including a leading space) 
-- and grab the deposit total.

tell application "Mail"
	set the_messages to selection
	repeat with i from 1 to count of the_messages
		set the_message to (item i of the_messages)
		set the_content to (content of the_message)
		repeat with myPara in paragraphs of the_content
			if myPara starts with " Net Batch Total: " then
				set MyTitle to (characters 18 thru -1 of myPara) as text
				exit repeat
			end if
		end repeat
		
		-- deposits appear 2 business days after the mail is sent, appearing 
		-- on the 3rd business day - so calculate the date the message is sent
		-- and add 3 business days.
		
		-- the notification email is sometimes sent after midnight, so the "-12"
		-- calculation insures that the deposit day is the day of the deposit.
		
		set the_date_sent to (date sent of the_message)
		set DepositDate to the_date_sent - 12 * hours + 3 * days
		
		-- Notifications received on Wed/Thu/Fri to appear on 
		-- Mon/Tue/Wed of the following week, so we add days for that
		
		if weekday of DepositDate is Saturday or weekday of DepositDate is Sunday or weekday of DepositDate is Monday then
			set DepositDate to DepositDate + 2 * days
		end if
		
		-- Set time of the event to 7 am so that it appears first in the day's events
		set time of DepositDate to 0 + 7 * hours
		
		-- Finally we add the event, with properties
		tell application "iCal"
			set FollowUpEvent to (make new event at end of every event of calendar 3 with properties {start date:DepositDate, end date:(DepositDate + 5 * minutes), summary:MyTitle, description:the_content})
		end tell
	end repeat
end tell

Model: PB G4 667
AppleScript: 1.9.3
Browser: Safari 125.12
Operating System: Mac OS X (10.3.9)

I started using this to do other Mail to Text processing of submitted forms. Form results come in as Plain Text email. I’ve hit a snag with this little piece:


           if myPara starts with "SomeField: " then
               set MyTitle to (characters 12 thru -1 of myPara) as text
           end if

Works great if there’s something in SomeField, but if there’s no entry, the script vomits - I think it’s looking for at least one character between the space after "SomeField: " and the next carriage return. The error is:

	"Can't make characters 12 thru -1 of item 7 of every paragraph of \"

" into a string."

I’ve tried:


           if myPara starts with "SomeField: \\r" then
               set MyTitle to "" as text
           else
           if myPara starts with "SomeField: " then
               set MyTitle to (characters 12 thru -1 of myPara) as text
           end if


           if myPara starts with "SomeField: %return%" then
               set MyTitle to "" as text
           else
           if myPara starts with "SomeField: " then
               set MyTitle to (characters 12 thru -1 of myPara) as text
           end if


           if myPara starts with "SomeField: " & return then
               set MyTitle to "" as text
           else
           if myPara starts with "SomeField: " then
               set MyTitle to (characters 12 thru -1 of myPara) as text
           end if

In the above attempts, the script never finds the first IF, and attempts to process the second one.

Any assistance is of course much appreciated.

Model: PB G4 667
AppleScript: 1.9.3
Browser: Safari 125.12
Operating System: Mac OS X (10.3.7)

Hi,

Take a look at this example:

set the_content to "Some text.
SomeField:
Some more text."

-- Pre-set the variable.
set MyTitle to ""

-- Loop through the paragraphs of the_content.
repeat with myPara in paragraphs of the_content
	
	-- Check that the paragraph starts with the text & that it is long enough to have more text.
	if (myPara starts with "SomeField: ") and (length of myPara is greater than 11) then
		
		-- Set the variable myTitle to rest of the paragraph and jump out of the repeat loop.
		set MyTitle to (characters 12 thru -1 of myPara) as text
		exit repeat
	end if
end repeat

display dialog MyTitle

The solutions you tried with various forms of "SomeField: " & return would not work as the paragraphs of in the repeat loop is dividing the text by the return character (removing the return).

HTH

John M