-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_org_file_handler
executable file
·222 lines (189 loc) · 5.58 KB
/
_org_file_handler
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
#!/usr/bin/perl
# Background:
# 1.This perl file is writen to add the time event to calendar in OSX
# 2.Auto run applescript with `osascript -e`
# Input: org file to organize the time
# Output(function): calendar's change when necessary.
# Author: Yuanqi Cao <caoyuanqi@gmail.com>
# website: http://caoyuanqi.github.io
# version: 0.8
use syntax 'junction';
$change = 0;
# get the already sheduled event times from file and read it into file
open (FILE, "<", "/Users/Cao/Dropbox/org/time.txt") or die("Can't open file");
@times = <FILE>;
close(FILE);
$change = 0;
while($line = <stdin>)
{
# get the title
if ($line =~ /^\* /)
{
$calendar = $line;
$calendar =~ s/\*//g;
chop($calendar);
$calendar =~ s/ //g;
next;
}
if ($line =~ /\*/)
{
$title = $line;
$title =~ s/\*//g;
$title =~ s/\'/\'\\\'\'/g;
$title =~ s/\"/\"\\\"\"/g;
$title =~ s/TODO *//g;
$title =~ s/STARTED *//g;
chop($title);
next;
}
# get the shedule date
if ($line =~ /(SCHEDULED: \<[0-9]{4}-[0-9]{2}-[0-9]{2} [a-zA-Z]{3}\>)/g)
{
print "This is a all-day event\n";
my $date = $1;
_create_mac_calendar($date,0);
next;
}
if ($line =~ /(SCHEDULED: \<[0-9]{4}-[0-9]{2}-[0-9]{2} [a-zA-Z]{3} [0-9]{1,2}:[0-9]{2}-[0-9]{1,2}:[0-9]{2}\>)/g)
{
my $date = $1;
_create_mac_calendar($date,1);
}
}
# Change file or not;
if ($change == 1)
{
_file_writer();
$command_to_restart = 'tell application "Calendar" to quit
delay 2 -- Wait for Safari to close
tell application "Calendar" to activate';
`osascript -e '$command_to_restart'`;
}
# Writer time information to the time.txt in org folder
sub _file_writer
{
print "Need to change the org time file";
`rm ~/Dropbox/org/time.txt`;
open (OUT, ">", "/Users/Cao/Dropbox/org/time.txt");
print OUT @times;
close OUT;
#`osascript -e 'tell application "Calendar" to quit'`
}
# calenadar creater
sub _create_mac_calendar
{
print "Checking the calendar for title: $title\n";
my $date = $_[0];
# if the event is a all-day event
if($_[1] == 0)
{
print "_create_mac_calendar: Get a all-day event\n";
($year,$month,$day)=($date =~ /SCHEDULED: \<([0-9]{4})-([0-9]{2})-([0-9]{2}) [a-zA-Z]{3}\>/g);
print "This is a whole day event year: $year, month: $month, date: $day";
$begin_date_string =$day.'/'.$month.'/'.$year;
# The check if this event is already added to the calendar
$this_saved = 0;
foreach $item (@times)
{
$test_item = $item;
$test_item =~ s/\n//g;
print "$item -- $begin_date_string\n";
if ($test_item eq $begin_date_string)
{
print "$test_item: This event is already been recorded\n";
$this_saved = 1;
last;
}
}
print "This changed is $this_saved";
if ( $this_saved == 0)
{
push @times, "$begin_date_string\n";
$change = 1;
}
else
{
return;
}
$string_command = '
set dateString to "'.$begin_date_string.'"
set startDate to date dateString
set theSummary to "'.$title.'"
tell application "Calendar" to run
tell application "Calendar"
tell calendar "'.$calendar.'"
make new event at end of events with properties {summary:theSummary, start date:startDate, allday event: true }
end tell
end tell
';
print $string_command;
`osascript -e '$string_command'`;
}
# if it is not a all-day element
# add this event with time stamp
elsif ($_[1] == 1)
{
print "This is a event with time stamp\n";
($year,$month,$day,$begin_time,$end_time )=($date =~ /SCHEDULED: \<([0-9]{4})-([0-9]{2})-([0-9]{2}) [a-zA-Z]{3} ([0-9]{1,2}:[0-9]{2})-([0-9]{2}:[0-9]{2})\>/g);
#print "$year:$month:$day:$begin_time:$end_time\n";
($begin_hour,$begin_min) = split(':',$begin_time);
($end_hour,$end_min) = split(':',$end_time);
my $begin_time = _get_right_time($begin_hour, $begin_min);
my $end_time = _get_right_time($end_hour, $end_min);
$begin_date_string = $day.'/'.$month.'/'.$year.' '.$begin_time;
$end_date_string = $day.'/'.$month.'/'.$year.' '.$end_time;
$this_saved = 0;
foreach $item (@times)
{
if ($item =~ $begin_date_string)
{
$this_saved = 1;
last;
}
}
if ( $this_saved == 0)
{
push @times, "$begin_date_string\n";
$change = 1;
}
else
{
return;
}
$string_command = '
set dateString to "'.$begin_date_string.'"
set startDate to date dateString
set endString to "'.$end_date_string.'"
set endDate to date endString
set theSummary to "'.$title.'"
tell application "Calendar" to run
tell application "Calendar"
tell calendar "'.$calendar.'"
name
make new event at end of events with properties {summary:theSummary, start date:startDate, end date:endDate }
end tell
end tell
endDate
';
print $string_command;
`osascript -e '$string_command'`;
}
}
# change the time into pm or am
sub _get_right_time
{
my $hour = $_[0];
my $min = $_[1];
if($hour <= 12)
{
$pm_or_am = "am";
$pm_or_am = "pm"if ($hour == 12);
}
else
{
$hour = scalar($hour) - 12;
$pm_or_am = "pm";
}
$time = $hour.':'.$min." $pm_or_am";
return $time;
}