Skip to content

Commit b283cb3

Browse files
authored
Merge pull request #1198 from supersational/main
Fix computer.calendar dates issue
2 parents ca1e3f3 + c6c37ee commit b283cb3

File tree

4 files changed

+28
-23
lines changed

4 files changed

+28
-23
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

interpreter/core/default_system_message.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
```python
1919
computer.browser.search(query) # Google search results will be returned from this function as a string
2020
computer.files.edit(path_to_file, original_text, replacement_text) # Edit a file
21-
computer.calendar.create_event(title="Meeting", start_date=datetime.datetime.now(), end=datetime.datetime.now() + datetime.timedelta(hours=1), notes="Note", location="") # Creates a calendar event
21+
computer.calendar.create_event(title="Meeting", start_date=datetime.datetime.now(), end_date=datetime.datetime.now() + datetime.timedelta(hours=1), notes="Note", location="") # Creates a calendar event
2222
computer.calendar.get_events(start_date=datetime.date.today(), end_date=None) # Get events between dates. If end_date is None, only gets events for start_date
2323
computer.calendar.delete_event(event_title="Meeting", start_date=datetime.datetime) # Delete a specific event with a matching title and start date, you may need to get use get_events() to find the specific event object first
2424
computer.contacts.get_phone_number("John Doe")

interpreter/terminal_interface/profiles/defaults/01.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
```python
7070
result_string = computer.browser.search(query) # Google search results will be returned from this function as a string
7171
computer.files.edit(path_to_file, original_text, replacement_text) # Edit a file
72-
computer.calendar.create_event(title="Meeting", start_date=datetime.datetime.now(), end=datetime.datetime.now() + datetime.timedelta(hours=1), notes="Note", location="") # Creates a calendar event
72+
computer.calendar.create_event(title="Meeting", start_date=datetime.datetime.now(), end_date=datetime.datetime.now() + datetime.timedelta(hours=1), notes="Note", location="") # Creates a calendar event
7373
events_string = computer.calendar.get_events(start_date=datetime.date.today(), end_date=None) # Get events between dates. If end_date is None, only gets events for start_date
7474
computer.calendar.delete_event(event_title="Meeting", start_date=datetime.datetime) # Delete a specific event with a matching title and start date, you may need to get use get_events() to find the specific event object first
7575
phone_string = computer.contacts.get_phone_number("John Doe")

interpreter/terminal_interface/profiles/defaults/os.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
print('''
6363
computer.browser.search(query) # Google search results will be returned from this function as a string
6464
computer.files.edit(path_to_file, original_text, replacement_text) # Edit a file
65-
computer.calendar.create_event(title="Meeting", start_date=datetime.datetime.now(), end=datetime.datetime.now() + datetime.timedelta(hours=1), notes="Note", location="") # Creates a calendar event
65+
computer.calendar.create_event(title="Meeting", start_date=datetime.datetime.now(), end_date=datetime.datetime.now() + datetime.timedelta(hours=1), notes="Note", location="") # Creates a calendar event
6666
computer.calendar.get_events(start_date=datetime.date.today(), end_date=None) # Get events between dates. If end_date is None, only gets events for start_date
6767
computer.calendar.delete_event(event_title="Meeting", start_date=datetime.datetime) # Delete a specific event with a matching title and start date, you may need to get use get_events() to find the specific event object first
6868
computer.contacts.get_phone_number("John Doe")

0 commit comments

Comments
 (0)