Search code examples
applescriptapple-mail

Sending Email to CSV File with AppleScript and Apple Mail


I am trying to run an AppleScript that sends emails through Apple Mail. It appears to be working properly, however I am seeing an error when I run the script.

This is the error I am seeing:

error "Can’t get text item 1 of \"\"." number -1728 from text item 1 of ""

This is my script:

set csv to read "/Users/Username/Documents/file.csv" as «class utf8»
set text item delimiters to ","
repeat with l in paragraphs of csv
    tell application "Mail"
        tell (make new outgoing message)
            set subject to "subject"
            set content to "content"
            make new to recipient at end of to recipients with properties {address:text 
            item 1 of l}
            send
        end tell
    end tell
end repeat

This is my CSV:

email1@gmail.com
email2@gmail.com

Any ideas on what I'm doing wrong? Thank you!


Solution

  • The content of text item 1 of l is literally just text item 1 of and then whatever the text is—that is, it hasn’t been dereferenced to a simple string. Most of the time this is fine, but some actions won’t accept text unless it’s been dereferenced. In this case, you can use text item 1 of l as string, i.e.:

    make new to recipient at end of to recipients with properties {address:text item 1 of l as string}
    

    Note that in your example above, the more simpler form of just l also works:

    make new to recipient at end of to recipients with properties {address:l}
    

    but I’m assuming this was a simplified example and your real use case involves extracting text from the CSV file.