How Do I Get Selected Event in Apple Calendar?

@ShaneStanley, do you have an update to this script that will get the last event selected in the Apple Calendar app?
Running Calendar 11.0 (2245.5.2) on macOS 10.14.6 (Mojave).

I entered one new event, ran the script, and it worked fine.
I added two more events, one recurring, one normal, but the script still finds only the first event I entered. I tried restarting both Calendar and SD, but to no avail.

Thanks.

(*
REF:  The following were used in some way in the writing of this script.

  1.  2017-07-30 02:12, Shane Stanley, MacScripter
      calendar selection
      https://macscripter.net/viewtopic.php?pid=191043#p191043
*)


use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "EventKit"
use script "CalendarLib EC" version "1.1.4"
use scripting additions

set selectedID to (((current application's NSUserDefaults's alloc()'s initWithSuiteName:"com.apple.ical")'s objectForKey:"SelectedEvents")'s objectForKey:"iCal")'s firstObject()
if selectedID = missing value then error "No selection found"
set theEKEventStore to fetch store
set theEvent to theEKEventStore's eventWithIdentifier:selectedID

### This is NOT Getting the LAST Event Selected ###
set theEventID to theEvent's calendarItemExternalIdentifier() as text

set theCalID to theEvent's calendar()'s calendarIdentifier() as text

tell application id "com.apple.iCal" -- Calendar
  set oEvent to event id theEventID of calendar id theCalID
  set eventDesc to description of oEvent
  
end tell

return eventDesc


Nothing that I’m aware of. You’re at the mercy of when Calendar feels the urge to update its prefs file. Without knowing why it even stores the value there, it’s hard to know what might prompt an update.

1 Like

Thanks Shane.

I can’t believe what a POS Apple Calendar is. This is just one more reason I’m glad I use MS Outlook, whose calendar is much, much more powerful, and very scriptable. I was just trying to help someone else who needs to get the details of the selected event.