5
5
from ..utils .run_applescript import run_applescript , run_applescript_capture
6
6
7
7
8
+ makeDateFunction = """
9
+ on makeDate(yr, mon, day, hour, min, sec)
10
+ set theDate to current date
11
+ tell theDate
12
+ set its year to yr
13
+ set its month to mon
14
+ set its day to day
15
+ set its hours to hour
16
+ set its minutes to min
17
+ set its seconds to sec
18
+ end tell
19
+ return theDate
20
+ end makeDate
21
+ """
22
+
8
23
class Calendar :
9
24
def __init__ (self , computer ):
10
25
self .computer = computer
@@ -18,23 +33,13 @@ def get_events(self, start_date=datetime.date.today(), end_date=None):
18
33
if platform .system () != "Darwin" :
19
34
return "This method is only supported on MacOS"
20
35
21
- # Format dates for AppleScript
22
- applescript_start_date = (
23
- start_date .strftime ("%A, %B %d, %Y" ) + " at 12:00:00 AM"
24
- )
25
- if end_date :
26
- applescript_end_date = (
27
- end_date .strftime ("%A, %B %d, %Y" ) + " at 11:59:59 PM"
28
- )
29
- else :
30
- applescript_end_date = (
31
- start_date .strftime ("%A, %B %d, %Y" ) + " at 11:59:59 PM"
32
- )
33
-
36
+ if not end_date :
37
+ end_date = start_date
34
38
# AppleScript command
35
39
script = f"""
36
- set theDate to date "{ applescript_start_date } "
37
- set endDate to date "{ applescript_end_date } "
40
+ { makeDateFunction }
41
+ set theDate to makeDate({ start_date .strftime ("%Y, %m, %d, 0, 0, 0" )} )
42
+ set endDate to makeDate({ end_date .strftime ("%Y, %m, %d, 23, 59, 59" )} )
38
43
tell application "System Events"
39
44
set calendarIsRunning to (name of processes) contains "{ self .calendar_app } "
40
45
if calendarIsRunning then
@@ -179,6 +184,9 @@ def create_event(
179
184
return "Can't find a default calendar. Please try again and specify a calendar name."
180
185
181
186
script = f"""
187
+ { makeDateFunction }
188
+ set startDate to makeDate({ start_date .strftime ("%Y, %m, %d, %H, %M, %S" )} )
189
+ set endDate to makeDate({ end_date .strftime ("%Y, %m, %d, %H, %M, %S" )} )
182
190
-- Open and activate calendar first
183
191
tell application "System Events"
184
192
set calendarIsRunning to (name of processes) contains "{ self .calendar_app } "
@@ -192,8 +200,6 @@ def create_event(
192
200
end tell
193
201
tell application "{ self .calendar_app } "
194
202
tell calendar "{ calendar } "
195
- set startDate to date "{ applescript_start_date } "
196
- set endDate to date "{ applescript_end_date } "
197
203
make new event at end with properties {{summary:"{ title } ", start date:startDate, end date:endDate, location:"{ location } ", description:"{ notes } "}}
198
204
end tell
199
205
-- tell the Calendar app to refresh if it's running, so the new event shows up immediately
@@ -223,9 +229,9 @@ def delete_event(
223
229
if not calendar :
224
230
return "Can't find a default calendar. Please try again and specify a calendar name."
225
231
226
- # Format datetime for AppleScript
227
- applescript_start_date = start_date .strftime ("%B %d, %Y %I:%M:%S %p" )
228
232
script = f"""
233
+ { makeDateFunction }
234
+ set eventStartDate to makeDate({ start_date .strftime ("%Y, %m, %d, %H, %M, %S" )} )
229
235
-- Open and activate calendar first
230
236
tell application "System Events"
231
237
set calendarIsRunning to (name of processes) contains "{ self .calendar_app } "
@@ -242,7 +248,6 @@ def delete_event(
242
248
set myCalendar to calendar "{ calendar } "
243
249
244
250
-- Define the exact start date and name of the event to find and delete
245
- set eventStartDate to date "{ applescript_start_date } "
246
251
set eventSummary to "{ event_title } "
247
252
248
253
-- Find the event by start date and summary
0 commit comments