-
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy paththings.sh
More file actions
executable file
·202 lines (183 loc) · 5.61 KB
/
things.sh
File metadata and controls
executable file
·202 lines (183 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env bash
#
# DESCRIPTION
#
# Simple read-only comand-line interface to your Things 3 database. Since
# Things uses a SQLite database (which should come pre-installed on your Mac)
# we can simply query it straight from the command line.
#
# We only do read operations since we don't want to mess up your data.
#
# CREDITS
#
# Author : Arjan van der Gaag (script for Things 2)
# Author : Alexander Willner (updates for Things 3, complete rewrite)
# License : Whatever. Use at your own risk.
# Source : https://github.com/AlexanderWillner/things.sh
###############################################################################
# Robust shell code ###########################################################
set -o errexit
set -o nounset
set -o pipefail
[[ "${TRACE:-}" ]] && set -x
###############################################################################
# Core parameters #############################################################
realpath() {
OURPWD="$PWD"
cd "$(dirname "$1")"
LINK="$(readlink "$(basename "$1")")"
while [ "$LINK" ]; do
cd "$(dirname "$LINK")"
LINK="$(readlink "$(basename "$1")")"
done
REALPATH="$PWD/$(basename "$1")"
cd "$OURPWD"
echo "$REALPATH"
}
readonly PROGNAME="$(basename "$0")"
readonly PATHNAME="$(dirname "$(realpath "$0")")"
readonly DEFAULT_DB=$(find ~/Library/Group\ Containers/JLMPQHK86H.com.culturedcode.ThingsMac -name 'main.sqlite' | head -1)
if [[ -z "$DEFAULT_DB" ]]; then
echo "Your database wasn't found!"
exit 1
fi
readonly THINGSDB="${THINGSDB:-$DEFAULT_DB}"
readonly PLUGINDIR="$PATHNAME/plugins"
###############################################################################
# Things database structure ###################################################
export readonly TASKTABLE="TMTask"
export readonly AREATABLE="TMArea"
export readonly TAGTABLE="TMTag"
export readonly TASKTAGTABLE="TMTaskTag"
export readonly ISNOTTRASHED="trashed = 0"
export readonly ISTRASHED="trashed = 1"
export readonly ISOPEN="status = 0"
export readonly ISNOTSTARTED="start = 0"
export readonly ISCANCELLED="status = 2"
export readonly ISCOMPLETED="status = 3"
export readonly ISSTARTED="start = 1"
export readonly ISPOSTPONED="start = 2"
export readonly ISTASK="type = 0"
export readonly ISPROJECT="type = 1"
export readonly ISHEADING="type = 2"
###############################################################################
# Use defined parameters ######################################################
export LIMIT_BY="all"
export WAITING_TAG="Waiting for"
export ORDER_BY="creationDate"
export EXPORT_RANGE="-1 year"
export SEARCH_STRING=""
export EVENTLIST="$HOME/.trip.thingslist"
export EVENTSTART=""
export EVENTDURATION=""
###############################################################################
# Define methods ##############################################################
main() {
require_sqlite3
require_db
parse "${@}"
}
require_sqlite3() {
command -v sqlite3 >/dev/null 2>&1 || {
echo >&2 "ERROR: SQLite3 is required but could not be found."
exit 1
}
}
require_db() {
test -r "$THINGSDB" -a -f "$THINGSDB" || {
echo >&2 "ERROR: Things database not found at '$THINGSDB'."
echo >&2 "HINT: You might need to install Things from https://culturedcode.com/things/"
exit 2
}
}
load_plugins() {
for plugin in "$PLUGINDIR"/*; do
# shellcheck source=/dev/null
source "$plugin"
done
}
parse() {
while [[ ${#} -gt 1 ]]; do
local key="${1}"
case "$key" in
-l | --limitBy)
LIMIT_BY="${2}"
shift
;;
-w | --waitingTag)
WAITING_TAG="${2}"
shift
;;
-o | --orderBy)
ORDER_BY="${2}"
shift
;;
-s | --string)
SEARCH_STRING="${2}"
shift
;;
-r | --range)
EXPORT_RANGE="${2}"
shift
;;
-e | --event)
EVENTLIST="${2}"
shift
;;
-d | --duration)
EVENTDURATION="${2}"
shift
;;
-t | --start)
EVENTSTART="${2}"
shift
;;
*) ;;
esac
shift
done
load_plugins
[[ "$LIMIT_BY" == "all" ]] && export LIMIT_BY="-1"
local command="${1:-}"
if [[ -n "$command" ]]; then
if hasPlugin "${1}"; then
invokePlugin "${1}"
else
usage
fi
else
usage
fi
}
usage() {
cat <<-EOF
usage: ${PROGNAME} <OPTIONS> [COMMAND]
OPTIONS:
-l|--limitBy <number> Limit output by <number> of results
-w|--waitingTag <tag> Set waiting/filter tag to <tag>
-o|--orderBy <column> Sort output by <column> (e.g. 'userModificationDate' or 'creationDate')
-s|--string <string> String <string> to search for
-r|--range <string> Limit CSV statistic export by <string>
-e|--event <filename> Event: <filename> that contains a list of tasks
-t|--start <date> Event: starts at <date>
-d|--duration <days> Event: ends after <days>
COMMANDS:
EOF
getPluginHelp
}
cleanup() {
local err="${1:-}"
local line="${2:-}"
local linecallfunc="${3:-}"
local command="${4:-}"
local funcstack="${5:-}"
if [[ "$err" -ne "0" ]]; then
echo 2>&1 "ERROR: line $line - command '$command' exited with status: $err."
echo 2>&1 "ERROR: In $funcstack called at line $linecallfunc."
echo 2>&1 "DEBUG: From function ${funcstack[0]} (line $linecallfunc)."
fi
}
###############################################################################
# Run script ##################################################################
[[ "${BASH_SOURCE[0]}" == "${0}" ]] && trap 'cleanup "${?}" "${LINENO}" "${BASH_LINENO}" "${BASH_COMMAND}" $(printf "::%s" ${FUNCNAME[@]:-})' EXIT && main "${@:-}"
###############################################################################