attaching all the files within a folder

Can anyone help me attach all the files that exist in a given folder? The below script works fine for one file, but it does not work when there is multiple files. I am sure a repeat loop is required, but I am not sure where it goes and how to include it for attachments. (I have searched for this, but to no avail.)

-Jeff

tell application "Finder"
	set targetFiles to every file of alias "Illustrations:Users:admin:Desktop:PDF:"
	set theAttachment to targetFiles as alias
	
end tell

tell application "Mail"
	activate
	set this_message to make new outgoing message with properties {visible:true, subject:"Test Subject", content:"Test Content"}
	tell this_message
		set attachedFiles to targetFiles
		
		tell content
			make new attachment with properties {file name:theAttachment} at after the last paragraph
			
		end tell
		activate
	end tell
end tell

Hi Jeff,

Mail.app doesn’t accept attachments as list.
You must create each attachment separately

tell application "Finder"
	set targetFiles to every file of alias "Illustrations:Users:admin:Desktop:PDF:"
end tell

tell application "Mail"
	activate
	set this_message to make new outgoing message with properties {visible:true, subject:"Test Subject", content:"Test Content"}
	tell this_message
		tell content
			repeat with i in targetFiles
				make new attachment with properties {file name:i as alias} at after the last paragraph
			end repeat
		end tell
		activate
	end tell
end tell

Darn,
I was so close, to the point that I was about to delete my post in fear of sounding like a buffoon (once again).

Stefan ” thank you very very much!

-Jeff

Don’t worry, Jeff,
AppleScript is a great language but has (like many languages) a couple of blasted irregular verbs :wink:

More than a couple, Stefan (and a nice metaphor, by the way) :lol: As a young man, my favorite school language was Latin - it only has 8 irregular verbs in common use. French has a lot, but English probably takes the prize – AppleScript is a piker by comparison.

Thanks that helps my esteem a bit :slight_smile:

I have another aesthetic question that is far from important, and not a show stopper by any means. So please disregard this question if you don’t have time.

I went ahead and made my email subject and part of the body text = the variable that represents the names of the files. Since there may be more than one file, the subject will appear as one long string without a space of each filename. Is there anyway to account for this and add a space or two that separates the filenames?

-Jeff

tell application "Finder"
	try
		set targetFiles to every file of alias "Illustrations:Users:admin:Desktop:PDF:"
		set targetFileNames to name of every file of alias "Illustrations:Users:admin:Desktop:PDF:" as Unicode text
	end try
end tell

tell application "Mail"
	activate
	set this_message to make new outgoing message with properties {visible:true, subject:targetFileNames, content:"There were questions about the attached to file(s):  " & targetFileNames}
	tell this_message
		tell content
			repeat with i in targetFiles
				make new attachment with properties {file name:i as alias} at after the last paragraph
			end repeat
		end tell
		activate
	end tell
end tell

Hi Jeff,

try this:

tell application "Finder"
	-- try
		tell every file of alias "Illustrations:Users:admin:Desktop:PDF" to set {targetFiles, targetFileNames} to {it, name of it}
		set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ", "}
		set targetFileNames to targetFileNames as Unicode text
		set AppleScript's text item delimiters to ASTID
	-- end try
end tell
...

Notes: ¢ The try block isn’t needed unless you want to trap the error, if the PDF folder doesn’t exist.
¢ If “Illustrations” is the startup volume, you can use only

tell application "Finder" to tell every file of folder "PDF" to set {targetFiles, targetFileNames} to {it, name of it}
set {TID, text item delimiters} to {text item delimiters, ", "}
set targetFileNames to targetFileNames as Unicode text
set text item delimiters to TID

because the Finder’s root folder is the current desktop folder

StefanK,
I really can’t thank you enough for your help on this particular script, which will ultimately transcend into helping in so many other future scripts. I would have never have figured this stuff out, even with the aid of some crappy books on my desk.

-Jeff