2

I'm trying to create an auto-responder that will respond to any email from a certain address with something like:

Thank you for your email.
Your number is #0000042.

The number should increment every time it sends one of these emails.

After looking online at the expensive solutions it looks like my best bet is automating something with AppleScript - possibly saving the last number in a file and incrementing it.

What would such a script look like?

  • What do you have so far? Are you looking for an entire script or just incrementing the "Your number is #0000042." part of the script? – user3439894 Nov 2 '17 at 13:39
  • @user3439894 An entire script, or at least the basics. – Theo C Nov 2 '17 at 14:08
  • I've expanded my answer with a complete script. – MikeLimaOscar Nov 2 '17 at 17:30
1

A simple way of implementing the auto incrementing would be to use an AppleScript property variable:

property responseNumber : 42

Property values are "remembered" between calls to your script. So, in your handler simply use:

set responseNumber to responseNumber + 1

However, the property value is reset whenever the AppleScript is compiled. So you would need to manually change the 1 in property responseNumber : 1 to the latest value when you changed the script. Using a file is therefore a more robust method and using a preference file to record the current property value means you can use built-in functionality.

A basic AppleScript example (with no error checks nor testing, since I don't use Mail), to give you an idea:

property responseNumber : 42
property prefFileName : "your.domain.in.reverse.emailresponder.plist"

on perform_mail_action(theData)
    my readPrefs()
    tell application "Mail"
        set theSelectedMessages to |SelectedMessages| of theData
        repeat with theMessage in theSelectedMessages
            set theReply to reply theMessage
            set the content of theReply to "Thank you for your email." & return & "Your number is #" & (zeroPad of me given value:responseNumber, minimumDigits:7) & "." & return
            send theReply
            set responseNumber to responseNumber + 1
        end repeat
    end tell
    my writePrefs()
end perform_mail_action

on zeroPad given value:n, minimumDigits:m : 2
    set val to "" & (n as integer)
    repeat while length of val < m
        set val to "0" & val
    end repeat
    return val
end zeroPad

on readPrefs()
    -- Get the path to the property list
    set plPath to (path to preferences folder as text) & prefFileName
    tell application "System Events"
        set plContents to contents of property list file plPath
        set responseNumber to value of property list item "ResponseNumber" of plContents
    end tell
end readPrefs

on writePrefs()
    -- Get the path to the property list
    set plPath to (path to preferences folder as text) & prefFileName
    tell application "System Events"
        set the value of property list item "ResponseNumber" of contents of property list file plPath to responseNumber
    end tell
end writePrefs

Save this script in your ~/Library/Application Scripts/com.apple.mail folder and set-up a Mail rule to call it.

You'll also need to create the appropriate plist file in your ~/Library/Preferences folder with the following contents:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>ResponseNumber</key>
    <integer>42</integer>
</dict>
</plist>

You must log in to answer this question.

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