Opening a mime-attachment.ics in Mail

I want to write a simple script that is called by a Apple Mail Rule that delects the current email has an attachment of .ics type.

Here is what I have so far:

property theAttachmentPath : (path to "desk") as string

on run
	tell application "Mail"
		set theSelection to selection
		activate -- make sure Mail is frontmost app after running the script...
	end tell
end run

on perform_mail_action(info)
	tell application "Mail"
		set selectedMessages to |SelectedMessages| of info
		
		- need code here to open mime-attachment.ics attachment
		
	end tell
end perform_mail_action

Need help with the line that would tell Mail to open the attachment named “mime-attachment.ics”

Thanks for helping a Newbie

Moving to OS X.

Opps, sorry about that.

Hi Mike,

unfortunately you can’t open attachments directly in Mail.app with AppleScript (AFAIK you can’t)
so you have to save it and open it from this location.
You can attach this script to a defined rule in Mail.app

property theAttachmentPath : (path to "desk") as string

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		repeat with This_Message in theMessages
			tell application "Mail"
				repeat with ma in mail attachments of This_Message
					set n to name of ma
					if n ends with ".ics" then
						save ma in (theAttachmentPath & n)
						tell application "Finder" to open (theAttachmentPath & n) -- or application "iCal"
					end if
				end repeat
			end tell
		end repeat
	end perform mail action with messages
end using terms from

Thanks for helping out.

I’m new to scripting and looking at the suggested script I’m stumped by the line:

Where is the variable ma set? Is it set by the line:

yes, ma is the internal index variable of the repeat loop,
which represents an item of the list in each pass

Okay, the reason I ask is I have created the suggested script above and attached it to a Mail Rule that checks for attachments that contain “.ics” and I have selected messages that contain .ics attachments and applied the rule and nothing happens.

Any ideas on how to debugg this?

with a little modification this script works on my machine:

property theAttachmentPath : (path to desktop) as Unicode text

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			repeat with This_Message in theMessages
				repeat with ma in mail attachments of This_Message
					set n to name of ma
					if n ends with ".ics" then
						save ma in (theAttachmentPath & n)
						tell application "Finder" to open (theAttachmentPath & n) -- or application "iCal"
					end if
				end repeat
			end repeat
		end tell
	end perform mail action with messages
end using terms from

:slight_smile: Yeah, it works. Thanks

I’d like to add one more line that deletes the saved file named n.

I have added the following line 2 lines:

after the line:

But it doesn’t delete the file. Whats wrong?

Hi Mike,

this should work

tell application "Finder" to delete alias (theAttachmentPath & n)

This script worked great. Thank to every one involved. :slight_smile: Can’t wait for Snow Leopard though, it will be sweet that it syncs with exchange right out of the box. But this will do for now. Thank you again.