-- 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