Saving Mail Attachments

For some time I have been trying to automate the transfer of email files to Word. Craig has been a lot of help with this but the thread has got long so I thought I would start another.

The scrip that transfers the files to word works just fine but it does not take the attachments.

Does anyone know of a way to a) save the attachments separately b) as part of the Word file?

It was suggested that it would be better to save the emails as text files with a doc extension but I am afraid after numerous hours trying to adapt Craig’s script I cannot to that. Just a newbie I am afraid.

I have copied the working scrip below.


set folder_path to (choose folder) as string --Select Document file to put emails in
set desk_path to folder_path
global desk_path
tell application "Mail"
	set m_boxes to the name of every mailbox
	set this_box to choose from list m_boxes
	repeat with a_mess in (every message in mailbox (item 1 of this_box))
		my MakeWordDoc((a_mess's content), (my TestFixSubject((a_mess's subject))), ((a_mess's date received) as string))		
	end repeat
	delete every message in mailbox (item 1 of this_box)
end tell

on MakeWordDoc(con, sub, dat)

	set file_nm to (desk_path & sub)
	tell application "Microsoft Word"
		try
			set newDoc to open file name file_nm
			insert text " " at end of text object of newDoc
		on error
			set newDoc to make new document
		end try
		insert text con at end of text object of newDoc
		insert text " " at end of text object of newDoc
		insert text dat at end of text object of newDoc
		save as newDoc file name file_nm
		close every document
	end tell
	delay 5
end MakeWordDoc
-------------------------------------------------
on TestFixSubject(str)
	set fixed_string to {}
	set bad_char to {":", "/"}
	repeat with c from 1 to (count every character in str)
		if bad_char contains (character c of str) then
			set end of fixed_string to "-"
		else
			set end of fixed_string to (character c of str)
		end if
	end repeat
	fixed_string as string
end TestFixSubject

Any help greatly appreciated. Craig if you see this I cannot figure out why I cannot display the result of fixed_string atthe end of the handler TestFixSubject. It tells me I cannot do it and shows up each letter that makes up the string . I tried seting up another string “fs” that started with nothing and concatenated it as it looped and passed it to the str as the last step, that seemed to work but is not as streamlined as your approach.

Peter

Peter:

I haven’t had time to look at this until today. I think you did a great job adding the date, by the way, well done! Anyway, to display the fixed_string at the end of the TestFixString handler, insert this line after the [end repeat] and before the [fixed_string as string]:

display dialog (fixed_string as string)

The attachments are another issue entirely. The problem there is that you might get Word files, Excel files, JPGs, PDFs, etc., etc. Saving attachments is not a huge deal, but it does require some forethought. You will find a thread on that issue (ironically, one that I started) here. My recommendation for pursuing that would be to dedicate a folder somewhere to hold all these attachments, and have the script create subfolders by month or day, save any and all attachments in the folder, and then write the path at the end of the document.

As for making these text files instead of Word files, it is certainly possible, and I am confident that it would be much faster than using Word. I personally have extremely limited experience in that realm, but it sounds interesting. I may think about that over the next few days…

Hi Craig

thanks for that I have now got the script working for word. I don’t get that many attachements and I normally want to save them anyway so what I have done will work for me.

however I am having a terrible job using textedit rather than word. I started trying to build based on what you had done for word. If I can bother you once more first here is my final scrip for word.

set folder_path to (choose folder with prompt "Select the Destination Folder") --Select Document file to put emails in
set Des_Folder to info for folder_path
set Des_Folder to the name of Des_Folder --Returns tha name of the Folder not the Path
set desk_path to folder_path as string
global desk_path

tell application "Mail"
	set m_boxes to the name of every mailbox
	set this_box to choose from list m_boxes with prompt "Select Source"
	if Des_Folder as string ≠ this_box as string then --Checks if folders are the same clicking OK accepts the difference or clicking cancel exits the scrip
		display dialog (("Target Selected is " & Des_Folder) & return & "Mail box is " & this_box) & return & " is this correct"
	end if
	repeat with a_mess in (every message in mailbox (item 1 of this_box))
		--Built to check if get names of file attachments
		set fn to (a_mess's subject) --Gets email Subject line
		repeat with attach from 1 to count every mail attachment of a_mess --Cycles through every attachment
			try
				set fn2 to fn & " " & the name of mail attachment attach of a_mess as string --Adds Attachment Name to email Subject
				save mail attachment attach of a_mess in desk_path & fn2 --Saves attachment	
			end try
		end repeat
		my MakeWordDoc((a_mess's content), (my TestFixSubject((a_mess's subject))), ((a_mess's date received) as string))
	end repeat
	--delete every message in mailbox (item 1 of this_box)
end tell

on MakeWordDoc(con, sub, dat)
	
	set file_nm to (desk_path & sub)
	tell application "Microsoft Word"
		try --Trys to save file with Subject Name for eMail if file name exists adds  text to existing file
			set newDoc to open file name file_nm --If file already exists it is opened
			insert text " " at end of text object of newDoc --Then adds space to existing file
		on error --File name does not exist it creates a file with name of Subject from email
			set newDoc to make new document
		end try
		insert text con at end of text object of newDoc --Adds Message text
		insert text " " at end of text object of newDoc --Adds a space
		insert text dat at end of text object of newDoc --Adds the date of the message		
		save as newDoc file name file_nm --Saves Document with file name of the subject in email message
		close every document
	end tell
	delay 5
end MakeWordDoc
-------------------------------------------------
on TestFixSubject(str)
	set fixed_string to {}
	set bad_char to {":", "/"}
	repeat with c from 1 to (count every character in str)
		if bad_char contains (character c of str) then
			set end of fixed_string to "-"
		else
			set end of fixed_string to (character c of str)
		end if
	end repeat
	fixed_string as string
end TestFixSubject

Then my basic script for text edit


on MakeTextDoc(con, sub, dat)
	
	set file_nm to (desk_path & sub & ".rtf")
	
	tell application "TextEdit"
		open file_nm
		
		make new document at front
		set name of window 1 to file_nm
		set text of front document to con
		save front document as (Unicode text) in file_nm		
		close every document
		
	end tell
	
	

I have tried adding “try”, “on error”, and “end try” as you did opening the text file if it is there (I would then select the text and add tyhe new text to it) but the error trapping does not seem to work. To duplicate my problem I wrote a short script

tell application "TextEdit"	
	
	try	
		open file "macintosh hd:users:petermitchell:documents:test1:texttest3.rtf"
	on error
		display dialog "Error"
		
	end try
end tell

but all this does is return the error " Cannot find the file" which I have to accept and then it shows the the dialog box with the word Error. In short it is not trapping the error. As in most cases the file does not exist I would be clicking accpet all the time. Can you suggest a way around it as you know it works just fine with word.

Thanks

Peter

Peter:

I am short on time again today, sorry, I will look over the big script later this week.

As for writing text files, you do not even need TextEdit (or any other editor, for that matter). You simply use Applescript. something like this:

try
	set w to open for access file "macintosh hd:users:petermitchell:documents:test1:texttest3.rtf" with write permission
on error
	display dialog "Error"
	--Send to a handler to make the file, and write something there, like the file path or something
	--set w to open for access file "macintosh hd:users:petermitchell:documents:test1:texttest3.rtf" with write permission--You now have to actually open the file.
end try
write whatever to w starting at eof --Writes to file, starting at the end of the file, so it appends to what is already there.
close access w

This may not be exact, but the idea is pretty close. I can check back in a couple of days to see how things are going.

Craig

Thanks as always for a very quick reply. I am also travelling to morrow but will try what you suggest and see how I go.

Take care

Peter

Craig

I have tried and tried and must be doing something stupid. your script works as a stand alone but the moment I incorporate it into my scrip (well yours as amended) it does not. If the file is not in the folder it puts it there but then does not exit the try but then displays the dialog that I inserted after on error. If it is there it does the same thing. Here is my script if you have a chnace to look at it, I apologise again.

set folder_path to (choose folder with prompt "Select the Destination Folder") --Select Document file to put emails in
set Des_Folder to info for folder_path
set Des_Folder to the name of Des_Folder --Returns tha name of the Folder not the Path
set desk_path to folder_path as string
global desk_path

tell application "Mail"
	set m_boxes to the name of every mailbox
	set this_box to choose from list m_boxes with prompt "Select Source"
	if Des_Folder as string ≠ this_box as string then --Checks if folders are the same clicking OK accepts the difference or clicking cancel exits the scrip
		display dialog (("Target Selected is " & Des_Folder) & return & "Mail box is " & this_box) & return & " is this correct"
	end if
	repeat with a_mess in (every message in mailbox (item 1 of this_box))
		--Built to check if get names of file attachments
		set fn to (a_mess's subject) --Gets email Subject line
		repeat with attach from 1 to count every mail attachment of a_mess --Cycles through every attachment
			try
				set fn2 to fn & " " & the name of mail attachment attach of a_mess as string --Adds Attachment Name to email Subject
				save mail attachment attach of a_mess in desk_path & fn2 --Saves attachment	
			end try
		end repeat
		my MakeTextDoc((a_mess's content), (my TestFixSubject((a_mess's subject))), ((a_mess's date received) as string))
		
	end repeat
	--delete every message in mailbox (item 1 of this_box)
end tell

on MakeTextDoc(con, sub)
	set file_nm to (desk_path & sub)
	try
		display dialog "The file name is " & sub
		set newDoc to open for access file file_nm with write permission
	on error
		display dialog "Error with file name"
	end try
	
	
end MakeTextDoc

-------------------------------------------------
on TestFixSubject(str)
	set fixed_string to {}
	set bad_char to {":", "/"}
	repeat with c from 1 to (count every character in str)
		if bad_char contains (character c of str) then
			set end of fixed_string to "-"
		else
			set end of fixed_string to (character c of str)
		end if
	end repeat
	fixed_string as string
end TestFixSubject

I am sorry but I have tried what I can think of if you can help it would be much appreciated.

Peter

Craig

I said I was stupid I did not realise I had to activate an application. My amended script is below I am still owrking on it but did not want you to waste your time if I have corrected my own dumb mistake.

set folder_path to (choose folder with prompt "Select the Destination Folder") --Select Document file to put emails in
set Des_Folder to info for folder_path
set Des_Folder to the name of Des_Folder --Returns tha name of the Folder not the Path
set desk_path to folder_path as string
global desk_path

tell application "Mail"
	set m_boxes to the name of every mailbox
	set this_box to choose from list m_boxes with prompt "Select Source"
	if Des_Folder as string ≠ this_box as string then --Checks if folders are the same clicking OK accepts the difference or clicking cancel exits the scrip
		display dialog (("Target Selected is " & Des_Folder) & return & "Mail box is " & this_box) & return & " is this correct"
	end if
	repeat with a_mess in (every message in mailbox (item 1 of this_box))
		--Built to check if get names of file attachments
		set fn to (a_mess's subject) --Gets email Subject line
		repeat with attach from 1 to count every mail attachment of a_mess --Cycles through every attachment
			try
				set fn2 to fn & " " & the name of mail attachment attach of a_mess as string --Adds Attachment Name to email Subject
				save mail attachment attach of a_mess in desk_path & fn2 --Saves attachment	
			end try
		end repeat
		my MakeTextDoc((a_mess's content), (my TestFixSubject((a_mess's subject))), ((a_mess's date received) as string))
		
	end repeat
	--delete every message in mailbox (item 1 of this_box)
end tell

on MakeTextDoc(con, sub, dat)
	set file_nm to (desk_path & sub)
	tell application "Finder"
		try
			--display dialog "The file name is " & sub
			set newDoc to open for access file file_nm with write permission
			write "  " to newDoc starting at eof
		on error
			set newDoc to open for access file file_nm with write permission
			display dialog "Error with file name"
		end try
		write con to newDoc starting at eof
		write " " to newDoc starting at eof
		write dat to newDoc starting at eof
		close access newDoc
		
	end tell
end MakeTextDoc

-------------------------------------------------
on TestFixSubject(str)
	set fixed_string to {}
	set bad_char to {":", "/"}
	repeat with c from 1 to (count every character in str)
		if bad_char contains (character c of str) then
			set end of fixed_string to "-"
		else
			set end of fixed_string to (character c of str)
		end if
	end repeat
	fixed_string as string
end TestFixSubject

Thanks

Peter

Peter:

You posted your reply whilst I was looking this over. Actually, you do not need either a Finder tell, or a try block to do this. I had forgotten that when you use the [open for access] command that AS will create the file if it does not already exist. That fact simplifies things greatly. You can now simply call the handler, send the parameters, and it will go nicely:

set folder_path to (choose folder with prompt "Select the Destination Folder") --Select Document file to put emails in
set Des_Folder to info for folder_path
set Des_Folder to the name of Des_Folder --Returns tha name of the Folder not the Path
set desk_path to folder_path as string
global desk_path

tell application "Mail"
	set m_boxes to the name of every mailbox
	set this_box to choose from list m_boxes with prompt "Select Source"
	if Des_Folder as string ≠ this_box as string then --Checks if folders are the same clicking OK accepts the difference or clicking cancel exits the scrip
		display dialog (("Target Selected is " & Des_Folder) & return & "Mail box is " & this_box) & return & " is this correct"
	end if
	repeat with a_mess in (every message in mailbox (item 1 of this_box))
		--Built to check if get names of file attachments
		set fn to (a_mess's subject) --Gets email Subject line
		repeat with attach from 1 to count every mail attachment of a_mess --Cycles through every attachment
			try
				set fn2 to fn & " " & the name of mail attachment attach of a_mess as string --Adds Attachment Name to email Subject
				save mail attachment attach of a_mess in desk_path & fn2 --Saves attachment	
			end try
		end repeat
		my MakeTextDoc((a_mess's content), (my TestFixSubject((a_mess's subject))), ((a_mess's date received) as string))
		
	end repeat
	--delete every message in mailbox (item 1 of this_box)
end tell

on MakeTextDoc(con, sub, dat)
	set file_nm to (desk_path & sub & ".txt")
	set newDoc to open for access file file_nm with write permission
	write con to newDoc starting at eof
	write " " to newDoc starting at eof
	write dat & return & return to newDoc starting at eof
	close access newDoc
end MakeTextDoc

-------------------------------------------------
on TestFixSubject(str)
	set fixed_string to {}
	set bad_char to {":", "/"}
	repeat with c from 1 to (count every character in str)
		if bad_char contains (character c of str) then
			set end of fixed_string to "-"
		else
			set end of fixed_string to (character c of str)
		end if
	end repeat
	fixed_string as string
end TestFixSubject

I only messed around with the MakeTextDoc portion; the rest is unchanged. Are you at all interested in my earlier suggestions about directing messages to specific folders, and having those folders created as you go?

Craig

Thanks once again Ihad discovered that it created the file if it was not there and thought I was doing something wrong as we never need the on error part. Must admit too embarrased to ask I seem to be in the slow lane.

I have run into a new problem on which I started a new thread and that is sorting the mailboxes before I make a selection. Mailboxes in my mail account are in the correct order but some of them have sub boxes and they therefore do not list correctly. I found a script on the web which I adapted a little but it stops after it as returned all the names the message “Mail got an error can’t continue MailBox Sort”. Here’s the script.


tell application "Mail"
	set m_boxes to the name of every mailbox
	set this_box to choose from list MailBox_Sort(the m_boxes)
end tell
on MailBox_Sort(my_list)
	set the index_list to m_boxes
	set the sorted_list to {}
	repeat (the number of items in my_list) times
		set the low_item to ""
		repeat with i from 1 to (number of items in my_list)
			if i is not in the index_list then
				set this_item to item i of my_list as text
				if the low_item is "" then
					set the low_item to this_item
					set the low_item_index to i
				else if this_item comes before the low_item then
					set the low_item to this_item
					set the low_item_index to i
				end if
			end if
		end repeat
		set the end of sorted_list to the low_item
		set the end of the index_list to the low_item_index
	end repeat
	return the sorted_list
end MailBox_Sort

I am assuming it has something to do with the form of the mailbox list and have tried coercing it to text etc but to no avail. The test scrip I sourced it from had a built in list when I run that with the log on I see that the list just has curly brackets either end, where as when I run my scrip the mailbox list has regular brackets outside the curly ones if that means anything.

By the way am really interested in creating the destination folders on the fly otherwise I have to go through each of my machines and make sure I have identical folders. As yuo can see I have amended your scrip so as I first select the destination folder then after the mailbox selection I test to see they both have the same name if not the option exists to cancel the script.
Once again thank you.

Peter

Peter:

Coming along nicely, you are. I found a couple of issues with your script, and then the sort routine you had was not working at all, so I replaced it with a bubblesort routine, I hope you don’t mind, and I hope it works for you. My Mailboxes are not nearly as complex as you state yours are, so we will see. Anyway, the main thing to remember is that when you call a handler from inside of a tell block, use the term [my] as outlined here:

tell application "Mail"
	set m_boxes to the name of every mailbox
	set this_box to choose from list (my bubblesort(m_boxes)) --Need to call [my] bubblesort, since the handler is outside of the tell
end tell

on bubblesort(array)
	repeat with i from length of array to 2 by -1 --> go backwards
		repeat with j from 1 to i - 1 --> go forwards
			tell array
				if item j > item (j + 1) then
					set {item j, item (j + 1)} to {item (j + 1), item j} -- swap
				end if
			end tell
		end repeat
	end repeat
	return array
end bubblesort

I found the bubblesort code out there in cyberspace a year or two ago, and it has no author listed, but I know it was not I. It is nifty, however, and I like it.

Good luck. Give me some idea of how you want to structure the folder issue and we can work on that next.

Hi Craig

Having posted my last problem I kept plugging away with no success so as so as I got your bubble sort I tried it and it works just great, thank you, all i have to do now is plug it into the rest of the script and I will be nearly there.

In respect of the folder issue what I would like to do is select the mailbox as that is the starting place check and see if I have that folder in Documents and if I do not create it, hopefully that makes sense. One of the reasons I started this exercise is that I move around a lot and use a number of email accounts and I could not find a way of synchronizing them. Anything in my mac account was OK of course but not the others. Anyway I figured the stuff that was relevant probably should not stay in the mail application. This application therefore is to clean up a large mail account. Once that is done (on three machines) I need to figure out how to attach the scrip or an appropriate variation to run every time I reply to an email. The reason I want to do it after a reply is to make sure I reply and don’t just shunt of the incoming to document folders. It would mean getting the source and reply emails at the same time. I got giddy thinking about it as rules in mail don’t automatically move the sent emails to the appropriate mailbox as they do incoming or at least mine don’t. Once again any thoughts greatly appreciated.

Thanks

Peter

Peter:

Here is a little handler I put together for another purpose this year. Every time you run this, it will check to see if a folder exists for each Mailbox, and if not, create it:

tell application "Mail"
	set m_boxes to the name of every mailbox
	my CheckOrMakeFolder(m_boxes)
	--set this_box to choose from list (my bubblesort(m_boxes)) --Need to call [my] bubblesort, since the handler is outside of the tell
end tell
--------------------------------------------------------
on CheckOrMakeFolder(mbx)
	set d to (path to documents folder as Unicode text) & "Mailboxes:"
	repeat with bx in mbx
		set dp to (d & (bx as string) & ":")
		tell application "Finder" to if not (exists folder dp) then do shell script "mkdir -p " & quoted form of POSIX path of dp
	end repeat
end CheckOrMakeFolder

Now, every time the script is run, the folders will always be there, based on the names of the mailboxes. When a mailbox is chosen, you can simply use that choice to build the proper path to access that folder.

I hope this is close to what you are looking for.

Craig:

I amended your script so it worked as I selected each folder and it works just fine with one very minor problem. The way I now have it I have to have starting folder in Documents to which I add another folder. In most cases I do if for instance I have a folder in documents called “Friends” I can then add a specific friend who has a mailbox in mail. I tried amending the script in the CheckOrMakeFolder sub routine so the set d to (path documents folder was path to users folder but that put it in a documents folder in users when I wanted it in my user folder “Mitch” but I have to admit I cannot figure out who to address that can you help please. By the way some one called Graff wrote the bubble sort (or something very similar ) in 2004. Here is my script (read yours as amended).

--Script Name eMailToTextWithAttachments4
set folder_path to (choose folder with prompt "Select the Destination Folder") --Select Primary  Document folder 
set Des_Folder to info for folder_path
set Des_Folder to the name of Des_Folder --Returns tha name of the Folder not the Path
set desk_path to folder_path as string
global desk_path
global Des_Folder
tell application "Mail"
	set m_boxes to the name of every mailbox
	
	set this_box to choose from list (my bubblesort(m_boxes)) --Calls sub routine to Sort Mailboxesl to order
	my CheckOrMakeFolder(this_box) -- Calls sub routine to see if a folder with the same name as the mailbox exists in the primary folder 
	--Check if message has any attachments	
	repeat with a_mess in (every message in mailbox (item 1 of this_box))
		
		set fn to (a_mess's subject) --Gets email Subject line
		repeat with attach from 1 to count every mail attachment of a_mess --Cycles through every attachment
			try
				set fn2 to fn & " " & the name of mail attachment attach of a_mess as string --Adds Attachment Name to email Subject
				save mail attachment attach of a_mess in desk_path & "eMail " & fn2 --Saves attachment	
			end try
		end repeat
		my MakeTextDoc((a_mess's content), (my TestFixSubject((a_mess's subject))), ((a_mess's date received) as string))
	end repeat
	--delete every message in mailbox (item 1 of this_box)
end tell
-----------------------------
on MakeTextDoc(con, sub, dat) --Saves Mail Message as a text file 
	set file_nm to (desk_path & "eMail " & sub & ".txt")
	set newDoc to open for access file file_nm with write permission --If file name exists opens that file otherwise creates a new file with the subject  from the email message as its name
	write con to newDoc starting at eof --Content of eamil message pasted to file
	write " " to newDoc starting at eof --space
	write dat & return & return to newDoc starting at eof --adds date of email message
	close access newDoc
end MakeTextDoc

-------------------------------------------------
on TestFixSubject(str) --strips characters that may cause a problem from the email message Subject
	set fixed_string to {}
	set bad_char to {":", "/"}
	repeat with c from 1 to (count every character in str)
		if bad_char contains (character c of str) then
			set end of fixed_string to "-"
		else
			set end of fixed_string to (character c of str)
		end if
	end repeat
	fixed_string as string
end TestFixSubject
-----------------------
on CheckOrMakeFolder(bx) --checks if folder same name as mailbox exists if not creates one
	set d to (path to documents folder as Unicode text) & Des_Folder & ":" --repeat with bx in mbx
	set dp to (d & (bx as string) & ":")
	--display dialog dp as string
	tell application "Finder" to if not (exists folder dp) then do shell script "mkdir -p " & quoted form of POSIX path of dp
	set desk_path to dp
end CheckOrMakeFolder
-----------------------------
on bubblesort(array) --Sorts mailboxs into alphabetical order
	repeat with i from length of array to 2 by -1 --> go backwards
		repeat with j from 1 to i - 1 --> go forwards
			tell array
				if item j > item (j + 1) then
					set {item j, item (j + 1)} to {item (j + 1), item j} -- swap
				end if
			end tell
		end repeat
	end repeat
	return array
end bubblesort

As always thank you I do try and figure it out myself but the brain doesn’t work that well any more, it was easier when I was hard wiring IBM 501.

Peter

Craig:

That may not have made sense, what I want to do is create a folder in the root of Documents.

thanks

Peter

Peter:

I am sorry, you are correct; I do not understand what you want. It appears that the current form of the CheckOrMakeFolder handler in your script will create ALL the folders in the user chosen folder of the Documents folder. Are you trying to make a single root folder automatically and not use the user prompt?

I apologize for being dense; I am a veterinarian by profession, so I do my scripting a lot like I do surgery - one piece at a time, and then put it all together at the end.

If you could try to explain your desires more clearly, I will do my best to help out.

Craig:

Thank you I knew that I had not explained it well. The script as amended that I attached to my last post starts at the document folder. So if I do not have as a subfolder of documents as the starting point I cannot create a new folder. The more I try and explain it the worse it gets so lets take an example. Say I have as a subfolder of documents a folder named Friends and in mailboxes I have Craig. The script works fine I just select Friends it sees that I do not have within Friends a sub folder called Craig and creates it. However if I do not have a sub folder called friends what I need to do is create a subfolder in Documents called Friends and within that a folder called Craig. Does that make it any clearer. The more I try and describe it the worse it gets so I apologise for that.

Thank you

Peter

Craig:

I have solved the immediate problem by checking if folder selected is in fact “Documents” then I just change the path. Of course if I have more than two layers of folders the same problem will occur for instance Documents/Bussiness/Oil. Will keep working on that I feel it is something to do with how I address the path.

I thought I was just about fixed but have run into a new and far more significant problem. As you know I am saving attachments in email messages but I find that I cannot return the content of the message itself if it has an attachment. I have tried to work out how to delete the attachments before I get the content but cannot do that either. Have gone back to some of the earlier scripts before I was saving the attachments in fact before I switched from Word to Text and the same thing happens the content is not returned well as content I get ˇ¸Ë‡¸ and then the date of the message. When I look at the log it shows it retuning “??” instead of the actual content. I am attaching the latest script I am working with any suggestions as always greatly appreciated.

--Script Name eMailToTextWithAttachments6Test
set folder_path to (choose folder with prompt "Select the Destination Folder") --Select Primary  Document folder 
set Des_Folder to info for folder_path
set Des_Folder to the name of Des_Folder --Returns tha name of the Folder not the Path
set desk_path to folder_path as string
global desk_path
global Des_Folder
tell application "Mail"
	set m_boxes to the name of every mailbox
	
	set this_box to choose from list (my bubblesort(m_boxes)) --Calls sub routine to Sort Mailboxes to order
	my CheckOrMakeFolder(this_box) -- Calls sub routine to see if a folder with the same name as the mailbox exists in the primary folder 
	--Check if message has any attachments	
	repeat with a_mess in (every message in mailbox (item 1 of this_box))
		
		set fn to (a_mess's subject) --Gets email Subject line
		repeat with attach from 1 to count every mail attachment of a_mess --Cycles through every attachment
			set fn3 to the name of mail attachment attach of a_mess as string
			display dialog fn3
			try
				set fn2 to fn & " " & the name of mail attachment attach of a_mess as string --Adds Attachment Name to email Subject
				save mail attachment attach of a_mess in desk_path & "eMail " & fn2 --Saves attachment
				display dialog "Try deletion"
				delete mail attachment of a_mess --this does not work
				delete fn3 --this does not work
				delete mail attachment fn3 in a_mess --this does not work
			end try
		end repeat
		
		my MakeTextDoc((a_mess's content), (my TestFixSubject((a_mess's subject))), ((a_mess's date received) as string))
	end repeat
	--delete every message in mailbox (item 1 of this_box)
end tell
-----------------------------
on MakeTextDoc(con, sub, dat) --Saves Mail Message as a text file 
	display dialog con
	set file_nm to (desk_path & "eMail " & sub & ".txt")
	set newDoc to open for access file file_nm with write permission --If file name exists opens that file otherwise creates a new file with the subject  from the email message as its name
	write con to newDoc starting at eof --Content of eamil message pasted to file
	write " " to newDoc starting at eof --space
	write dat & return & return to newDoc starting at eof --adds date of email message
	close access newDoc
end MakeTextDoc

-------------------------------------------------
on TestFixSubject(str) --strips characters that may cause a problem from the email message Subject
	set fixed_string to {}
	set bad_char to {":", "/"}
	repeat with c from 1 to (count every character in str)
		if bad_char contains (character c of str) then
			set end of fixed_string to "-"
		else
			set end of fixed_string to (character c of str)
		end if
	end repeat
	fixed_string as string
end TestFixSubject
-----------------------
on CheckOrMakeFolder(bx) --checks if folder same name as mailbox exists if not creates one
	--display dialog Des_Folder
	if Des_Folder = "documents" then --Check to see if root of Documents selected		
		set d to (path to documents folder as Unicode text)
	else
		set d to (path to documents folder as Unicode text) & Des_Folder & ":"
	end if
	--display dialog d
	set dp to (d & (bx as string) & ":")
	--display dialog quoted form of POSIX path of dp
	tell application "Finder" to if not (exists folder dp) then do shell script "mkdir -p " & quoted form of POSIX path of dp
	set desk_path to dp
end CheckOrMakeFolder
-----------------------------
on bubblesort(array) --Sorts mailboxs into alphabetical order
	repeat with i from length of array to 2 by -1 --> go backwards
		repeat with j from 1 to i - 1 --> go forwards
			tell array
				if item j > item (j + 1) then
					set {item j, item (j + 1)} to {item (j + 1), item j} -- swap
				end if
			end tell
		end repeat
	end repeat
	return array
end bubblesort

thanks

Peter

Craig

It is probably obvious but the moment I remove the attachment manually the content of the scrip is saved. So the trick is the coding to remove the attachment once i have saved it.

thanks

Peter

Peter:

I really don’t know what to say. I am still confused about your folder concerns; I think the way you have running now is quite good, in fact.

In addition, the script runs perfectly well for me as is. I created a folder in Mail with a few messages, some with attachments, some without. I ran the script EXACTLY as you have it in your latest post; all I did was click on the link to open it in my editor, and clicked run. I got a bunch of weird display dialogs (for your de-bugging work, I am sure), and in the end, I examined the newly created files in the folder I selected. Every single message was saved in the appropriate .txt files precisely as they were in the MailBox. All the attachments were also saved in that folder, but there was no indication which attachment went with which email.

Craig

You must think I am nuts I do. I had been running my tests on one file with an attachment so when I read your post I decided maybe it was a problem with just that file so I loaded my test mail box with 10 files with attachments and it worked for 50% of them the others it simply did not pick up the messages but did the attachments. I have run it half a dozen times and it is always the same files so it is not a random error. My test included files I had sent and received with attachments so it not appear to have anything to do with that.

The solution seems to me to be to save the attachments then remove them from the email and process the email itself, if I do this manually (removing the attachment) it works just fine. As you know Mail has an option mail/remove attachments; and my research suggests that what Mail does is trash the file and replace it minus the attachment. As you can see from my script I have tried a number of ways to delete the attachment from the mail message once I have saved it, all did not work. If you could suggest how I could do this it would really help.

By the way if you look at the file names for the attachments you will see it starts with “email” so I know where it came from and it is then concatenated with the subject line from the message followed by the name of the attachment so when you sort the files in the folder by name they come together.
I can only apologise again for taking so much of your time with this problem but I fell close and hope you are not over frustrated with me. I am sure I can work around my folder problem, one way would be to simply set up master folders in Documents so please do not think about that anymore.

Thanks as always

Peter