forked from ttscoff/OTask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
otask
executable file
·259 lines (209 loc) · 6.88 KB
/
otask
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/usr/bin/ruby
# == Synopsis
# OTask provides CLI functionality for adding tasks to OmniFocus.
# It requires rb-appscript, amatch and chronic, all installable with rubygems
#
# == Usage
# otask [options] "Task string"
#
# For help use: otask -h
#
# == Options
# -h, --help Displays help message
# -q, --quiet Output as little as possible, overrides verbose
# -V, --verbose Verbose output
# -g, --growl Use Growl for feedback
#
# == Author
# Brett Terpstra
#
# == Copyright
# Copyright (c) 2011 Brett Terpstra. Licensed under the MIT License:
# http://www.opensource.org/licenses/mit-license.php
require 'optparse'
require 'ostruct'
require 'date'
require 'rubygems'
require 'appscript';include Appscript
require 'amatch';include Amatch
require 'chronic'
class OTask
VERSION = '0.1'
attr_reader :options
def initialize(arguments, stdin)
@arguments = arguments
@stdin = stdin
# Set defaults
@options = OpenStruct.new
@options.verbose = false
@options.quiet = false
# TO DO - add additional defaults
end
# Parse options, check arguments, then process the command
def run
if parsed_options? && arguments_valid?
puts "Start at #{DateTime.now}\n\n" if @options.verbose
output_options if @options.verbose # [Optional]
process_arguments
process_command
puts "\nFinished at #{DateTime.now}" if @options.verbose
else
output_usage
end
end
protected
def parsed_options?
# Specify options
opts = OptionParser.new
opts.on('-h', '--help') { output_help }
opts.on('-V', '--verbose') { @options.verbose = true }
opts.on('-q', '--quiet') { @options.quiet = true }
opts.on('-g', '--growl') { @options.growl = true }
# TO DO - add additional options
opts.parse!(@arguments) rescue return false
process_options
true
end
# Performs post-parse processing on options
def process_options
@options.verbose = false if @options.quiet
if @options.growl
procs = app("System Events").processes.name.get(:result_type => :list)
if procs.include? "GrowlHelperApp"
app("GrowlHelperApp").register(:all_notifications => ["Alert"], :as_application => "OTask", :icon_of_application => "OmniFocus.app", :default_notifications => ["Alert"])
else
@options.growl = false
end
end
end
def output_options
puts "Options:\n"
@options.marshal_dump.each do |name, val|
puts " #{name} = #{val}"
end
end
# True if required arguments were provided
def arguments_valid?
# TO DO - implement your real logic here
true if @arguments.length == 1
end
# Setup the arguments
def process_arguments
input = @stdin.stat.size > 0 ? @stdin.read : nil
titlestring = @arguments.join(' ')
# check for trailing ! (flagged task)
@options.flagged = titlestring.match(/ !\Z/).nil? ? false : true
titlestring.sub!(/ !\Z/,'') if @options.flagged
# check for #project in the string
projmatch = titlestring.match(/ ?#([a-z0-9]+)/i)
@options.project = projmatch.nil? ? nil : projmatch[1]
titlestring.sub!(/ ?##{projmatch[1]}/,'') unless projmatch.nil?
# check for @contexts
contextmatch = titlestring.match(/ @([^ ]+)/)
@options.context = contextmatch.nil? ? false : contextmatch[1]
titlestring.sub!(/ @[^ ]+/,'') unless contextmatch.nil?
# start/due date
startmatch = titlestring.match(/ s(?:tart)?\(([^\)]+)\)/)
@options.start = startmatch.nil? ? false : startmatch[1]
titlestring.sub!(/ s(tart)?\(([^\)]+)\)/,'') unless startmatch.nil?
duematch = titlestring.match(/ d(?:ue)?\(([^\)]+)\)/)
@options.due = duematch.nil? ? false : duematch[1]
titlestring.sub!(/ d(ue)?\(([^\)]+)\)/,'') unless duematch.nil?
@options.notes = ''
if titlestring =~ /\(([^\)]+)\)/
@options.notes = $1
titlestring.gsub!(/\(([^\)]+)\)/,'')
end
if @options.notes == '' && !input.nil? # check for piped input on STDIN
@options.notes = input
elsif @options.notes && input
@options.notes = @options.notes + "\n\n" + input
end
@options.name = titlestring
end
def output_help
output_version
RDoc::usage() #exits app
end
def output_usage
RDoc::usage('usage') # gets usage from comments above
end
def output_version
puts "#{File.basename(__FILE__)} version #{VERSION}"
end
def add_task(dd, props)
if props['project']
proj_name = props["project"]
proj = dd.flattened_tasks[proj_name]
end
if props['context']
ctx_name = props["context"]
ctx = dd.flattened_contexts[ctx_name]
end
tprops = props.inject({}) do |h, (k, v)|
h[:"#{k}"] = v
h
end
tprops.delete(:project)
tprops[:context] = ctx if props['context']
t = dd.make(:new => :inbox_task, :with_properties => tprops)
t.assigned_container.set(proj) if props['project']
return true
end
def best_match(items,fragment)
highscore = {'score'=>0,'name'=>nil}
items.each {|item|
if fragment && !item.nil?
score = (Jaro.new(item).match(fragment) * 10).to_i
highscore = score > highscore['score'] ? {'score'=>score,'name'=>item} : highscore
end
}
return highscore['name']
end
def parse_date(datestring)
days = 0
if datestring =~ /^\+(\d+)$/
days = (60 * 60 * 24 * $1.to_i)
newdate = Time.now + days
else
newdate = Chronic.parse(datestring, {:context => :future, :ambiguous_time_range => 8})
end
# parsed = newdate.strftime('%D %l:%M%p').gsub(/\s+/,' ');
# return parsed =~ /1969/ ? false : parsed
return newdate
end
def growl(title, message)
app("GrowlHelperApp").notify(:title => title, :description => message, :application_name => "OTask", :with_name => "Alert") if @options.growl
end
def process_command
of = app('OmniFocus')
dd = of.default_document
@props = {}
@props['name'] = @options.name
if @options.project
projs = dd.flattened_projects.name.get
@props['project'] = best_match(projs,@options.project)
end
if @options.context
ctxs = dd.flattened_contexts.name.get
@props['context'] = best_match(ctxs,@options.context)
end
@props['start_date'] = parse_date(@options.start) if @options.start
@props['due_date'] = parse_date(@options.due) if @options.due
@props['note'] = @options.notes unless @options.notes == ''
@props['flagged'] = @options.flagged
add_task(dd, @props)
unless @options.quiet
o = "Task added to "
o += @props['project'].nil? ? "Inbox" : @props['project']
o += ", context "+@props['context']+"." unless @props['context'].nil?
if @options.growl
growl("Task created",o)
else
puts o
end
end
end
end
app = OTask.new(ARGV, STDIN)
app.run