Question

trying to send several mails with specific attachments for each address. every address has its own subfolder for attachments. the "grab attachments part" does not work and I am not sure if the handler is set up right: should I pass the subfolder to mail inside the handler or keep it as I have it. This is my first long script so please don't be too harsh ;-)

I'm thinking that i get closer to the working solution, I still don't get it to function. here is my script so far:

`  with timeout of 600 seconds

-- Liste: Alle Empfänger  

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


-- Liste fuer die Übergabe alphabetisch sortieren 

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

-- Liste: Alle Subfolder 

tell application "Finder"
    set mainfolder to choose folder "select a folder"
    set folderList to {}
    set myFolders to every folder of mainfolder
    repeat with attachFolder from 1 to (count of myFolders)
        set end of folderList to attachFolder as string
    end repeat
end tell

-- Sicherheits-Check

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

 --handler processfolder(myFiles)

 on processfolder(myFiles)
tell application "Mail"
    activate
    set theAddress to (item i of emailList)
    set theMex to (make new outgoing message at end of outgoing messages with   properties {visible:true, subject:"Subjectheader",   content:"email body"})

    tell content of theMex
        make new attachment with properties {file name:FileList} 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 tell
end processfolder

-- grab attachments and send mail   

tell application "Finder"
repeat with myFolder from 1 to (count of folderList)
    set FileList to {}
    set myFiles to 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
display dialog (count1 as string) & " Nachrichten verschickt."

end`

i believe the handler should work alright. Matching the subfolder list with the address list still seems to be a problem, 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?

thanks for being helpful! i really appreciate this! marco

Était-ce utile?

La solution

You must pass the variables as parameters in the handler :

1- (item i of emailList) : i and emailList is not defined in the handler.

2- {file name:FileList} : FileList is not defined in the handler, file name must be a path of type alias or string, not a list of path.

set myFiles to entire contents of myFolder : the myfolder variable is an integer, entire contents will contains the folders and files, if the folder doesn't contains subfolders, entire contents is useless, use files of xFolder.

The rest is okay, but contains unnecessary lines.

Here is the script:

with timeout of 600 seconds
    -- Liste: Alle Empfänger  

    tell application "Contacts"
        set emailList to value of email 1 of every person of group "Test"
    end tell

    -- Liste fuer die Übergabe alphabetisch sortieren 

    set otid to AppleScript's text item delimiters
    set AppleScript's text item delimiters to {linefeed}
    do shell script "echo " & (quoted form of (emailList as string)) & " | sort -f"
    set emailList to (paragraphs of the result)
    set AppleScript's text item delimiters to otid


    -- Liste: Alle Subfolder 

    activate
    set mainfolder to choose folder "select a folder"
    tell application "Finder" to set folderList to folders of mainfolder


    -- Sicherheits-Check

    set count1 to count folderList
    if count1 is not equal to (count emailList) then
        display dialog "Houston, we have a problem:" & return & "Die beiden Listen sind nicht gleich lang..." buttons {"ok"} cancel button "ok" with icon 2
    end if
end timeout

-- grab attachments and send mail   

repeat with i from 1 to count1
    try
        tell application "Finder" to set myFiles to (files of entire contents of (item i of folderList)) as alias list
        my processfolder(myFiles, item i of emailList)
    end try -- no error on empty folder
end repeat
display dialog (count1 as string) & " Nachrichten verschickt."


on processfolder(tFiles, theAddress)
    tell application "Mail"
        activate
        tell (make new outgoing message at end of outgoing messages with properties {visible:true, subject:"Subjectheader", content:("email body" & linefeed & " ")})
            make new to recipient at end of to recipients with properties {address:theAddress}
            tell content to repeat with tFile in tFiles
                make new attachment with properties {file name:tFile} at after last paragraph
                make new character with data linefeed at after last paragraph
            end repeat
            send
        end tell
    end tell
end processfolder

Autres conseils

it is done! Thanks to you and one or two other pros I now have a beautiful bulk mailing script routine using automator, a bash line and (mainly) applescript. I use it for job applications but you can use it for any case where you want individualised bulk emailing with Mail, MS Word and any given list of contacts in Excel (or Address Book for that matter). For the sake of being complete I will add all necessary steps. with any given list of x names, email addresses, personal addresses you can generate x subfolders, containing x personalized letters and not-personalized documents (thanks, Jack! adding the docs works perfectly). once you start the last script and select the folder you can watch mail sending them all out, addressing the person by name and attaching the right personalized letter! It corrects for foreign name spelling that is rendered differently in the email address. It works best for email addresses using the last name before the "@" and can now ignore the first name if it is set in front of the last name (i.e. firstname.lastname@company.com). Thank you all very much for the assistance! this was great team effort. I shall post it as soon as I am home, should I post it up here and in the other related question or is there a sharing forum?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top