Friday, January 9, 2015

Create new Todoist task from Mac Outlook

It can be very challenging if you are in the midst of dozens of emails a day to even get near a zero-inbox.  As of this writing, I am over 300 messages sitting in my inbox.  I have a relatively simple AppleScript which I have pieced together from a few different sources.

First, copy the script (below) to your local machine into your user folder ~/Library/Application Support/Microsoft/Office/Outlook Script Menu Items.

You can navigate to that path in a new Finder window by holding the Option key and opening the Go menu.  You should see the Library option appear there.  Now, just follow the path to the location mentioned above.

After you create  the file there, create the file todoist-token.txt. In this file, you'll want to copy the API token from your account in Todoist.  Open your browser, navigate and login to Todoist.  Once you see your task lists, click the wheel and navigate to Settings.  Once there, select Account.  In the bottom half to the window, you'll see API token.  Copy that token and place it in the file, todoist-token.txt.  Make sure there are no new lines below the token.  This will cause an error when parsing that file.

Once you have completed these two steps, it should be possible to open Outlook, select a message and send it to Todoist.  Click the AppleScript icon in the menu bar and locate Todoist Task.  Clicking this will automatically send it to Todoist and move the message in the folder "Archive".

Features of this script:

  • Create a new task in Todoist.
  • Create a new note on the Todoist with the body converted to plain text.
    • NOTE: Todoist does not support HTML notes at this time.
  • Archive the message to your "Archive" folder.
Also, view this script on github.  For further enhancements and fixes - https://github.com/mhorner/OutlookToTodoist




(*
Create Todoist task from email

*)

on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars

-- get todoist token from text file
set todoistToken to my replace_chars(read file ((path to home folder as text) & "Library:Application Support:Microsoft:Office:Outlook Script Menu Items:todoist-token.txt"), "
", "")

tell application "Microsoft Outlook"
-- get the currently selected message or messages
set selectedMessages to current messages
-- if there are no messages selected, warn the user and then quit
if selectedMessages is {} then
display dialog "Please select a message first and then run this script." with icon 1
return
end if
repeat with theMessage in selectedMessages
-- get the information from the message, and store it in variables
set theName to subject of theMessage
set theSender to sender of theMessage
set thePriority to priority of theMessage
set theBody to content of theMessage
if thePriority is priority high then
set thePriority to "4"
else
set thePriority to "0"
end if
-- remove new lines from the body first
set theBody to do shell script "echo '" & theBody & "'| tr -d '\\r'"
-- remove HTML tags from the body creating a plain text output
set plainText to do shell script "echo '" & theBody & "' | /usr/bin/textutil -stdin -convert txt -stdout"
-- create a new task with the information from the message
set newItem to do shell script "curl -X POST -d 'content=" & theName & " for " & name of theSender & "' -d 'token=" & todoistToken & "'  -d 'priority=" & thePriority & "' -d 'date_string=today' https://todoist.com/API/additem -d 'note=" & plainText & "'"
set theAccount to account of theMessage
set archiveFolder to folder "Archive" of theAccount
move theMessage to archiveFolder
end repeat
end tell