Calendar.app: Getting the UID of calendars

The application “Calendar.app”, as far as I noticed, suffers from one incurable disease - it is impossible to get the UID of its calendars in the usual way (ie, through the uid property).

Considering also that this application allows calendars with the same name, getting the UID of the calendars becomes essential.

The following script gets the UID indirectly, but completely accurate:


set TheCalendarName to "NewCal2"

tell application "Calendar"
	set newCalendar to make new calendar with properties {name:TheCalendarName}
	try
		set theID to uid of newCalendar
		return theID
	on error number -10000 -- error "AppleEvent handler failed"
		try
			newCalendar as text
		on error errorMessage
			set ATID to AppleScript's text item delimiters
			set AppleScript's text item delimiters to {"Can’t make «class wres» id \"", "\" of application \"Calendar\" into type text."}
			set theID to text item 2 of errorMessage
			set AppleScript's text item delimiters to ATID
			return theID
		end try
	end try
end tell

--> "6BF38A0A-F85D-4F18-B9A3-E293B56F92E4"