Connect Todoist with Postbox via Applescript
Tags: productivity
Published: 2020-01-06 23:15:40

I use the GTD workflow for my task management, and until recently have been using Omnifocus on the Mac. I had Omnifocus set up with custom scripts to generate reports and receive events from other applications. It's not really built to be scriptable and doesn't provide easy access to the underlying data. It also only works well on my desktop.

Due to these shortcomings, I decided to take the plunge and try [Todoist](https://todoist.com/r/javin_lacson_bsbfug). The main reason for this decision is the ability to leverage the API. One of the things I liked about Omnifocus is that can be scripted even if it's not that easy. [Todoist](https://todoist.com/r/javin_lacson_bsbfug), with the API, provides similar capabilities in a much more straightforward fashion. However, there was one missing feature - a seamless link to add Postbox messages to [Todoist](https://todoist.com/r/javin_lacson_bsbfug). 

In the paid version of [Todoist](https://todoist.com/r/javin_lacson_bsbfug), you can get a unique email address to forward your emails as todo items. This doesn't really work with Inbox Zero and GTD, as if you add an email todo to reply later, you'd then have to go search for that email. It creates quite a bit of friction. To eliminate that, Postbox provides a link that will open the email directly in Postbox. The idea is that when you are going through your todo list processing emails saved for later, you can click the todo and end up right in the message. Meanwhile, you can archive the message and have Inbox Zero knowing that there's an item linking back to it in your todo list. 

### What do we want

To that end, I'd like to have a hotkey in Postbox to do this when I'm reading a message that generates a todo. The hotkey would copy the message subject and link back to it in my todo list. After that, it would automatically archive the message, clearing it from my inbox. All in all, this is possible with a few quick lines of applescript and a few hours of fighting with Mojave sandbox security and permissions. 

### Automate The Things

First, get the [todoist CLI](https://github.com/sachaos/todoist) and set that up. I alias this to a shell command "td".

Create a bash script to add the todo. Name this add.sh and make it executable. The script takes 2 arguments - the subject of the message and the link. 

```bash
#!/bin/bash
/usr/local/bin/td add "[$1]($2)" &
echo Todo scheduled for addition
```

Open automator and create a new quick action. Use the applescript automation. 

```applescript
on run {input, parameters}

    tell application "Postbox"
	    set emailMessage to item 1 of (get selection)
	    set messageURL to URL of emailMessage
	    set messageSubject to subject of emailMessage
    end tell
	
    tell application "Postbox" to activate
    tell application "System Events"
	  keystroke "a"
    end tell

    do shell script "/path/to/add.sh " & "\"" & messageSubject & "\"" & " " & "\"" & messageURL & "\""

	
	return input
end run

```

Replace the path to your add.sh script. Save this and recall the name, I used AddTodoFromMessageAction. 

### Hotkeys and Permissions

Set up a hotkey. Go to Apple -> Settings -> Keyboard -> Shortcuts. Select Services and under General you should see your automation. Select this and set up a hotkey shortcut. I used command + D. You will need to pick a shortcut that is not used by Postbox. 

Finally, set up the permissions. Go to Apple -> Settings -> Security and Privacy -> Privacy -> Accessibility. Add Automator and Postbox from applications and add System Events from HD -> System -> Core Services. 

Test in postbox with your shortcut. Accept the security dialogs to allow the automation, it will only ask once. 

This was written for Mojave 10.14.6. If it doesn't work, test the components up the stack in this order: Todoist CLI, Bash script, Applescript stand alone, then Automator script.