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

10.5: An AppleScript to link iCal events to Mail messages Apps
I use mail and iCal at work, and often need to refer to agendas and other files which were sent by email. The new Message ID URL allows you to link to an email using the URL property of an iCal event. However, this involves displaying the header, copying and pasting.

The following AppleScript automates the process. Just select the email message, change to iCal, create or select the event, then run the script. You can then move the email out of your inbox and retrieve it later by a single click on the url link in the iCal event.
-- Script to read message id from selected mail message and put into iCal
-- Copyright 2007 G W Aylward

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 myCal to calendar "Bill regular"
    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
[robg adds: This worked as described when I tested it. To make it easy to run, save it to your user's Library/Scripts/iCal folder (create the folders as necessary).]
    •    
  • Currently 3.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[16,475 views]  

10.5: An AppleScript to link iCal events to Mail messages | 8 comments | Create New Account
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.
Two Alternatives
Authored by: derek_beatty on Dec 19, '07 08:21:01AM

A similar capability is available via drag-and-drop: Create an event in iCal, then drag message header into URL field of the event's Edit pop-up.

Also, if you use the Apple Data Detectors pop-up menu to create an event, it will be automatically linked to the message.



[ Reply to This | # ]
10.5: An AppleScript to link iCal events to Mail messages
Authored by: maczen on Dec 19, '07 08:31:27AM

It is easier than that even.
Just grab the message in the preview (upper) pane of mail. Drag it, I use expose to get to my iCal window easily, and drop it on the appropriate day / time slot. You can then edit the new entry and the "Show in Mail" link will work as expected.

It would be nice to be able to drop onto an existing event, but currently the edit info window needs to already be open on the existing event. Seems to be a little bit of oversite by Apple.



[ Reply to This | # ]
10.5: An AppleScript to link iCal events to Mail messages
Authored by: SeanAhern on Sep 06, '12 10:07:24AM

This seems to have been broken in the intervening 5 years. Pity.



[ Reply to This | # ]
Can't make this work
Authored by: jlftrsh on Dec 19, '07 06:07:13PM

I can't seem to get email messages attached to events, whether by this script or by dragging onto the URL field in the iCal edit box. I select the email in mail, switch to iCal and select the event, then run the script, which generates a brief chime sound. But then if I subsequently click on the "Show in Mail..." link in the iCal edit box, I get an error from Mail that says "No associated application can be found". It doesn't seem like the message gets attached properly.



[ Reply to This | # ]
Can't make this work
Authored by: jonro on Dec 20, '07 07:47:04AM

I can't make it work either and when I drag an email to an event's URL, it looks like it works, but then disappears from the event after I press done.



[ Reply to This | # ]
10.5: An AppleScript to link iCal events to Mail messages
Authored by: quantumalpha on Dec 21, '07 01:54:35PM

I'm a bit new to AppleScripts... I've copied the script into Script Editor and saved it to ~/Library/Scripts/iCal (i made the Scripts/iCal folders), but once in iCal, where do I go to actually run the script? Is there a menu option or button I'm not seeing?



[ Reply to This | # ]
10.5: An AppleScript to link iCal events to Mail messages
Authored by: gaylward on Dec 22, '07 10:10:34AM

Run Applescript Utility (in /Applications/Applescript/) and there is a check box to display your scripts folder in the menu bar



[ Reply to This | # ]
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 | # ]