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.

For anyone interested, and building on viewtopic.php?id=45861 (editor won’t let me post URL here) here’s a solution that does work. This will return selectedEvent regardless of which calendar the event is selected in. This gets around the issue of the selectedID reported by the OP above.

Hmmm, wonder how you are supposed to put code snippets in this editor…

Rod

Code
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

-- Determine selected event
set defaultsReply to (do shell script "defaults read com.apple.ical SelectedEvents")

set localUIDs to {}
set {TID, text item delimiters} to {text item delimiters, quote}
set resultItems to text items of defaultsReply
set text item delimiters to TID
repeat with i from 1 to (count resultItems)
	if i mod 2 = 0 then set end of localUIDs to resultItems's item i
end repeat


set selectedID to (item 1 of localUIDs) -- (((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

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

tell application id "com.apple.iCal" -- Calendar
	set theProps to properties of event id (item 1 of localUIDs) of calendar id theCalID
	--	set oEvent to event id theEventID of calendar id theCalID
	--	set eventDesc to description of oEvent
	--	set theProps to properties of oEvent
end tell

return theProps