forked from danielpunkass/RemindersImport
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.m
More file actions
214 lines (184 loc) · 7.4 KB
/
Copy pathmain.m
File metadata and controls
214 lines (184 loc) · 7.4 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
203
204
205
206
207
208
209
210
211
212
213
//
// main.m
// RemindersImport
//
// Created by Daniel Jalkut on 1/30/13.
// Copyright (c) 2013 Bitsplitting. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <EventKit/EventKit.h>
NSArray* importableReminderItems(EKEventStore* theEventStore)
{
NSMutableArray* qualifiedItems = [NSMutableArray array];
NSString* remindersListName = @"Reminders";
if ([[[NSProcessInfo processInfo] arguments] count] < 2)
{
NSLog(@"No Reminders list name specified, defaulting to \"%@\"", remindersListName);
}
else
{
remindersListName = [[[NSProcessInfo processInfo] arguments] objectAtIndex:1];
}
// Get all events from the "Reminders" list
__block BOOL done = NO;
// Get all the dedicated Reminders calendars
NSArray* remindersCalendars = [theEventStore calendarsForEntityType:EKEntityTypeReminder];
NSPredicate *remindersPredicate = [theEventStore predicateForIncompleteRemindersWithDueDateStarting:nil ending:nil calendars:remindersCalendars];
[theEventStore fetchRemindersMatchingPredicate:remindersPredicate completion:^(NSArray *reminders)
{
// Since fetchRemindresMatchingPredicate doesn't actually work with general-purpose NSPredicate,
// we have to use one of the coarse-grained EKEventStore-vended predicates, and then further filter on
// our own. This predicate attempts to match The Omni Group's own filtering approach: if the reminder
// is in the list and is not a location-based reminder, just snag it and delete it, otherwise,
// leave it alone.
//
// NOTE: I'm being lazy for starters and not figuring out how to specify that the matched reminder
// should have NO alarms that feature a non-nil structuredLocation (i.e. the alarm is not to remind when
// arriving at a speicfic location). I just filter those out in the for loop below.
NSPredicate* fineGrainPredicate = [NSPredicate predicateWithFormat:@"calendar.title = %@", remindersListName];
NSArray* targetReminderItems = [reminders filteredArrayUsingPredicate:fineGrainPredicate];
for (EKReminder* thisReminder in targetReminderItems)
{
BOOL hasLocationBasedAlarm = NO;
for (EKAlarm* thisAlarm in [thisReminder alarms])
{
if ([thisAlarm structuredLocation] != nil)
{
hasLocationBasedAlarm = YES;
break;
}
}
if (hasLocationBasedAlarm == NO)
{
[qualifiedItems addObject:thisReminder];
}
}
done = YES;
}];
while (done == NO)
{
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
}
return qualifiedItems;
}
NSString* omniFocusAddTaskScriptTemplate()
{
return @"\
-- Work around a problem where if OmniFocus is not already running,\n\
-- it might hang when trying to handle our request just as it's launching\n\
set isRunning to true\n\
tell application \"System Events\"\n\
try\n\
application process \"OmniFocus\"\n\
on error\n\
set isRunning to false\n\
end try\n\
end tell\n\
\n\
-- Wait a good long 10 seconds to make sure OmniFocus has time to launch\n\
if (isRunning is false) then\n\
tell application \"OmniFocus\" to launch\n\
delay 10.0\n\
end if\n\
\
tell application \"OmniFocus\"\n\
tell default document\n\
make new inbox task with properties {name:\"%@\", start date:%@, due date:%@, note:\"%@\"}\n\
end tell\n\
end tell";
}
NSString* dateArgumentStringForDateString(NSString* theDateString)
{
// The date arguments should be either "date \"<dateString>\"" or else "missing value" if nil
NSString* dateArgument = ([theDateString length] > 0) ? [NSString stringWithFormat:@"date \"%@\"", theDateString] : @"missing value";
return dateArgument;
}
BOOL makeOmniFocusInboxTaskFromReminderInfo(NSString* newTaskTitle, NSString* startDateString, NSString* dueDateString, NSString* notes)
{
BOOL didSucceed = NO;
// Escape any quotes in the task title and notes
NSString* escapedTaskTitle = [newTaskTitle stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
NSString* escapedTaskNotes = [notes stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
NSString* startDateArgument = dateArgumentStringForDateString(startDateString);
NSString* dueDateArgument = dateArgumentStringForDateString(dueDateString);
NSString* newTaskAppleScript = [NSString stringWithFormat:omniFocusAddTaskScriptTemplate(), escapedTaskTitle, startDateArgument, dueDateArgument, escapedTaskNotes];
NSAppleScript* addTaskScript = [[[NSAppleScript alloc] initWithSource:newTaskAppleScript] autorelease];
NSDictionary* anyError = nil;
(void) [addTaskScript executeAndReturnError:&anyError];
if (anyError != nil)
{
NSLog(@"Failed to add task, got scripting error: %@", anyError);
didSucceed = NO;
}
else
{
NSLog(@"Added task \"%@\" to OmniFocus.", newTaskTitle);
didSucceed = YES;
}
return didSucceed;
}
NSCalendar* gregorianCalendar()
{
return [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];;
}
NSDateFormatter* gregorianFormatterWithFormat(NSString* formatString)
{
NSDateFormatter* gregorianFormatter = [[[NSDateFormatter alloc] init] autorelease];
[gregorianFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[gregorianFormatter setDateFormat:formatString];
[gregorianFormatter setCalendar:gregorianCalendar()];
return gregorianFormatter;
}
NSString* scriptableDateStringFromComponents(NSDateComponents* inComponents)
{
NSString* dateString = nil;
if (inComponents != nil)
{
// This is in Unicode format as described here: http://userguide.icu-project.org/formatparse/datetime,
// and designed to match the example like "Thursday, January 31, 2013 4:32:40 PM". I observed this
// format as the default format returned by AppleScript on my US-based English Mac. I can't vouch
// for how well this will work in other locales, but let me know if you try it!
NSString* dateFormatString = @"EEEE, LLLL d y hh:mm:ss a";
NSDate *targetDate = [gregorianCalendar() dateFromComponents:inComponents];
NSDateFormatter* dateFormatter = gregorianFormatterWithFormat(dateFormatString);
dateString = [dateFormatter stringFromDate:targetDate];
}
return dateString;
}
int main(int argc, const char * argv[])
{
@autoreleasepool
{
EKEventStore* eventStore = [[EKEventStore alloc] initWithAccessToEntityTypes:EKEntityMaskReminder];
for (EKReminder* thisReminder in importableReminderItems(eventStore))
{
// Extract the information we need to ask OmniFocus to add the item
NSString* newTaskName = [thisReminder title];
NSString* startDateString = scriptableDateStringFromComponents([thisReminder startDateComponents]);
NSString* dueDateString = scriptableDateStringFromComponents([thisReminder dueDateComponents]);
NSString* notes = [NSString string];
if (thisReminder.hasNotes)
{
notes = [thisReminder notes];
}
if (makeOmniFocusInboxTaskFromReminderInfo(newTaskName, startDateString, dueDateString, notes) == YES)
{
NSLog(@"Removing task %@ from event store", newTaskName);
NSError* removeError = nil;
if ([eventStore removeReminder:thisReminder commit:NO error:&removeError] == NO)
{
NSLog(@"Failed to remove reminder %@ from event store. Error: %@", newTaskName, [removeError localizedDescription]);
}
}
}
// We queued up any removes by specifying commit:NO, so let's commit now to finish the job
NSError* commitError = nil;
if ([eventStore commit:&commitError] == NO)
{
NSLog(@"Failed to commit reminder removes. Error: %@", [commitError localizedDescription]);
}
// Clean up because it just feels right.
[eventStore release];
}
return 0;
}