multiple mail attachments automated with script, mail and adress book

hi all,

I have this situation and really need help!

-an Adress Book group “Adresses” with 99 adresses, one entry field carries the appropriate adress i.e. ‘Dear Mr. Miller’ and ‘Dear Ms. Soso’

-A Folder with 99 subfolders containing 4 .pdf’s each, one always different, three the same -

-I need Mail to send out the emails with each adress receiving the 4 pdf’s form the corresponding subfolder, with the standardized subjectline and text, BUT with a specified Adress i.e. “Dear Mr. Miller…”

-I made a code that matches the right folders to the right adresses but only manages to upload one file as attachment when automatically creating an email with Mail, not all four. below you see my efforts to bring in all four files:

PHP-Code:

with timeout of 600 seconds --  

-- List 1: All attachments 

tell application "Finder"
    set mailing to selection
    set myFolders to every folder of (mailing as alias list)
    set myFiles to every item of myFolders i
end tell

-- List 2: All recipients  

tell application "Contacts"
    set emailList to {}
    set testPersons to every person of group "Adresses"
    repeat with thisTestPerson in testPersons
        set end of emailList to (value of email of thisTestPerson) as string
    end repeat
end tell

-- List for matching: puts adresses in alphabetical order 

set the_list to emailList
set otid to AppleScript's text item delimiters
set AppleScript's text item delimiters to {ASCII character 10} -- always a linefeed 
set list_string to (the_list as string)
set new_string to do shell script "echo " & quoted form of list_string & " | sort -f"
set new_list to (paragraphs of new_string)
set AppleScript's text item delimiters to otid

 -- Double-Check: lengths of the two lists  

set count1 to count of myFolders
set count2 to count of new_list
if count1 is not equal to count2 then
    display dialog "There is a problem:" & return & "Die beiden Listen sind nicht gleich lang..." buttons {"Shit"} with icon 2
    return
end if

-- creates and sends mass mailing: 

tell application "Mail"
    activate
    repeat with i from 1 to count1
        set theFiles to every file of (item i of myFolders) as alias
        set theAddress to (item i of emailList)
        set theMex to (make new outgoing message at end of outgoing messages with properties {visible:true, subject:"mailing", content:"Bitte entnehmen Sie dem Anhang ....."})
        tell content of theMex
            make new attachment with properties {file name:theFiles} at after last paragraph
        end tell
        tell theMex
            make new to recipient at end of to recipients with properties {address:theAddress}
        end tell
        send theMex
    end repeat
end tell

display dialog (count1 as string) & " Nachrichten verschickt."
end timeout

so, what I need now for this code to do is: 1) have the right form of adress (Dear Mr. Miller) in each email body 2) manage to get all 4 pdf’s attached to the mails

so I guess the first and the last part of the code need some working on, but I have not found out how to organize the subfolders as reference list for the adresses and get into the subfolders for picking out the right attachments. I also do not know how to implement the specific field I created in adressbook as a proper form of adress into the body.

thanks yo much for every little hint!

Hi all,

i think the long code might have put some people off, I actually need help only with the first part:

-- List 1: All attachments 

tell application "Finder"
set mailing to selection
set myFolders to every folder of (mailing as alias list)
set myFiles to every item of myFolders i
end tell 

This part needs to oben the selected folder make a list of subfolders and supply their content to the matching mailing adress -this bit obviously does not work and I am still desperate for some help!
thanks a million

Hi,

every [element] of in the Finder works only with a single object, not with a list.
So you need two repeat loops to walk through the hierarchy
for example


tell application "Finder"
	set mailing to selection
	set myFiles to {}
	repeat with selectedFolder in mailing
		repeat with aFolder in (get folders of selectedFolder)
			set myFiles to every item of aFolder
			my processFiles(myFiles) -- sample handler
		end repeat
	end repeat
end tell

thanks a lot stefan, this sounds like it is the solution, but just tried it and got an error with regards to

my processfiles(myFiles) 

saying that the document pdf cannot be converted into the expected type… what does this mean?

this handler call is just an example.
Of course you have to implement the handler in your script.

Actually “myFiles” is used later in the script, so you have to pass the subfolder list of each selected folder


tell application "Finder"
	set mailing to selection
	repeat with selectedFolder in mailing
		set theFolders to every folder of selectedFolder
		my processFoder(theFolders) -- sample handler
	end repeat
end tell

on processFoder(myFolders)
	
	-- List 2: All recipients 
	
	-- creates and sends mass mailing: 
	tell application "Mail"
		-- .
	end tell
	
end processFoder

hi stefan, thanks for your assistance!

@ all:

stefans advice kept me busy for a few days, but although I’m thinking that i get closer to the working solution, I still don’t get it to function. here is the part of the script that I have problems wit far:


	

-- grab attachments and send mail	

tell application "Finder"
	repeat with myFolder from 1 to (count of folderList)
		set FileList to {}
		set myFiles to every file of (entire contents of myFolder)
		repeat with thisFile in myFiles
			set end of FileList to thisFile as string
		end repeat
		my processfolder(myFiles)
	end repeat
end tell

I am not sure if my repeat loop “grab attachment und send mail” does the trick. It is a tricky use of repeat loops and I am still struggling with it. any quick thoughts about what I am still doing wrong? I get an error message that “entire contents of 1 cannot be read.”

thanks for being so helpful! i really appreciate this!
marco