Save Attachment has Generic Icon

This script was put together from parts of other scripts on the form. I wanted a script that would save my attachments to a specific folder with the Senders name and Subject. The script works fine on LION OSX but it saves attachments with a generic Icon. If I save the attachments manually the icons come over correctly. I notice this on an attachment that is “JPG” or “Jpeg”. Do I have something missing in my script.
Thanks, PolishPrince


(*
This Script saves the attachments of a selected email with an attachment. The name of the attachment is changed to the Sender and Subject of the Email.
*)

set destinationFolder to ((path to current user folder as text) & "Documents:Mail_Attachments:")

tell application "Mail"
	set themsg to selection
	set themsg to item 1 of themsg
	set Subject_ to subject of item 1 of themsg
	set Sender_ to sender of item 1 of themsg
	set Sender_ to extract name from Sender_
	repeat with themsg in themsg -- loop through the messages sent by Mail
		tell contents of themsg
			if (count (mail attachments)) > 0 then
				set theAttachment to first item of mail attachments -- this will crash if there are no attachments in the message
				set theAttachmentFileName to Sender_ & " - " & Subject_ -- Replace theAttachment's name with Sender and Subject of Email
				save theAttachment in (destinationFolder) & theAttachmentFileName
			end if
		end tell
	end repeat
end tell

Every time I try to run your script it fails with error number -10000


tell current application
	path to home folder as text
		--> "Macintosh HD:Users:xxxx:"
end tell
tell application "Mail"
	get selection
		--> {message id 223873 of mailbox "INBOX" of account "iCloud"}
	get subject of item 1 of message id 223873 of mailbox "INBOX" of account "iCloud"
		--> "Pages to Doc Script"
	get sender of item 1 of message id 223873 of mailbox "INBOX" of account "iCloud"
		--> "<zz@yy>"
	extract name from "<zz@yy>"
		--> "zz yy"
	count message id 223873 of mailbox "INBOX" of account "iCloud"
		--> 1
	count every mail attachment of item 1 of message id 223873 of mailbox "INBOX" of account "iCloud"
		--> 1
	get item 1 of every mail attachment of item 1 of message id 223873 of mailbox "INBOX" of account "iCloud"
		--> mail attachment id "2.2" of message id 223873 of mailbox "INBOX" of account "iCloud"
	get properties of mail attachment id "2.2" of message id 223873 of mailbox "INBOX" of account "iCloud"
		--> {downloaded:true, MIME type:"image/jpeg", name:"Logo Reduced 2.jpg", file size:5717, class:mail attachment, id:"2.2"}
	save mail attachment id "2.2" of message id 223873 of mailbox "INBOX" of account "iCloud" in "Macintosh HD:Users:xxxx:Documents:Mail_Attachments:zz yy - Pages to Doc Script"
		--> error number -10000
Résultat :
error "Erreur dans Mail : Le gestionnaire AppleEvent a échoué." number -10000

Yvan KOENIG (VALLAURIS, France) mardi 19 février 2013 17:03:18

Hi. The icon is generic because you are tossing the file extension in the act of renaming the file. You might still encounter a problem if the new name is too long, but this generally accounts for the main issue.

set tachmntIndex to 1
set destinationFolder to (((path to desktop) as text)) --& "Documents:Mail_Attachments:")

tell application "Mail"
	set themsg to (get selection)'s item 1
	set Subject_ to themsg's subject
	set Sender_ to extract name from themsg's sender
	try
		repeat with tachmnt in themsg's mail attachments
			save tachmnt in destinationFolder & Sender_ & " - " & Subject_ & space & tachmntIndex & (get tachmnt's name)'s text -4 thru -1
			set tachmntIndex to tachmntIndex + 1
		end repeat
	end try
end tell

After checking out my code I do see that it doesn’t work. I also can not get the code that Marc Anthony submitted. My knowledge of Applescript isn’t good enough to figure this one out.
Thanks for any help you can give.
PolishPrince

On my side, I uses GUIScripting which is the unique scheme allowing me to save every kind of attached files.

Yvan KOENIG (VALLAURIS, France) mercredi 20 février 2013 21:05:44

Every poster on this forum once had zero knowledge of Applescript. I provided only an example, not finished work. There are likely reasons it might fail; I observed that the new name may be too long. I also only accommodated for a three letter extension, but you might have four”or none.