Search code examples
applescriptemail-attachmentsapple-mail

can i add several attachments from subfolders to Mail?


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


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