on notify_user(subject, sender, type)
tell application "GrowlHelperApp"
notify with name ¬
"ActionMail" title ¬
"Moving " & type description ¬
subject & " from " & sender application name "Growl ActionMail"
end tell
end notify_user
on notify_intent(account, size, type)
tell application "GrowlHelperApp"
notify with name ¬
"ActionMail" title ¬
"Intent for " & account description ¬
"Move " & size & " messages of type " & type application name "Growl ActionMail"
end tell
end notify_intent
on identify_inbox(theAccount, theInbox)
tell application "Mail"
tell account theAccount
mailbox theInbox
end tell
end tell
end identify_inbox
on identify_target_mailbox(theAccount, theTarget)
tell application "Mail"
if theAccount is "QUT" then
tell account theAccount
tell mailbox "Inbox"
return mailbox theTarget
end tell
end tell
end if
if theAccount is "MobileMe" then
tell account theAccount
return mailbox theTarget
end tell
end if
end tell
end identify_target_mailbox
on taskify_messages(theSelectedMessages)
tell application "Mail"
repeat with selected_message in theSelectedMessages
set message_id to the message id of the selected_message
set theSender to the sender of the selected_message
set theSubject to the subject of the selected_message
set fromBit to theSender & return
set message_url to "message://%3C" & message_id & "%3E"
set theBody to fromBit & return & the content of the selected_message
set theAttachments to every mail attachment of the selected_message
tell application "OmniFocus"
tell front document
set newTask to make new inbox task with properties {name:theSubject, note:theBody}
tell newTask
tell note
insert "Original Message" & return & return at before first character
set value of attribute "link" of style of paragraph 1 to message_url
repeat with theAtt in theAttachments
set theName to the name of theAtt
set myFilePath to "Macintosh HD:Users:simon:Desktop:" & theName
save theAtt in myFilePath
make new file attachment with properties {file name:myFilePath, embedded:true}
tell application "Finder"
delete file myFilePath
end tell
end repeat
end tell
end tell
end tell
end tell
my notify_user(theSubject, theSender, "Sent to OmniFocus")
end repeat
end tell
end taskify_messages
on move_messages(theAccount, moveList, sourceMailbox, targetMailbox, moveType)
tell application "Mail"
my notify_intent(theAccount, count of moveList, moveType)
if (count of moveList) is greater than 0 then
repeat with theMessage in moveList
tell account theAccount
if mailbox of theMessage is sourceMailbox then
set read status of theMessage to true
move theMessage to targetMailbox
set theSubject to the subject of theMessage
set theSender to the sender of theMessage
my notify_user(theSubject, theSender, moveType)
delay 1
end if
end tell
end repeat
delay 5
end if
end tell
end move_messages
on appIsRunning(appName)
tell application "System Events" to (name of processes) contains appName
end appIsRunning
on run
tell application "GrowlHelperApp"
set the allNotificationsList to {"ActionMail"}
register as application ¬
"Growl ActionMail" all notifications allNotificationsList ¬
default notifications allNotificationsList ¬
icon of application "Mail"
end tell
tell application "OmniFocus"
activate
delay 10
end tell
tell application "Mail"
if not my appIsRunning("Mail") then
tell application "Mail"
check for new mail
tell application "Mail"
repeat until background activity count = 0
delay 1
end repeat
end tell
end tell
end if
delay 2
with timeout of 90000 seconds
set myActionsBox to my identify_target_mailbox("MobileMe", "Action")
set mobilemeArchiveBox to my identify_target_mailbox("MobileMe", "Archive")
tell myActionsBox
set theMessages to every message
end tell
my taskify_messages(theMessages)
-- uncomment next line to actually move actioned messages to archive
my move_messages("MobileMe", theMessages, myActionsBox, mobilemeArchiveBox, "Moved to MobileMe")
delay 60
tell application "OmniFocus"
synchronize
end tell
end timeout
end tell
end run
tell application "GrowlHelperApp"
notify with name ¬
"ActionMail" title ¬
"Moving " & type description ¬
subject & " from " & sender application name "Growl ActionMail"
end tell
end notify_user
on notify_intent(account, size, type)
tell application "GrowlHelperApp"
notify with name ¬
"ActionMail" title ¬
"Intent for " & account description ¬
"Move " & size & " messages of type " & type application name "Growl ActionMail"
end tell
end notify_intent
on identify_inbox(theAccount, theInbox)
tell application "Mail"
tell account theAccount
mailbox theInbox
end tell
end tell
end identify_inbox
on identify_target_mailbox(theAccount, theTarget)
tell application "Mail"
if theAccount is "QUT" then
tell account theAccount
tell mailbox "Inbox"
return mailbox theTarget
end tell
end tell
end if
if theAccount is "MobileMe" then
tell account theAccount
return mailbox theTarget
end tell
end if
end tell
end identify_target_mailbox
on taskify_messages(theSelectedMessages)
tell application "Mail"
repeat with selected_message in theSelectedMessages
set message_id to the message id of the selected_message
set theSender to the sender of the selected_message
set theSubject to the subject of the selected_message
set fromBit to theSender & return
set message_url to "message://%3C" & message_id & "%3E"
set theBody to fromBit & return & the content of the selected_message
set theAttachments to every mail attachment of the selected_message
tell application "OmniFocus"
tell front document
set newTask to make new inbox task with properties {name:theSubject, note:theBody}
tell newTask
tell note
insert "Original Message" & return & return at before first character
set value of attribute "link" of style of paragraph 1 to message_url
repeat with theAtt in theAttachments
set theName to the name of theAtt
set myFilePath to "Macintosh HD:Users:simon:Desktop:" & theName
save theAtt in myFilePath
make new file attachment with properties {file name:myFilePath, embedded:true}
tell application "Finder"
delete file myFilePath
end tell
end repeat
end tell
end tell
end tell
end tell
my notify_user(theSubject, theSender, "Sent to OmniFocus")
end repeat
end tell
end taskify_messages
on move_messages(theAccount, moveList, sourceMailbox, targetMailbox, moveType)
tell application "Mail"
my notify_intent(theAccount, count of moveList, moveType)
if (count of moveList) is greater than 0 then
repeat with theMessage in moveList
tell account theAccount
if mailbox of theMessage is sourceMailbox then
set read status of theMessage to true
move theMessage to targetMailbox
set theSubject to the subject of theMessage
set theSender to the sender of theMessage
my notify_user(theSubject, theSender, moveType)
delay 1
end if
end tell
end repeat
delay 5
end if
end tell
end move_messages
on appIsRunning(appName)
tell application "System Events" to (name of processes) contains appName
end appIsRunning
on run
tell application "GrowlHelperApp"
set the allNotificationsList to {"ActionMail"}
register as application ¬
"Growl ActionMail" all notifications allNotificationsList ¬
default notifications allNotificationsList ¬
icon of application "Mail"
end tell
tell application "OmniFocus"
activate
delay 10
end tell
tell application "Mail"
if not my appIsRunning("Mail") then
tell application "Mail"
check for new mail
tell application "Mail"
repeat until background activity count = 0
delay 1
end repeat
end tell
end tell
end if
delay 2
with timeout of 90000 seconds
set myActionsBox to my identify_target_mailbox("MobileMe", "Action")
set mobilemeArchiveBox to my identify_target_mailbox("MobileMe", "Archive")
tell myActionsBox
set theMessages to every message
end tell
my taskify_messages(theMessages)
-- uncomment next line to actually move actioned messages to archive
my move_messages("MobileMe", theMessages, myActionsBox, mobilemeArchiveBox, "Moved to MobileMe")
delay 60
tell application "OmniFocus"
synchronize
end tell
end timeout
end tell
end run