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 & theName)
            set end of savedFileList to (saveFilePath & 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)

  • Looks good. Will adapt to what I need and edit your answer, test, and mark as solved. Tomorrow. | You will most likely understand the idea once I edit. – JBis Jul 26 '18 at 1:45
  • @JBis were you able to use any of the code I posted for you? Just curious if it set you on the right path. – wch1zpink Jul 27 '18 at 3:45
  • Sorry. Been busy with other things will test tomorrow. If possible I could use your help (maybe in chat) to finish the project. The code you provided was probably the most difficult part. – JBis Jul 27 '18 at 4:02
  • Okay I'm in Chat now waiting for you chat.stackexchange.com/rooms/38/ask-different-chat – wch1zpink Jul 27 '18 at 4:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .