I have a script that sends an autoreply if triggered by a Mail rule. I want to send the autoreply 2 hours after I receive the message, so I put in a long “delay 7200”. The problem is that the delay basically hangs Mail for 2 hours – nothing else gets done. Is there a better way to do this? Script below.
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
repeat with eachMessage in theMessages
set theNewName to name of first recipient of eachMessage
set theNewAddress to address of first recipient of eachMessage
set theName to extract name from theNewName
set theAddress to extract name from theNewAddress
set newMessage to make new outgoing message
tell newMessage
make new to recipient at end of to recipients with properties {name:theName, address:theAddress}
set visible to false
set subject to "Re: " & subject of eachMessage
set content to "**message content**"
end tell
delay 7200
send newMessage
tell eachMessage
set was replied to to true
end tell
end repeat
end tell
end perform mail action with messages
end using terms from
well, you could store the messages and run another script every 1-5min via crontab or launchdemon
so this would be the mail-script:
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
repeat with eachMessage in theMessages
write_reply_cronjob(eachMessage)
end repeat
end tell
end perform mail action with messages
end using terms from
on write_reply_cronjob(themessage)
tell application "Mail"
set themessage to themessage as specifier
set theid to id of themessage
set thebox to mailbox of themessage
set theaccount to name of account of thebox
set thebox to name of thebox
set timerecieved to do shell script "perl -e 'print time.\"\\n\";'"
set theline to timerecieved & tab & theid & tab & (theaccount as rich text) & tab & (thebox as rich text)
do shell script "echo " & quoted form of theline & ">>~/Desktop/bla.txt"
end tell
end write_reply_cronjob
and this here on your cronjob:
parse_cronfile("~/Desktop/bla.txt", 7200)
on getmesg(theid, thebox, theaccount)
tell application "Mail" to return item 1 of (get every message of mailbox thebox of account theaccount whose id is theid)
end getmesg
on send_reply(eachMessage)
tell application "Mail"
set theNewName to name of first recipient of eachMessage
set theNewAddress to address of first recipient of eachMessage
set theName to extract name from theNewName
set theAddress to extract name from theNewAddress
set newMessage to make new outgoing message
tell newMessage
make new to recipient at end of to recipients with properties {name:theName, address:theAddress}
set visible to true
set subject to "Re: " & subject of eachMessage
set content to "**message content**"
end tell
-- send newMessage
end tell
end send_reply
on parse_cronfile(cronfile, runafterseconds)
set crons to every paragraph of (do shell script "cat " & cronfile)
set current_time to do shell script "perl -e 'print time.\"\\n\";'"
set newcron to {}
repeat with cron in crons
set AppleScript's text item delimiters to tab
set cron_data to every text item of cron
set AppleScript's text item delimiters to ""
get cron_data
set timediff to do shell script "echo " & current_time & "-" & item 1 of cron_data & "|bc"
if timediff as integer > runafterseconds then
set themessage to my getmesg((item 2 of cron_data) as integer, (item 4 of cron_data), item 3 of cron_data)
my send_reply(themessage)
else
set AppleScript's text item delimiters to tab
set cron_data to cron_data as text
set AppleScript's text item delimiters to ""
set newcron to newcron & {cron_data}
end if
set AppleScript's text item delimiters to "
"
set newcron to newcron as text
set AppleScript's text item delimiters to ""
do shell script "echo " & quoted form of newcron > cronfile
end repeat
end parse_cronfile