Open Mail Attachments

Hi there

Have been trying to get Mail.app to automatically open attachments using Mail rules and an applescript.

I have found a script for automatically saving an attachment which works fine so I added a couple of lines at the end as per below:

using terms from application "Mail"
	on perform mail action with messages The_Messages
		tell application "Mail" to repeat with this_message in The_Messages
			set attachement_file to first mail attachment of this_message
			set attachment_name to name of the first mail attachment of this_message
			save attachement_file in "Users:andrewtetley:Desktop:Files:" & attachment_name
		end repeat
	end perform mail action with messages
end using terms from

delay 5
tell application "System Events" to open "Users:andrewtetley:Desktop:Files:" & attachment_name

The last two lines are my own work but they do not workl. The file saves fine (so the plagiarised code works) but the System Events bit does nothing (although it works if I have it in a stand alone script directly addressing the file in question)

Any suggestions?

Ideally I would just like a script to open the attachment without the two step process of saving then opening (Entourage has a rule action that does this, I believe, but Mail needs a script.

Cheers

My suggestion would be not to open e-mail attachments automatically.

Appreciate the security concerns but I still want to do it!

Entourage makes this very easy. Should be easy with Mail as well but not proving to be the case so far.

The short script I tried (to avoid the two stage process) was as follows:

using terms from application "Mail"
	on perform mail action with messages The_Messages
		tell application "Mail" to repeat with this_message in The_Messages
			set attachement_file to first mail attachment of this_message
			tell application "Finder" to open file (attachment_file as text) using application file id selectedApp
		end repeat
	end perform mail action with messages
end using terms from

It didn’t work either!

Any other suggestions?

Side note: When posting, select your script and press the AppleScript button once to get the proper effect. (That is, your script should be inside the two applescript tags.)

Thanks. I’ll try to format it correctly next time.

Following further experimenting, it seems that all the code past the end of “end using terms from” is simply ignored. I tried putting in a Display Dialog after them and it never appeared.

I am now completely baffled and am giving up hoping that someone more knowledgeable will point me in the right direction.

Thanks

I hope other users don’t learn from this as an example of safe practice, because it is most certainly not. When the first real threat hits, you’re going to help spread it. Thanks.

Thanks, Jacques. Will need to look at incorporating this tomorrow (bit late here)

Regarding Mikey-San comment, the Mail rule only will only open the attachment when the email comes from my email address with a particular subject wording and where the attachment is of a particular type (all prescribable under Mail). If this is so dangerous, why does Entourage make it child’s play to do this (rhetorical)?

Tout est pour le mieux dans le meilleur des mondes possibles.

You assume that it will, because you make the assumption that there is nothing wrong with your code. What if there’s a bug in your script? Now you have another point of insertion, so to speak. Automatically opening e-mail attachments is a stupid thing to do, no matter how good you think your coding ability is.

Entourage does this because Microsoft doesn’t know a damned thing about security. Have you seen Windows and Outlook in the last decade?

You do realize that Voltaire was being ironic, right?

Jacques

Many thanks. I was able to use your script to make this:

property dest_folder : (path to desktop as Unicode text) & “Pando Files:”
using terms from application “Mail”
on perform mail action with messages The_Messages
tell application “Mail” to repeat with this_message in The_Messages
repeat with This_Attachment in (get mail attachments of this_message)
set This_file to dest_folder & name of This_Attachment
save This_Attachment in This_file – the Attachment is saved
tell application “Finder” to open This_file – open file in default application
tell application “Finder” to move This_file to trash – then bin it
end repeat
end repeat
end perform mail action with messages
end using terms from

My derivative of your code is smaller because it bins the attachment each time as the attachment is only ever a link to something else. No chance of two attachments having the same name and therefore I could dispense with the routines for that which you included. If I ever need to x-ref any particular attachment, it will still be attached to the relevant email.

Great and helpful post.

I think you have helped me out on more than one occasion now and it is much appreciated (by an infrequent applescripter).

Cheers

L’ironie est un art qui ne s’exprime pas de manière ouverte mais qui se revèle mieux en douceur

Whoops! And after a reminder in this thread how to do it properly…

Jacques

Many thanks. I was able to use your script to make this:

property dest_folder : (path to desktop as Unicode text) & "Pando Files:"
using terms from application "Mail"
    on perform mail action with messages The_Messages
        tell application "Mail" to repeat with this_message in The_Messages
            repeat with This_Attachment in (get mail attachments of this_message)
                set This_file to dest_folder & name of This_Attachment
                save This_Attachment in This_file -- the Attachment is saved
                tell application "Finder" to open This_file -- open file in default application
                tell application "Finder" to move This_file to trash -- then bin it
            end repeat
        end repeat
    end perform mail action with messages
end using terms from

My derivative of your code is smaller because it bins the attachment each time as the attachment is only ever a link to something else. No chance of two attachments having the same name and therefore I could dispense with the routines for that which you included. If I ever need to x-ref any particular attachment, it will still be attached to the relevant email.

Great and helpful post.

I think you have helped me out on more than one occasion now and it is much appreciated (by an infrequent applescripter).

Cheers

L’ironie est un art qui ne s’exprime pas de manière ouverte mais qui se revèle mieux en douceur