AppleScript to get contents of file in Apple Mail I am attempting to make an auto replier bot. The bot will respond differently (and perform different actions) based on the message that is received. Instead of emailing the bot, I will be texting the bot (with an iPhone). My provider (Verizon) receives the message and sends it as an attachment, in a file named text_0.txt, as plaintext.

Unfortunately, I don't know how to get AppleScript to get the contents of the file.

So my question is as follows, how can I get AppleScript to (writing this non apple script code to make it easier to understand) do this:

if (email contains type["file"]) {

if (email.file[0].name() === "text_0.txt") {

contents = email.file[0].content;

// Do stuff

} else {
// Do some other stuff
}

} else {
content = email.content;
}
I'm a little confused as to exactly what you're trying to achieve.  This is not an exact solution to your question, but maybe this following code will help put you on the right path.  

The first part of this following code with the "Mail" commands will take the currently selected message in Mail.app, and save all of the email attachments in that message to your desktop.



The second part of this following code, with the "Finder" commands, will read the contents of any .txt files in the list of files stored in savedFileList and save that information to the variable theContent



set saveFilePath to POSIX path of (path to desktop)
set savedFileList to {}

tell application "Mail"
    set theCount to every mail attachment of item 1 of (get selection)
    repeat with i from 1 to count of theCount
        try -- handles if "downloaded of (get mail attachment id theID of item 1 of (get selection))" is false
            set thisItem to item i of theCount
            set theID to id of thisItem
            set theFile to mail attachment id theID of item 1 of (get selection)
            set isDownloaded to downloaded of (get mail attachment id theID of item 1 of (get selection))
            set theName to name of theFile
            save theFile in POSIX file (saveFilePath &amp; theName)
            set end of savedFileList to (saveFilePath &amp; theName) as POSIX file as alias
        end try
    end repeat
end tell

set nameExtensions to {"txt"}

tell application "Finder"
    repeat with j from 1 to count of savedFileList
        set thisItem2 to item j of savedFileList
        if name extension of thisItem2 is in nameExtensions then
            set theContent to read thisItem2
        end if
    end repeat
end tell




At this point you should be able to send actual files, or content from txt files you have received from the currently selected message in "Mail.app". (As all of the information has been stored in different variables in the code... For you to choose and act upon)