Last active
April 25, 2023 17:15
-
-
Save ivoleitao/f3937582744c412c1042c8d4617ea0d8 to your computer and use it in GitHub Desktop.
Get next meeting from Microsoft Outlook with apple script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Set the target Calendar | |
set calendars to "Calendar" | |
-- Set the start time margin to allow the return of the meeting x minutes before the start | |
set startTimeMargin to (5 * minutes) | |
-- The default meeting name in case no meeting is found | |
set defaultMeeting to "Untitled Meeting" | |
set now to current date | |
copy now to today | |
set time of today to 0 | |
set tomorrow to today + (1 * days) | |
set delims to AppleScript's text item delimiters | |
if calendars contains ", " then | |
set AppleScript's text item delimiters to {", "} | |
else | |
set AppleScript's text item delimiters to {","} | |
end if | |
set calendarNames to every text item of calendars | |
set AppleScript's text item delimiters to delims | |
tell application "Microsoft Outlook" | |
set calendarId to {} | |
set fallbackMeeting to "" | |
repeat with i from 1 to number of items in calendarNames | |
set calendarName to item i of calendarNames | |
set calendarId to calendarId & (id of every calendar whose name is calendarName) | |
end repeat | |
set calendarEvents to {} | |
repeat with i from 1 to number of items in calendarId | |
set CalID to item i of calendarId | |
tell (calendar id CalID) | |
set calendarEvents to (every calendar event whose start time is greater than or equal to today and start time is less than tomorrow) | |
repeat with i from 1 to (count of calendarEvents) | |
repeat with j from i + 1 to count of calendarEvents | |
if start time of item j of calendarEvents < start time of item i of calendarEvents then | |
set temp to item i of calendarEvents | |
set item i of calendarEvents to item j of calendarEvents | |
set item j of calendarEvents to temp | |
end if | |
end repeat | |
end repeat | |
repeat with i from 1 to (count of calendarEvents) | |
set startTime to start time of item i of calendarEvents | |
set endTime to end time of item i of calendarEvents | |
set startTimeWithMargin to startTime - startTimeMargin | |
set s to subject of item i of calendarEvents | |
if (now is greater than or equal to startTimeWithMargin and now is less than endTime) then | |
return s | |
end if | |
-- If no meeting is found select the first one | |
if (fallbackMeeting is "" and now is less than startTime) then | |
copy s to fallbackMeeting | |
end if | |
end repeat | |
end tell | |
end repeat | |
if fallbackMeeting is not "" then | |
return fallbackMeeting | |
else | |
return defaultMeeting | |
end if | |
end tell |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment