mail.app rule and ical

So i wrote a simple script that parse’s an e-mail’s contents into specific variables and then creates an ical event using said variables. as a plain script, with a message selected in mail.app it works flawlessly. however, when i set it to be triggered by a rule, nothing happens. first, here is the content of the email (right now, it has to be in this format):

the subject line has to correspond with an existin iCal calendar.
the body is:

OI, Financial
Mike Johnson x32338
12/27/07 1:00 PM
12/27/07 3:00 PM
50 people
This is a test event with information for an iCal Entry. Attempt for Mail Rule #2

when a message is sent with this content, the following script works when run from AS


tell application "Mail"
	set allMessages to selection
	
	-- cycle through all new messages
	repeat with i from 1 to count of allMessages
		set theMessage to (item i of allMessages)
		set theContent to (content of theMessage) as text
		
		set AppleScript's text item delimiters to ASCII character 10
		
		-- parse details from e-mail for iCal
		set theCalendar to subject of theMessage
		set theGroup to text item 1 of theContent
		set theContact to text item 2 of theContent
		
		set theStart to text item 3 of theContent
		
		set theEnd to text item 4 of theContent
		
		
		set theAttendees to text item 5 of theContent
		set theNotes to text item 6 of theContent
	end repeat
end tell

set startDate to date theStart
set endDate to date theEnd


--enter iCal event
tell application "iCal"
	tell calendar theCalendar
		set newEvent to (make new event at end of events with properties {summary:theGroup, location:theContact, start date:startDate, end date:endDate, description:theNotes})
	end tell
	tell newEvent
		make new attendee at beginning of attendees with properties {display name:theAttendees, participation status:unknown}
	end tell
end tell

however, when i set a rule to trigger this, nothing happens. if i select the message and choose “apply rules” it works.

to further the issue, i adapted this script to what i thought was a “rule-friendly” format, as follows:


using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		repeat with eachMessage in theMessages
			
			tell application "Mail"
				tell eachMessage
					set theMessage to (item i of allMessages)
					set theContent to (content of theMessage) as text
					set AppleScript's text item delimiters to ASCII character 10
					
					-- parse details from e-mail for iCal
					set theCalendar to subject of theMessage
					set theGroup to text item 1 of theContent
					set theContact to text item 2 of theContent
					set theStart to text item 3 of theContent
					set theEnd to text item 4 of theContent
					set theAttendees to text item 5 of theContent
					set theNotes to text item 6 of theContent
				end tell
				
				set startDate to date theStart
				set endDate to date theEnd

				--enter iCal event
				tell application "iCal"
					tell calendar theCalendar
						set newEvent to (make new event at end of events with properties {summary:theGroup, location:theContact, start date:startDate, end date:endDate, description:theNotes})
					end tell
					tell newEvent
						make new attendee at beginning of attendees with properties {display name:theAttendees}
					end tell
				end tell
			end tell
		end repeat
	end perform mail action with messages
end using terms from

This does not work even when the rule should have been triggered (scanning “from” address or subject content) nor by choosing “apply rules.”

Any help would be greatly appreciated.

Mike

Model: Mac Pro, Octo
AppleScript: 1.10.7
Browser: Firefox 2.0.0.11
Operating System: Mac OS X (10.4)

I haven’t tried your scripts, but I’ll bet it’s a security issue – Mail is prevented from running your script without direct interaction from you as a security precaution to avoid running a script containing something hideous like do shell script “rm /Users/”

Hi,

the rule cannot work, because allMessages isn’t defined
I haven’t tested it, but try this


using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		repeat with eachMessage in theMessages
			tell application "Mail"
				tell eachMessage
					set theContent to its content
					set theCalendar to its subject
					-- parse details from e-mail for iCal
					set {theGroup, theContact, theStart, theEnd, theAttendees, theNotes} to paragraphs 1 thru 6 of theContent
				end tell
				
				set startDate to date theStart
				set endDate to date theEnd
			end tell
			--enter iCal event
			tell application "iCal"
				tell calendar theCalendar
					set newEvent to (make new event at end of events with properties {summary:theGroup, location:theContact, start date:startDate, end date:endDate, description:theNotes})
				end tell
				tell newEvent
					make new attendee at beginning of attendees with properties {display name:theAttendees}
				end tell
			end tell
		end repeat
	end perform mail action with messages
end using terms from


because you don’t get any error message in a rule script you can test those scripts in this way
by commenting out the first and last two lines and adding the tell application “Mail” line,
then select a few mails and run the script

-- using terms from application "Mail"
--	on perform mail action with messages theMessages for rule theRule
tell application "Mail" to set theMessages to selection
.

.
--	end perform mail action with messages
-- end using terms from

Adam, I’m not sure I understand what you mean. Why is there an option to run a script from a rule if I need to be there to “approve it”?

Stefan,
In my rules script (the second one) the allMessages variable came straight from examples of mail.app rules. I get no errors stating that the variable is not set. If I change it to theMessages then I’m told that variable i is not set.

Again, the standalone script works when set to the rule, but I have to manually tell Mail.app to run the rule; it will not run automatically. The second one with the “using terms from” doesn’t do a thing either way.

mike

this works on my machine, also called by a rule.
The problem was, coercing a string to a date doesn’t work within a Mail tell block


using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		repeat with eachMessage in theMessages
			tell application "Mail"
				tell eachMessage
					set theContent to its content
					set theCalendar to its subject
					-- parse details from e-mail for iCal
					set {theGroup, theContact, theStart, theEnd, theAttendees, theNotes} to paragraphs 1 thru 6 of theContent
				end tell
			end tell
			
			set startDate to date theStart
			set endDate to date theEnd
			--enter iCal event
			tell application "iCal"
				tell calendar theCalendar
					set newEvent to (make new event at end of events with properties {summary:theGroup, location:theContact, start date:startDate, end date:endDate, description:theNotes})
				end tell
				tell newEvent
					make new attendee at beginning of attendees with properties {display name:theAttendees}
				end tell
			end tell
		end repeat
	end perform mail action with messages
end using terms from

Excellent! That did the trick. Thank you so much.

Thanks to all who got this working. I’ve got a slight variation, that has some strange behaviour. I have a website where a user fills in a form to generate an email to me with only their email address being submitted. They form then sets the Reply-to of the email, to the entered email address. A new email looks like this:

[code] From: services@squarespace.com
Subject: Form Submission - subscription
Date: August 18, 2009 4:15:35 PM MDT (CA)
To: XXXX
Reply-To: test@email.com

This email was transmitted via www.XXXXX.com.

Submitted from address XXX


Subscribe to our mailing list here:: test@email.com


Powered by Squarespace (http://www.squarespace.com/).[/code]
I’d then like to parse that into my AddressBook. I’m using the following script.


using terms from application "Mail"
    on perform mail action with messages theMessages for rule theRule
        repeat with eachMessage in theMessages
            tell application "Mail"
                tell eachMessage
                    set theEmail to its reply to
                end tell
            end tell
            
            --enter AddressBook Entry
            tell application "Address Book"
                set thePerson to make new person with properties {first name:"Mailing List"}
                tell thePerson
                    make new email at end of emails with properties {label:"Work", value:theEmail}
                end tell
                add thePerson to group "Mailing List"
                save addressbook
            end tell
            
        end repeat
    end perform mail action with messages
end using terms from

I have a rule in mail that detects the email based on the subject, then files it into a folder, and runs the AppleScript. Works almost perfectly. The flaw, is that the email address passed onto Address Book is “Squarespace Services services@squarespace.com” and not “test@email.com.”

Here’s the interesting part. If I manually move the email back to my inbox, and then Apply Rules, it operates correctly. I get a new entry with “test@email.com” as the email field, not “Squarespace Services…”

Can anyone shed some insight?

Thanks.