-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnaturaldate.py
executable file
·67 lines (51 loc) · 1.61 KB
/
naturaldate.py
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
#!/usr/bin/env python
import parsedatetime.parsedatetime as pdt
import parsedatetime.parsedatetime_consts as pdc
import datetime
from osax import OSAX
from appscript import *
def gettime():
# by specifying System Events you keep python from
# creating a GUI app
sa = OSAX("StandardAdditions", name="System Events")
# bring dialog to front
sa.activate()
data = sa.display_dialog("Test",buttons=["OK","Cancel"],
default_button=1,
default_answer="Event - tomorrow")
if data == None:
return ""
if data[k.button_returned] == "OK":
return data[k.text_returned]
else:
return ""
def datetimeFromString( s ):
# parse date and time from natural string.
# http://stackoverflow.com/questions/1810432/handling-the-different-results-from-parsedatetime
c = pdc.Constants()
p = pdt.Calendar(c)
result, what = p.parse( s )
dt = 0
if what in (1,2,3):
# result is struct_time
dt = datetime.datetime( *result[:6] )
if what == 0:
# Failed to parse
return ""
return dt
def setcal(title, rd):
app(u'iCal').activate()
event = app(u'iCal').calendars[u'Work'].events.end.make(new=k.event)
event.summary.set( title )
event.description.set( title )
event.start_date.set( rd )
def main():
nattime = gettime()
if nattime == "":
return
title = nattime.split("-")[0]
NLPDate = datetimeFromString(nattime)
realdate = NLPDate
setcal( title, realdate )
if __name__ == '__main__':
main()