Trying to save mail attachments based on their types

Hello community…

I am trying to create a script that will save an .xls file and maybe several .jpg(s) files into a folder on my desktop that will be created with the name of the .xls file

The script will start from 1 mail selected, it will always contains an .xls attached files with sometimes some pictures then after saving the .xls file it should open it, it does not need to open the pictures just save them in the same folder as the .xls file.
I have 2 pictures in the signature that I do not want to save called savepaper.gif & s_bar.gif.

Here is a piece of script I started it works well to create non existing folders on my desktop but do not save the particular files in the newly created folders and I cannot figure how to create the enclosing folder with the file name of my .xls file…:frowning:


set destinationFolder to "Macintosh HD:Users:clbair2012:Desktop:Temp RLPS:Temp RLPS from mail new inquiry"
set listOfAttachments to {}
property MasterShareFolder : "Temp RLPS"
set folderList to "'RLPS Format','Temp RLPS from mail new inquiry'"
set picsFolder to "Macintosh HD:Users:clbair2012:Desktop:Temp RLPS:Temp RLPS from mail new inquiry"
set docsfolder to "Macintosh HD:Users:clbair2012:Desktop:Temp RLPS:Temp RLPS from mail new inquiry"
set picsExt to {".jpg", ".tif", ".png", ".psd"} #I omitted on purpose the ".GIF" as the signature uses it
set excelFile to {".xls"}

-- create folders in case they don't exist
do shell script "/bin/mkdir -p " & quoted form of POSIX path of (path to desktop) & quoted form of MasterShareFolder & "/{" & folderList & "}"

--Save mail attachment based on their types and open the xls file.
tell application "Mail"
	set selected_messages to selection
	repeat with cont1 from 1 to count (selected_messages)
		set theAttachments to every mail attachment of item cont1 of selected_messages
		repeat with cont2 from 1 to count (theAttachments)
			set saveAttachment to item cont2 of theAttachments
			set saveName to destinationFolder & name of saveAttachment
			set allFiles to files of selected_messages
			repeat with theFile in allFiles
				copy name of theFile as string to FileName
				
				repeat with ext in picsExt
					if FileName ends with ext then
						save theFile in picsFolder
					end if
				end repeat
				
				repeat with ext in docsExt
					if FileName ends with ext then
						save theFile in docsfolder
					end if
				end repeat
			end repeat
		end repeat
	end repeat
end tell

tell application "Finder" to open excelFile using application file id

Not sure that it would be sufficient but it would be a good idea to define the list docsExt.

In the last instruction you try to open the file whose pathname is excelFile but excelFile is the list {“.xls”}.

I guess that one or two of these three instructions is(are) wrong :
set destinationFolder to “Macintosh HD:Users:clbair2012:Desktop:Temp RLPS:Temp RLPS from mail new inquiry”
set picsFolder to “Macintosh HD:Users:clbair2012:Desktop:Temp RLPS:Temp RLPS from mail new inquiry”
set docsfolder to “Macintosh HD:Users:clbair2012:Desktop:Temp RLPS:Temp RLPS from mail new inquiry”

I don’t understand what’s the need for the variable saveName

As far as I know, instructions like :

set saveName to destinationFolder & name of saveAttachment
copy name of theFile as string to FileName

must be targeted to System Events or Finder but not to Mail.

Yvan KOENIG (VALLAURIS, France) mercredi 19 février 2014 22:17:15

Hello Yvan,

Thanks for your answer.

The list of excel document actually is the part where I am confused since this list should be made from the saved .xls attachment. Actually it will always be only 1 file but with different names on each emails. Could you show me how to define this list?

I realise by reading your comment my mistake then I am lost here again in opening the proper file could you give me a hint on how to open this particular file?

The saveName is for mail to save the attachment to the proper folder but it does not work I guess then it is because mail does not do these things… Could you teach me then how to do it with finder or System Events?

Thanks for your time.

As it seems that you are lost when I ask several questions in a single answer I will try to help step by step.

May you double check the beginning of you script which I edited a bit.

property MasterShareFolder : "Temp RLPS"
set p2d to path to desktop as text # This way the script will not be linked to a single machine

# I create variables for the subfolderNames so the script
# will be easier to edit if you decide to change these names
set subFolder1 to "RLPS Format"
set subFolder2 to "Temp RLPS from mail new inquiry"
set folderList to quoted form of subFolder1 & "," & quoted form of subFolder2

-- create folders in case they don't exist
do shell script "/bin/mkdir -p " & quoted form of POSIX path of (p2d & MasterShareFolder) & "/{" & folderList & "}"

# May you check what these pathnames must really be.
# My guess is that they may be :
# set destinationFolder to p2d & MasterShareFolder 
# set picsFolder to p2d & MasterShareFolder & ":" & subFolder1
# set docsfolder to p2d & MasterShareFolder & ":" & subFolder2

set destinationFolder to p2d & MasterShareFolder & ":" & "Temp RLPS from mail new inquiry"
set picsFolder to p2d & MasterShareFolder & ":" & "Temp RLPS from mail new inquiry"
set docsfolder to p2d & MasterShareFolder & ":" & "Temp RLPS from mail new inquiry"

As I already wrote yesterdays, I’m quite sure that two definitions among those of the three pathnames are wrong.
If picsFolder and docsFolder are equal, there is no need to treat separately spreadsheet documents and picture ones as they would be stored in the same folder.

Yvan KOENIG (VALLAURIS, France) jeudi 20 février 2014 12:30:50

Hello Yvan,

Thank you for teaching me how to create this script.

The modified beginning of the script is correct the way you did it. I want to create in the “subFolder2” another sub-folder called with the .xls attachment name and then save inside the newly created folder both the .xls attachment and the pictures of my mail.

You are correct picsFolder and docsFolder are equal.

I hope that the embedded comments are clear enough.
If they aren’t, ask.

# for this code, there is no need to work with a property
# property MasterShareFolder : ""

set MasterShareFolder to "Temp RLPS"
set p2d to path to desktop as text # This way the script will not be linked to a single machine

# I create variables for the subfolderNames so the script
# will be easier to edit if you decide to change them
set subFolder1 to "RLPS Format"
set subFolder2 to "Temp RLPS from mail new inquiry"
set folderList to quoted form of subFolder1 & "," & quoted form of subFolder2

-- create folders in case they don't exist
do shell script "/bin/mkdir -p " & quoted form of POSIX path of (p2d & MasterShareFolder) & "/{" & folderList & "}"

# I choose subfolder2 but maybe it must be subfolder1
set destinationFolder to p2d & MasterShareFolder & ":" & subFolder2
# set picsFolder to p2d & MasterShareFolder & ":" & subfolder2
# set docsfolder to p2d & MasterShareFolder & ":" & subfolder2


# It doesn't matter here but the dot is not supposed to be included in the name extension
set picsExt to {".jpg", ".tif", ".png", ".psd"} #I omitted on purpose the ".GIF" as the signature uses it
set docsExt to {".xls"}

# Save mail attachment based on their types and open the xls file.
tell application "Mail"
	set selected_messages to selection
	repeat with a_message in selected_messages
		# get the attachments embedded in the message
		set the_attachments to every mail attachment of a_message
		# define a nil subfolder name
		set the_xls_path to ""
		# first loop accross the attached files to create the destination folder
		repeat with an_attachment in the_attachments
			# get the attachment's name
			set attachment_name to name of an_attachment
			if attachment_name ends with ".xls" then
				# builds the name of the subfolder supposed to receive the files
				set destination_name to rich text 1 thru -5 of attachment_name
				# builds its full pathname
				set destination_path to destinationFolder & ":" & destination_name
				# asks System Events to create the subfolder if it doesn't already exists
				tell application "System Events"
					if not (exists folder destination_path) then
						make new folder at end of folder destinationFolder with properties {name:destination_name}
					end if
				end tell
				# builds the pathname of the stored Xls file
				set the_xls_path to destination_path & ":" & attachment_name
				exit repeat # no need to continue to loop
			end if
		end repeat # in the first loop accross attachments
		
		if the_xls_path > "" then
			# as theXlsName is not nil, the destination folder exists
			# loop one more time accross the attached files to save them
			repeat with an_attachment in the_attachments
				# get the attachment's name
				set attachment_name to name of an_attachment
				repeat with ext in (picsExt & docsExt) # concatenate the lists to loop only once
					if attachment_name ends with ext then
						# try to save the attached file in the destination folder
						# I assume that the subfolder is used only once
						# If I'm wrong add code to treat possible duplicate names
						try
							save an_attachment in file (destination_path & ":" & attachment_name)
						end try
					end if
				end repeat # with ext
			end repeat # in the 2nd loop accross the Attachments
			# here instruction supposed to open the xls file
			try
				tell application "Finder" to open (the_xls_path as alias)
			end try
		end if
	end repeat # in the loop accross selected messages
end tell

Yvan KOENIG (VALLAURIS, France) jeudi 20 février 2014 19:20:05

Big thanks Yvan the script does what I needed and with your explanations I can follow it through it will help me a lot to build more scripts. One more time thank you.