Multiple Attachments in Mail

Hi all,

Can anyone tell me how to attach multiple files to a Mail.app new message?

Thanks

Hi,

you need one line for each attachment like this


tell application "Mail"
	tell newMessage
		tell content to make new attachment with properties {file name:theAttachment1 as alias} at after the last paragraph
		tell content to make new attachment with properties {file name:theAttachment2 as alias} at after the last paragraph
	end tell
end tell

or a repeat loop

Does not work for me, is there more to this script? I want to send all files containing ‘X’ in the title, in a folder. each file has, besides form ‘X’ the time or a date in it.

thanks

Hello

Here is something to get you going.

This script will not choose some static filenames for you or based on a pattern, but you must manually select the files.
then run the script from the script menu.

If this is something you would like to save to multiple recipients I guess you can save the initial message as a draft or do something else in mail to keep as a “mail template”. You should install the script somewhere in the script menu and run it from there.

global selected_items

tell application “Finder”
activate
set the selected_items to (selection)
if the selected_items is {} then
beep
display dialog “Please choose some file to attach to a new mail message and run this script again!” buttons {“Cancel”} default button 1
error number -128
end if
end tell

tell application “Mail”
activate

try
	set newMessage to make new outgoing message with properties {visible:true, subject:"My Subject", content:"My Body"}
	
	tell newMessage
		repeat with anAttachment in the selected_items
			tell content to make new attachment with properties {file name:anAttachment as alias} at after the last paragraph
		end repeat
	end tell
on error the error_message number the error_number
	display dialog "Error: " & the error_number & ". " & the error_message buttons {"Cancel"} default button 1
end try

end tell

Best regards

McUsr

thanks McUsr, that would work except that this script needs to run on my blind server, so no person there to supervise.

Well

How do you think of triggering the script?

And how would you supply the address of the recipient.

Would you have it implemented as folder action, or will you manually supply the filenames, - the address and whatnot to
an applet ??

What are your thoughts about this workflow?

Isn’t there any utilities that goes with MacOsX server?

Best Regards

McUsr

Hi McUsr, first of I am using a normal OSX version, not a server one.

Workflow.

Triggered by iCal a script would create a email with recipients etc. that is simple, I have all that.


tell application "Mail"
	activate
end tell
delay 6
set theSubject to ((do shell script "date '+%d.%m.%Y'") & (" Subject ")) --change me 
set theBody to ((do shell script "date '+%d-%m-%Y'") & (" 

Body ")) --change me
set theTarget to "you@XYZ" --change me
tell application "Mail"
	set newMessage to make new outgoing message with properties {subject:theSubject, content:theBody}
	tell newMessage
		make new to recipient at end of to recipients with properties {address:theTarget}
		set sender to "me <me@XYZ>"
	end tell
	send newMessage
end tell

But not sure how to get the different files in it. BTW these are all pictures of the desktop in .png format. I could imagine the script takes all those (like in script below). Pics are on the DT as well.

as said, I have a script to trash them at the end of the process.



set theFolder to path to desktop

tell application "Finder"
	activate
	open theFolder
	tell front Finder window to set current view to list view
	delay 2
	delete (every file whose name contains ".png")
end tell


remains getting them in the mail.

some of this script might help.

set docfolderpath to path to documents folder as string
set yourfolder to "ABC" --------- the folder with the documents
set pathtoyourfolder to docfolderpath & yourfolder & ":" as string
set FNDVAR to "X" --------your "FIND" variable for the files
set ppath to quoted form of POSIX path of pathtoyourfolder
set theSender to "your email @ yahhoo.com"
set thercpnt to "The Recipient @google.com" ---- you could have a list here and do a repeat
set theFiles to (do shell script "mdfind -onlyin " & ppath & " 'kMDItemDisplayName  == \"*" & FNDVAR & "*\"'") ------seems to be faster than finder
set theattachmentspaths to {} ------preparing list for attachment paths
repeat with x from 1 to the count of paragraphs in theFiles
	set theF to alias POSIX file (paragraph x of theFiles)
	set end of theattachmentspaths to theF
end repeat
tell application "Finder"
	set theSubject to short date string of (current date) & (" Subject ")
	set theBody to "Read these Documents carefully and get back"
	tell application "Mail"
		set newMessage to make new outgoing message with properties {address:thercpnt, subject:theSubject, content:theBody & return & return}
		tell newMessage
			set visible to true
			set sender to theSender
			make new to recipient at beginning of to recipients with properties {address:thercpnt}
			repeat with y from 1 to count of items in theattachmentspaths
				set theAttachment to item y in theattachmentspaths
				tell content
					make new attachment with properties {file name:theAttachment} at after the last paragraph
				end tell
			end repeat
		end tell
		send
	end tell
	activate
end tell

Val

Val I am on the road right now, will test later an post back.

Val I have been testing.

The .png files are on my DT.

so I set this line to

set docfolderpath to path to documents folder as string
set yourfolder to "[u]Desktop[/u]" --------- the folder with the documents
set pathtoyourfolder to docfolderpath & yourfolder & ":" as string
set FNDVAR to "[u].png[/u]" --------your "FIND" variable for the files
set ppath to quoted form of POSIX path of pathtoyourfolder

the output I get in ‘Events’ is:

tell current application
	path to documents folder as string
		--> "OS X:Users:e:Documents:"
	do shell script "mdfind -onlyin '/Users/e/Documents/OS E/Users/e/' 'kMDItemDisplayName  == \"*.png*\"'"
		--> ""
end tell

So it picks a different folder, and although the script runs fine and produces a new email (and indeed fast!) it does not enclose the .png files, and does not send the message.

I also tried setting the path (and put the .png files there) to the path I get in ‘events’ (OS X:Users:e:Documents:) no difference

thanks

Have you included

set theattachmentspaths to {} ------preparing list for attachment paths
repeat with x from 1 to the count of paragraphs in theFiles
   set theF to alias POSIX file (paragraph x of theFiles) ---- This changes POSIX path files to alias files
   set end of theattachmentspaths to theF ---- this puts each one into a new list of alias files that Mail can handle
end repeat

I use that to convert the list of POSIX path files to a list of alias files that Mail can handle.

You can do it step by step in a separate script to see that being done.

Val

Yes I did, I did copy your script verbatim and just changed the location of the file.

Don’t think so.

 set theAttachment to item y in theattachmentspaths

makes theAttachment an alias

At the moment I just can’t come up with any further solution.

I have no experience whatsoever with servers and am quite new to Applescript.

What I can vouch for is that the script above works on my single machine.

An observation I would make is that if it is choosing the wrong folder then the problem could lie with the ‘/Users/e/Documents/OS E/Users/e/’ part.

Should you have spaces in that path ? ----as in OS E

Could you set XYZ to the quoted form /Users/e/Documents/OS E/Users/e/ and then set theFiles to (do shell script “mdfind -onlyin " & XYZ & " ‘kMDItemDisplayName == "" & FNDVAR & ""’”) ------------------------------- having already set FNDVAR to “.png” ??

I can’t really answer these . I haven’t a clue about servers.

Hopefully someone with more experience will contribute to this post.

Val

Thanks Val, well you are better at it than I am so I would say a good beginner!

FYI I was testing it on my MBP and the script as you made it did not run. I know that if it runs on my MBP it runs on my MM server as it is the same OS.

Anyway, solved it.

set docfolderpath to path to desktop folder as string
set yourfolder to "~/Desktop/"--------- the folder with the documents
set pathtoyourfolder to docfolderpath & yourfolder & ":" as string
set FNDVAR to ".png" --------your "FIND" variable for the files
set ppath to quoted form of POSIX path of pathtoyourfolder
set theSender to "your email @ yahhoo.com"
set thercpnt to "The Recipient @google.com" ---- you could have a list here and do a repeat
set theFiles to (do shell script "mdfind -onlyin " & "~/Desktop/" & " 'kMDItemDisplayName == \"*" & FNDVAR & "*\"'") ------seems to be faster than finder
set theattachmentspaths to {} ------preparing list for attachment paths
repeat with x from 1 to the count of paragraphs in theFiles
	set theF to alias POSIX file (paragraph x of theFiles)
	set end of theattachmentspaths to theF
end repeat
tell application "Finder"
	set theSubject to short date string of (current date) & (" Subject ")
	set theBody to "Read these Documents carefully and get back"
	tell application "Mail"
		delay 2
		set newMessage to make new outgoing message with properties {address:thercpnt, subject:theSubject, content:theBody & return & return}
		tell newMessage
			set visible to true
			set sender to theSender
			make new to recipient at beginning of to recipients with properties {address:thercpnt}
			repeat with y from 1 to count of items in theattachmentspaths
				delay 2
				set theAttachment to item y in theattachmentspaths
				tell content
					make new attachment with properties {file name:theAttachment} at after the last paragraph
				end tell
				delay 2
			end repeat
		end tell
		delay 2
		send newMessage
	end tell
	activate
end tell

Changes I made are in the following lines:

[center]Had to put in some delays to slow it down so I could follow it better, will tweak those later.

set yourfolder to “~/Desktop/”

set pathtoyourfolder to docfolderpath & yourfolder & “:” as string

set theFiles to (do shell script “mdfind -onlyin " & “~/Desktop/” & " ‘kMDItemDisplayName == "" & FNDVAR & ""’”) ----- Had to change this as it was uploading all the files form Safari!

send newMessage (as it did not send it)[/center]

Thanks for your help!

Val,

PS does this script run on your machine?

Yes.

With my iteration.

I have a folder “ABC” in my documents folder with 2 webarchive (ie Safari) documents.
I replaced (set thercpnt to) “The Recipient @google.com” with my own email and replaced send with send newMessage as in yours. I had no delays in the script.

The script just sent these back to me via email.

In your script the lines

set docfolderpath to path to desktop folder as string
set yourfolder to "~/Desktop/"--------- the folder with the documents
set pathtoyourfolder to docfolderpath & yourfolder & ":" as string

Don’t seem quite right. Desktop Desktop as in Doctor Doctor ???

Val

In my case the folder where the files are is the users desktop.

Correct I replaced the recipient with my email address too.

I had to slow the script down (with delays) as the MM (first Core Duo processor) is to slow to open Mail and get it all done. Hence the delays of 2 seconds.

Not sure how right it is, but it works.

Yes, desktop as in the desktop of the present user.