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


Click here to return to the 'Use iCal to set a response reminder for a Mail message' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Use iCal to set a response reminder for a Mail message
Authored by: mark hunte on Jul 06, '05 01:24:33PM
Ok got this so far, but if the mail is moved it will not be found again as it gets a different number.. One way around this could be to move the mail into a special reminder mailbox first with the script.
tell application "Mail"
	set selectionMessage to selection
	set thisMessage to item 1 of selectionMessage
	set theId to id of thisMessage as string
	set the_search to do shell script "mdfind \"kMDItemKind == 'emlx' \"| grep -iw " & theId & ".emlx" & " | grep -v grep "
	set theUrl to "file://" & the_search as string
end tell

---
mh

[ Reply to This | # ]

Use iCal to set a response reminder for a Mail message
Authored by: mark hunte on Jul 06, '05 07:39:18PM
This seems to work, I created a mailbox called 'reminders' for the messages to be either
moved or duplicated to. The script shows how to get the url for the email , which can then be place in the url of the iCal event.
I will post an updated version of the full script later with this included. ( i need to go to bed)
tell application "Mail"
	set theCount to ""
	set selectionMessage to selection
	set thisMessage to item 1 of selectionMessage
	
	set the_mailbox to mailbox "reminders"
	set new_place to duplicate thisMessage to the_mailbox -- you can change duplicate to move
	set theCount to count messages in the_mailbox --this is just to get the path to the new mailbox
	set themessage to message theCount of the_mailbox -- this is just to get the path to the new mailbox
	set theId to id of themessage as string
	using terms from application "Finder"
		set the_Url to do shell script "mdfind \"kMDItemKind == 'emlx' \"| grep -iw " & theId & ".emlx" & " | grep -v grep " as string
		set the_delim1 to do shell script "echo " & quoted form of the_Url & " " & " |awk -FMessages '{print $1}'"
		set copy_folder to the_delim1 & "Messages/"
		set the_Url to "file://" & copy_folder & (do shell script "cd " & quoted form of copy_folder & " ; ls -t1| perl -pe 'exit if $. > 1'")	
	end using terms from
	
end tell

---
mh

[ Reply to This | # ]

Use iCal to set a response reminder for a Mail message
Authored by: zmagyar on Jul 07, '05 04:51:37AM
Hi Mark, this is starting to be interesting :-)) But is widening the discussion too, so let me know if you start to get bored, either/or prefer that we disccuss this directly in private mails and post only the results to forum... The URL thing is very cool, I overlooked this too, and it does solve the problem I hit yesterday, but cuts the functionality, but let me go one by one. Your approach with URL solves my latest problem and this is how to open this damn emlx file. Finder does not want to open it (or I'm doing something wrong), mail and message viewer do open it but show only an empty vidow with a b..it note that the message was not downloaded from the server... just try to add
 open the_search 
at the last line of your example code and you will see what do I mean. I went then into automator which opens it too but then we are getting tangled in AS - Automator calls and this does not go smoot either, so we better skip it. Why is this so complicated when a simple double click from finder works, I have no idea.... But with URL approach you miss one functionality I implemented and this is updates. When I have an update to the event, like they guy replied that he will answer in 3 days, then I write an update into the ical event, postpone it for 3 days, and in the update I write a new id with which I can open the refering email message. With the URL approach I could add the URL to the even text, but first the URL is long, second it will not work with a click. I could replace the original URL but the I would lose the reference to the first email... Also, there is one big difference between yours and mine, you search for the path at the moment of creation of the event, and I want to log only the ID and search for it at the moment of opening the emlx file, which overcomes the reminder mailbox problem. So now we can go two ways, one is yours, URL in event, no additional links to update emails, this makes the script much simpler, or mine but then we should figure out how to open an emlx file from applescript. As I said let me know when you had enough. For me it is kind of a challenge and therefore fun, but maybe you or others simply want a working solution no matter how good it is. BTW I have no idea why the mdfind takes so long, and maybe you can shorten it by using the -onlyin option. Let me know, Zsolt

[ Reply to This | # ]
Use iCal to set a response reminder for a Mail message
Authored by: roballen on Jul 07, '05 06:09:40AM

Hi

I have been following this thread with interest, but with no apple scripting skill. One additional idea/request. An option to create the reminder as a to-do instead of a calendar event?

Thanks, Rob



[ Reply to This | # ]
Use iCal to set a response reminder for a Mail message
Authored by: zmagyar on Jul 07, '05 11:15:59AM

It is not a problem to code it (I guess) but to make an appropriate dialog for selection, as the AS does not support radio buttons or checkmarks. This means you would need then two separate dialogs and that is not very ellegant, but again doable. Let us sort out the current problems and we might come back to this later on. Thanks for input.
Zsolt



[ Reply to This | # ]
Use iCal to set a response reminder for a Mail message
Authored by: zmagyar on Jul 08, '05 12:03:16PM
OK, I managed to put it together today, so you might have some fun over the weekend.
Notes:
The search for the file is rather slow, I have no idea why is mdfind so slow on my machine considering it should search in index, but probably this can be fixed somehow, at the moment it takes on my machine about 5-10 seconds to find the file, and you will have absolutely no visual feedback that something is going on, unless you monitor your CPU or disk activity. A nice progress bar, or instant search would be nice. As I'm still wandering how to open an email file from AS, to give you a working code, the place where the files should open is just displaying a dialog with a file path. But as soon as you figure out how to open the file, just replace 3 lines of code and you are done.
Here is the script, at the end I will paste the usage....

--NOTE: the events will be created in the callender named "MailFollowup".
--it is hardcoded, if you don't like it, change it in the code ;-)
--for insturctions see Script Description

display dialog "Please Choose:" buttons {"Create Event", "Get ID", "Open Mail with ID"} default button 2 with icon 1
if button returned of result = "Create Event" then
	my Create_Event()
else if button returned of result = "Get ID" then
	my Get_ID()
else if button returned of result = "Open Mail with ID" then
	my Open_Mail_with_ID2()
else
	display dialog "Something went wrong"
end if

on Create_Event()
	set messInfo to (my Get_Mess_Info2())
	set messInfo to messInfo as list
	set messSubject to item 1 of messInfo
	set mailMessID to item 2 of messInfo
	
	tell application "iCal"
		activate
		set theCal to 0
		set callist to {}
		--repeat with i in (calendars whose writable is true)
		repeat with i in calendars
			set caltitle to title of i
			set callist to callist & caltitle --(title of i)
		end repeat
	end tell
	
	--set thecal to item 1 of (choose from list callist)
	set theCal to "MailFollowup"
	repeat with i from 1 to (count callist)
		if theCal = item i of callist then
			exit repeat
		end if
	end repeat
	set theCal to i
	
	tell application "iCal"
		calendar theCal exists
	end tell
	
	tell application "iCal"
		--	make new event at the end of events of calendar thecal with properties {start date:startdate, end date:enddate, summary:thetitle, description:descript}
		set newEvent to (make new event at the end of events of calendar theCal with properties {start date:(current date), end date:(current date), summary:messSubject, description:"Message ID: " & mailMessID})
		show newEvent
		--current date
	end tell
end Create_Event

on Get_ID()
	set messInfo to (my Get_Mess_Info2())
	set messSubject to item 1 of messInfo
	set mailMessID to item 2 of messInfo
	set the clipboard to "MMID " & mailMessID
	
	tell application "iCal"
		activate
	end tell
end Get_ID

on Open_Mail_with_ID2()
	tell application "Mail"
		activate
		set slectedMessmyMessIDs to (askPassword "Enter Message ID")
		--this is the part where we search for the email message file
		--the search is slow and CPU intensive, it has to run better
		set the_search to do shell script "mdfind \"kMDItemKind == 'emlx' \"| grep -iw " & slectedMessmyMessIDs & ".emlx" & " | grep -v grep "
		--here we should figure out how to open the found message file
		--as at the moment I don't know how, I simply display the path and file name
		--this shows if the code is working fine and you might even want to manually
		--navigate to this path and open it by doubleclicking
		--of course this is more then akward
		set the_search to the_search as string
		display dialog the_search
	end tell
end Open_Mail_with_ID2

------------------------------------------------------------------------------------------------

on Get_Mess_Info2()
	tell application "Mail"
		activate
		--set messageSelection to selected messages of message viewer 1
		--the line above is OK if you have only one message viewer open, but if you have
		--two or more then we have to find the front one...
		set mywin to window of the message viewers
		set mymsgviewers to message viewers
		set winindex to index of item 1 of mywin
		if winindex is equal to 1 then
			set frontmsgview to item 1 of mymsgviewers
		else
			set frontmsgview to item 2 of mymsgviewers
		end if
		set messageSelection to selected messages of frontmsgview
		if (count of messageSelection) is equal to 0 then
			display dialog "Please select a message in Mail first, then run this script again."
		else if (count of messageSelection) is equal to 1 then
			--geting the reference to the selected message
			set selectedMessage to item 1 of messageSelection
			set messSubject to subject of selectedMessage
			if messSubject is equal to "" then
				display dialog "Mail subject equals empty string! continuting..."
			end if
			set mailMessID to id of selectedMessage
			if mailMessID is equal to 0 then
				display dialog "Mail ID equals 0, something went wrong, exiting..."
				exit repeat
			end if
			return {messSubject, mailMessID}
		else
			display dialog "Please select only one message in Mail first, then run this script again."
		end if
	end tell
	return
end Get_Mess_Info2
Usual disclamer apply!!!
Usage:
Save the file in the "~/Library/Scripts/Mail Scripts", you can then invoke it thru the script menu.
I named it MailCalV7___ctl-e.app, in Jaguar this would let you invoke it with ctrl-e in the Mail, in Tiger I'm not sure if this works.
There are 3 ways to use it:
- create event: this will find the unique ID of the selected mail message and create a new event in iCal taking the mail subject as the title of the event, and the current date and time as the date and time of the event, it will also add string "MMID" and the ID of the message in the Notes of the event, where the MMID stands for Mail Message ID. Then it will take you to the event so that you can modify the date time and add some more comments
- get id: the idea is to get the ID of the selected message which is a followup to an already created event, copy it into the clipboard and take you to the iCal. Then you can find the event you want to update, select the notes and hit ctrl-v to paste the new MMID, then you can add comments as followup notes to the event
- open message with ID: this is when you want to actually see the content of the mail message you are referring to in the even notes. You check in the event which MMID you want to see, then start this script, select the option to open a message with ID, enter the ID manually, (sorry for bullets, I dont' know of any other way to enter text) and once you hit OK the mail message should appear. At this moment this will just show a dialog with a message path.
Have fun, Zsolt

[ Reply to This | # ]
Use iCal to set a response reminder for a Mail message
Authored by: blueeye on Jul 11, '05 05:16:26PM

To avoid the delay caused by shell script mdfind, I found the following script much faster in producing a URL of the selected mail message, identified as variable theURL, to hand off to iCal.

property LibraryPath : (path to library folder from user domain) as string
property MailBoxPath : LibraryPath & "Mail:Mailboxes:"

tell application "Mail"
tell message viewer 1
set MailBoxName to ""
set b to item 1 of (get selected mailboxes)
set i to 1
try
repeat while class of b is mailbox
set {n, b} to {name, container} of b
if i is 1 then
set MailBoxName to n
else
set MailBoxName to n & ":" & MailBoxName
end if
set i to i + 1
end repeat
end try
end tell
set theMessages to selection --assuming that a message has been selected
repeat with eachMessage in theMessages
set msgID to id of eachMessage
end repeat
set URLPath to (MailBoxPath & MailBoxName & ".mbox:Messages:" & msgID & ".emlx")
tell application "System Events" to set theURL to URL of alias (URLPath)
end tell



[ Reply to This | # ]
Use iCal to set a response reminder for a Mail message
Authored by: mark hunte on Jul 11, '05 07:50:33PM
I do not seem to have any delay on my Mac using the mdfind, but i will try you script out,, cheers

also here is a way to jump to the event of the selected email. that zmagyar was
after using the id of the email, again I have been tied up so hav not had time to rewrite all of this into
on whole script.

but using these bits the script will now make the event reminder with a working
Url. and also be able to jump from a email to its event if it exists.

I will write it up soon, as I am sure others will, to their preferred version.

tell application "Mail"
	set theMessages to selection
	repeat with eachMessage in theMessages
		set theId to id of eachMessage as string
	end repeat
end tell

tell application "iCal"
	
	repeat with aCalendar in calendars
		tell aCalendar
			set the_none to "0"
			if exists (some event whose description contains theId) then
				
				set theEvent to (some event whose description contains theId)
				activate
				show theEvent
			else
				set the_none to "no match found"
			end if
			
		end tell
	end repeat
end tell
if the_none is equal to "no match found" then
	display dialog the_none
end if 

---
mh

[ Reply to This | # ]