get selection in iCal

I am so sure (well kind of) that I have done this in the past.

I want to be able to get properties on a selected event in ical.

But I can not even get the selection to work first.

set the_selection to selection 

does not work nor anything else so far.
looking at the libray/defs. there does not seem to be a selection class ??

Hi Mark,

Correct, there is no selection property, but one workaround is to copy the selection. This gives you the summary and if it unique, then you can get the event. If it’s not unique, then you can try mathing it with the current date or something.

tell application “iCal” to activate
delay 3
tell application “System Events”
tell process “iCal”
keystroke return
keystroke “c” with command down
keystroke return
end tell
end tell
tell application “iCal”
activate
set cb to the clipboard
set home_cal to first calendar whose title is “Home”
set the_event to first event of home_cal whose summary is cb
end tell

When using macros, you need to be precise, so this is just an idea.

Edited: I forgot to change ‘with command down’ to ‘using command down’.

gl,

Kel what can I say, that will work a treat for what I need to do.
I Thanks You. :slight_smile:

I changed the calendar selection to just look at the one i Need, and also corrected the command down syntax
.

tell application "iCal" to activate
delay 3
tell application "System Events"
	tell process "iCal"
		keystroke return
		keystroke "c" using {command down}
		keystroke return
	end tell
end tell
tell application "iCal"
	activate
	set cb to the clipboard
	set home_cal to calendar "Homestuff"
	set the_event to first event of home_cal whose summary is cb
end tell

Hi Mark,

Glad You can use it. You don’t need the delay there. It’s just left over from when I was testing the keystrokes.

gl,

Nice workaround but surely it would be better to have a selection property within iCal. I hope that Apple is still actively improving Applescript and iCal and add this in a future release.

Perhaps the already existing hacks (e.g. iCalFix) could add the ability to get the selected event?

I was using this, but was noticing that I wasn’t getting the selected event because sometimes the event selected wasn’t in the home calendar. Is there a way to get the fore-most calendar to make this script work on any calendar?

BTW, I am using a script that utilizes the quick event feature in iCal, but I don’t know how to force it to add to the Home calendar, or else, I wouldn’t be asking this question.

Thanks,
Rob

Hi Rob,

This finds the calendar for the selected event:


tell application "Calendar" to activate
delay 2
-- assumes there is a selected event
tell application "System Events"
	tell process "Calendar"
		keystroke return
		keystroke "c" using command down
		keystroke return
	end tell
end tell
set event_summary to the clipboard
tell application "Calendar"
	set cal_list to every calendar
	repeat with this_cal in cal_list
		try
			set this_event to (first event of this_cal whose summary is event_summary)
			exit repeat
		end try
	end repeat
	set this_cal to contents of this_cal
	{name of this_cal, summary of this_event}
end tell
--> {"Phases of the Moon", "Full Moon (100%)"}

As you can see by the date of the post, I’m a little bit rusty. But, I think you need to ‘make’ new event at this_cal. See the make command in the Calendar dictionary. Write if you need more help.

gl,
kel

Yes. Thanks. Actually, I already worked it out this evening, much the same way. In fact, I added some extra to make it faster (when you have a ton of huge calendars). See below.

on get_selected_event()
	-- Get selected event summary into clipboard
	tell application "System Events"
		tell process "iCal"
			keystroke return
			keystroke "c" using {command down}
			keystroke return
		end tell
	end tell
	
	-- Now find event with that summary
	tell application "iCal"
		activate
		
		try
			--Let's first store that event we just copied
			set myClipboard to the clipboard
			--display dialog "Event's UID is: [" & uid of myClipboard & "]"
			
			--We're going to assume that the selected event is in the default calendar, which is a safe assumption in this overall script since we are using quickevent above to create an event and then get that event here so we can edit it. This will speed up the script.  If we're wrong, we'll search all the other calendars exhaustively.
			--So we need to get the default calendar
			
			--First, determine the default calendar's UID
			
			set defcalUID to first paragraph of (do shell script "defaults read com.apple.iCal CalDefaultCalendar")
			--display dialog "Default Calendar's UID is: [" & defcalUID & "]"
			
			--Then use that UID to obtain the calendar
			
			set defCal to (first calendar whose uid is defcalUID)
			--display dialog "Default Calendar's name is [" & name of defCal & "]]"
			
			--Now we can search for the event in the default calendar
			
			set myEvent to first event of defCal whose summary is myClipboard
			
		on error
			
			--Search all the calendars (slower)
			set TheCalendars to calendars
			
			set IsDefined to true
			repeat with anItem in TheCalendars
				try
					set myEvent to (first event of anItem whose summary is myClipboard)
				on error
					--display dialog "Event [" & myClipboard & "] is not in Calendar: [" & name of anItem & "]"
					set IsDefined to false
				end try
				if IsDefined then exit repeat
			end repeat
			
		end try
		
		return (myEvent)
	end tell
end get_selected_event