Skip to content

Instantly share code, notes, and snippets.

@craigeley
Created June 18, 2017 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craigeley/05d4bd0c07fbe2bf96b6296dc7cb4b84 to your computer and use it in GitHub Desktop.
Save craigeley/05d4bd0c07fbe2bf96b6296dc7cb4b84 to your computer and use it in GitHub Desktop.
Sync tasks assigned to you in an Asana workspace to an area in Things 3. Define variables for lines 20 and 21 in a "data.yml" file. Fill in your area name on line 22.
#!/usr/bin/env ruby
# Syncing Tasks Assigned to you in Asana with Things 3
require 'json'
require 'yaml/store'
require 'time'
# Load storage file and variables
store = YAML::Store.new("data.yml")
mod = store.transaction { store[:mod_time] }
if mod == nil
mod = Time.now.utc.iso8601
end
existing = store.transaction { store[:asana_ids] }
if existing == nil
existing = []
end
# Load Asana variables and Things area
asana_token = store.transaction { store[:asana_token] }
workspace = store.transaction { store[:asana_workspace] }
area = "YOUR AREA NAME"
# Find and close completed tasks
completed = %x{ osascript <<APPLESCRIPT
tell application "Things3"
set output to ""
repeat with toDo in (to dos whose status is completed) of area "#{area}"
set theID to notes of toDo
set output to output & theID & ","
end repeat
return output
end tell
APPLESCRIPT}
completed = completed.strip
if completed != ""
completed = completed.chomp(",").split(",")
completed.each do |id|
`curl -s --request PUT -H "Authorization: Bearer #{asana_token}" https://app.asana.com/api/1.0/tasks/#{id} --data-urlencode "completed=true"`
end
`osascript -e 'Tell application "Things3" to log completed now'`
else
puts "Nothing doing!"
end
# Get new uncompleted tasks from Asana, create them if they don't already exist
tasks = `curl -s -H "Authorization: Bearer #{asana_token}" "https://app.asana.com/api/1.0/tasks?workspace=#{workspace}&assignee=me&modified_since=#{mod}&completed_since=now"`
data = JSON.parse(tasks)
if data["data"].empty? == true
puts "No changes!"
else
existing = store.transaction { store[:asana_ids] }
data["data"].each do |task|
name = task["name"]
id = task["id"].to_s
if existing.include?(id) == false
%x{ osascript <<APPLESCRIPT
tell application "Things3"
set newToDo to make new to do ¬
with properties {name:"#{name}", notes:"#{id}"} at beginning of area "#{area}"
end tell
APPLESCRIPT}
store.transaction do
store[:asana_ids].push(id)
end
puts "#{name} created!"
else
puts "#{name} already present!"
end
end
end
store.transaction do
store[:mod_time] = Time.now.utc.iso8601
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment