Skip to content

Commit d54c06e

Browse files
committed
computer.calendar fix dates bug #1148
converting dates from python to applescript used string parsing which is dependant on localization settings (e.g. american vs english dates). Here we use a bulletproof conversion function to fix:
1 parent 268e8ec commit d54c06e

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

interpreter/core/computer/calendar/calendar.py

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,21 @@
55
from ..utils.run_applescript import run_applescript, run_applescript_capture
66

77

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+
823
class Calendar:
924
def __init__(self, computer):
1025
self.computer = computer
@@ -18,23 +33,13 @@ def get_events(self, start_date=datetime.date.today(), end_date=None):
1833
if platform.system() != "Darwin":
1934
return "This method is only supported on MacOS"
2035

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
3438
# AppleScript command
3539
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")})
3843
tell application "System Events"
3944
set calendarIsRunning to (name of processes) contains "{self.calendar_app}"
4045
if calendarIsRunning then
@@ -179,6 +184,9 @@ def create_event(
179184
return "Can't find a default calendar. Please try again and specify a calendar name."
180185

181186
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")})
182190
-- Open and activate calendar first
183191
tell application "System Events"
184192
set calendarIsRunning to (name of processes) contains "{self.calendar_app}"
@@ -192,8 +200,6 @@ def create_event(
192200
end tell
193201
tell application "{self.calendar_app}"
194202
tell calendar "{calendar}"
195-
set startDate to date "{applescript_start_date}"
196-
set endDate to date "{applescript_end_date}"
197203
make new event at end with properties {{summary:"{title}", start date:startDate, end date:endDate, location:"{location}", description:"{notes}"}}
198204
end tell
199205
-- tell the Calendar app to refresh if it's running, so the new event shows up immediately
@@ -223,9 +229,9 @@ def delete_event(
223229
if not calendar:
224230
return "Can't find a default calendar. Please try again and specify a calendar name."
225231

226-
# Format datetime for AppleScript
227-
applescript_start_date = start_date.strftime("%B %d, %Y %I:%M:%S %p")
228232
script = f"""
233+
{makeDateFunction}
234+
set eventStartDate to makeDate({start_date.strftime("%Y, %m, %d, %H, %M, %S")})
229235
-- Open and activate calendar first
230236
tell application "System Events"
231237
set calendarIsRunning to (name of processes) contains "{self.calendar_app}"
@@ -242,7 +248,6 @@ def delete_event(
242248
set myCalendar to calendar "{calendar}"
243249
244250
-- Define the exact start date and name of the event to find and delete
245-
set eventStartDate to date "{applescript_start_date}"
246251
set eventSummary to "{event_title}"
247252
248253
-- Find the event by start date and summary

0 commit comments

Comments
 (0)