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>