Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the '10.5: An AppleScript to link iCal events to Mail messages' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.5: An AppleScript to link iCal events to Mail messages
Authored by: pobs on Jul 09, '09 08:48:30AM

I know that this is a pretty dated link but I modified the code a little so I thought I would respond for those of you who happen upon this very useful feature of 10.5. I use many calendars to keep jobs and activities separate and to publish only some. I found this to be a very useful script for keeping my email organized along with that hierarchy but I needed something to select the right calendar so the script would complete. The original script hard coded the calendar so that was a problem.

Since I could not find a way to get the 'current' calendar. I added a select box of all calendars that you can select from.

HTH - pobs


-- Script to read message id from selected mail message and put into iCal
-- Copyright 2007 G W Aylward
-- Modded 2009 S G P

tell application "Mail"
	set selectedMessages to selection
	set selectionCount to (count of selectedMessages)
	
	-- Check that only one message is selected
	if selectionCount is greater than 1 then
		display dialog "Only one message should be selected"
	else if selectionCount is equal to 0 then
		display dialog "No messages selected"
	else
		set theMessage to item 1 of selectedMessages
		set messageid to message id of theMessage
		
		-- Make URL (must use URL-encoded values for "<" and ">")
		set urlText to "message:" & "%3c" & messageid & "%3e"
		
		-- Insert into currently selected event in iCal
		my insertURL(urlText)
	end if
	
end tell

-- Function to insert a URL into an iCal event (calendar choice hard-coded)
-- Uses system events as workaround for iCal bug (no selection property!)
on insertURL(urlText)
	tell application "iCal" to activate
	delay 1
	-- 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
		set myClipboard to the clipboard
		--set calResult to text returned of (display dialog "Which calendar is the selected event in?" default answer "" buttons {"OK"} default button 1)
		set all_calendars_names to the name of every calendar
		choose from list all_calendars_names with prompt "Which calendar is the selected event in?" with title "Calendar Mail-Linker"
		if result is not false then
			set calResult to item 1 of result
			if calResult = "Birthdays" then
				display dialog "Can't Attach Mail To The Birthdays Calendar" buttons {"Quit"}
				return
			end if
		else
			return
		end if
		set myCal to calendar calResult
		set myEvent to (first event of myCal whose summary is myClipboard)
		-- Set URL and open event
		set url of myEvent to urlText
		show myEvent
		
	end tell
end insertURL


[ Reply to This | # ]