Skip to content

Instantly share code, notes, and snippets.

@dzuelke
Created January 29, 2012 22:46
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save dzuelke/1701148 to your computer and use it in GitHub Desktop.
AppleScript to import flagged messages from Mail into The Hit List
#!/usr/bin/osascript
on run
if not (application "Mail" is running and application "The Hit List" is running) then
return
end if
tell application "Mail"
repeat with _account in imap accounts
set _inbox to _account's mailbox "INBOX"
set _messages to (a reference to (every message of _inbox whose flagged status is true))
-- We must use this workaround, because the reference will self-update once we unflag a message, and that will get us just one of two flagged messages imported
-- "Mail got an error: Can’t get item 2 of every message of mailbox..." is the error that applescript reports in this case
-- That error was previously suppressed by the try block (which is weird, because it happens in the repeat), so I've commented it out
set _msglist to contents of _messages
repeat with _message in _msglist
--try
set theSubject to "Handle @email \"" & subject of _message & "\" from " & sender of _message
set theDate to date received of _message
set hours of theDate to 0
set minutes of theDate to 0
set seconds of theDate to 0
set theBody to "message:%3c" & (message id of _message) & "%3e"
tell application "The Hit List"
tell inbox to make new task with properties {title:theSubject, notes:theBody, start date:theDate}
end tell
-- don't forget to unflag the message :)
set flagged status of _message to false
--end try
end repeat
end repeat
end tell
end run
@dzuelke
Copy link
Author

dzuelke commented Jan 29, 2012

And then have it run every ten minutes, just add it via crontab -e:

localhost:~ dzuelke$ crontab -l
*/10    *   *   *   *   /usr/local/bin/flagged_mails_to_thl.applescript >> /dev/null

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment