Skip to content

Instantly share code, notes, and snippets.

@alexrudy
Last active July 21, 2017 13:37
Show Gist options
  • Save alexrudy/4c6017f2c2ee7d3c51079176f5a56b7b to your computer and use it in GitHub Desktop.
Save alexrudy/4c6017f2c2ee7d3c51079176f5a56b7b to your computer and use it in GitHub Desktop.
Things.app basic CLI
#/usr/bin/osascript
using terms from application "Things"
on appendTags(thecontainer, thetags)
if (count of {tags in thecontainer}) is greater than 0 then
repeat with thistag in (tags of thecontainer)
set thisname to name of thistag as text
if thetags does not contain thisname then
copy thisname to the end of thetags
end if
end repeat
end if
end appendTags
on formatDueDate(thetodo)
set datetext to ""
tell application "Things"
if due date of thetodo is not equal to missing value then
set ndays to round (((due date of thetodo) - (current date)) / (3600 * 24))
set duedate to (due date of thetodo)
if year of duedate is equal to year of (current date) and month of duedate is equal to month of (current date) and day of duedate is equal to day of (current date) then
set datetext to " !Due Today"
else
if ndays is not equal to 1 then
set datetext to " !Due in " & ndays & " days"
else
set datetext to " !Due Tomororw"
end if
end if
end if
end tell
return datetext
end formatDueDate
on formatTodo(thetodo, counter)
tell application "Things"
set todotext to name of thetodo
-- Add star for Today
if (id of to dos of list "Today") contains id of thetodo then
set todotext to "★ " & todotext
end if
-- Collect tags
set thesetags to {}
appendTags(thetodo, thesetags) of me
-- Add project / area information
set posttext to ""
if project of thetodo is not equal to missing value then
set posttext to posttext & " @" & name of project of thetodo
appendTags(project of thetodo, thesetags) of me
if area of project of thetodo is not equal to missing value then
set posttext to posttext & " / " & name of area of project of thetodo
appendTags(area of project of thetodo, thesetags) of me
end if
else
if area of thetodo is not equal to missing value then
set posttext to posttext & " @" & name of area of thetodo
appendTags(area of thetodo, thesetags) of me
end if
end if
if (count of thesetags) is greater than 0 then
set AppleScript's text item delimiters to {" +"}
set tagtext to " (+" & (thesetags as text) & ")"
set todotext to todotext & tagtext
end if
set todotext to todotext & posttext & formatDueDate(thetodo) of me
set innertodos to {}
try
set innertodos to (to dos of thetodo whose status is open)
end try
set innercounter to 0
repeat with innertodo in innertodos
if (id of to dos of list "Trash") does not contain (id of innertodo) then
set innercounter to innercounter + 1
set thiscounter to counter & "." & innercounter
set todotext to todotext & newline & " " & thiscounter & ") " & formatTodo(innertodo, thiscounter) of me
end if
end repeat
end tell
return todotext
end formatTodo
on getTodoCollection(thelistname)
tell application "Things"
if thelistname is in (name of lists) then
set todos to (to dos of list thelistname)
else if thelistname is in (name of projects) then
set todos to (to dos of project thelistname)
else if thelistname is in (name of areas) then
set todos to (to dos of area thelistname)
else if thelistname is in (name of tags) then
set todos to (to dos of tag thelistname)
else
error "Can't find list '" & thelistname & "' in Things"
end if
return todos
end tell
end getTodoCollection
on run (argv)
global newline
set newline to "
"
tell application "Things"
if (count of argv) is greater than 1 then
set thelistname to item 1 of argv
else
set thelistname to "Today"
end if
set myList to {}
set todos to getTodoCollection(thelistname) of me
if todos is not equal to {} and (count of todos) is greater than 0 then
set counter to 0
repeat with thetodo in todos
if (id of to dos of list "Trash") does not contain (id of thetodo) then
if status of thetodo is open then
set counter to counter + 1
set prefix to (counter as text) & ") "
set end of myList to prefix & formatTodo(thetodo, counter as text) of me
else
set prefix to "X) "
end if
end if
end repeat
set AppleScript's text item delimiters to newline
get myList as text
end if
end tell
end run
end using terms from
#!/usr/bin/env zsh
#
# things
# A simple CLI interface to Things.app
#
# Created by Alexander Rudy on 2016-08-15.
# Copyright 2016 Alexander Rudy. All rights reserved.
HELP=$(cat <<EOF
usage: $0 [list]
This uses applescript to access the Things.app database
and print todo items to standard-out. Pass no arguments
to see the contents of your "Today" list, or pass in the
name of a Project, Area, or "Next", "Inbox" etc. to see
only todos in that list. You can also pass in the name
of a tag to filter by tag.
EOF
)
if [[ "$1" == '-h' ]]; then
echo "$HELP"
exit 1
fi
LIST=${1:-"Today"}
osascript things.applescript $LIST
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment