Create Reminders from Mail

Hi,

I know this has been done to some degree before, but attached is my (first ever) script for creating a Reminder from within Mail. It builds on other people’s effort of course. However, there’s a couple of things that I’d like it to do, which I just can’t seem to nail;

  1. when I use it, sometimes the result shows a very neat “Show in Mail” link in the body of the Reminder. But other times, and it seems it’s specific to the Sender of the email, I get a ‘message://3c<>’ unlinked body. I can’t figure out why - any help would be appreciated.
  2. I’ve got 2 dialogs in this script - Select the Reminder List, and Enter the Task Text. It would be nice to have once dialog with both fields on it, and a due date with a proper Calendar control - is this possible at all?
    It could probably use a general tidy up - it’s been ages since I’ve coded.

Thanks,
E.


on run {input, parameters}
	
	--Get Info from Mail
	tell application "Mail"
		set theSelection to selection
		set theMessage to item 1 of theSelection
		set theurl to "message://%3c" & theMessage's message id & "%3e"
		set input to theMessage's subject
	end tell
	
	--Get the List to Save it to
	activate application "Reminders"
	tell application "Reminders"
		set todo_lists to get every list
		-- Reminder has a number of list across accounts, present them all
		set numList to {}
		set chosen to {}
		
		#	set list_list to ""
		repeat with k from 1 to length of todo_lists
			tell list k
				if k > 9 then
					set row_number to k
				else
					set row_number to "0" & k
				end if
				set end of numList to my row_number & ". " & name
			end tell
		end repeat
		
		--Choose the List where the reminder will be created
		set choices to choose from list numList with prompt "Hold the Command key down to make multiple choices." with multiple selections allowed
		-- Extract the numbers only from the choices found in list "choices".
		repeat with j from 1 to count choices
			tell choices to set end of chosen to (text 1 thru ((my (offset of "." in item j)) - 1) of item j) as integer
		end repeat
		
		-- Return a list of item numbers
		set k to item 1 of chosen
		set myList to list k #{text 1 thru 1 of chosen_list}
		
		--Get the Task Title
		display dialog "What is the next Task?" default answer input
		set task_desc to text returned of result
		
		--Create the Reminder
		set newremin to make new reminder with properties {name:task_desc, body:theurl, container:myList}
	end tell
	
	activate application "Mail"
	return input
end run


Model: MBA
Browser: Safari 537.36
Operating System: Mac OS X (10.8)

Hi. Welcome to MacScripter.

I don’t know what’s causing your link problem (I don’t use Mail or Reminders myself), but two things to check would be:

  1. Does the part of the sender’s address used in the message ID contain any characters which need be converted to “%”+hex form?
  2. Does concatenating the reference ‘theMessage’s message id’ into theurl always work? Does changing the line to .
set theurl to "message://%3c" & (get theMessage's message id) & "%3e"

. improve things?

If you had more than nine todo lists, you’d hit a problem with the row number, which you haven’t coerced to text. (You have to do this to ensure that ‘row_number & ". " & name’ a few lines later produces text and not a list):

if k > 9 then
	set row_number to k as text -- Explicitly coerce the integer to text.
else
	set row_number to "0" & k -- The integer's automatically coerced to text when concatenated to text.
end if
set end of numList to my row_number & ". " & name -- The concatenation begins with text, so its result's text.

Less importantly, your ‘choose from list’ command specifies ‘with multiple selections allowed’, which is apparently unnecessary, since you only use the first item selected. But you should ideally check for ‘choices’ being ‘false’, which is what ‘choose from list’ returns when its “Cancel” button’s clicked:

set choices to choose from list numList with prompt "Hold the Command key down to make multiple choices." with multiple selections allowed
if choices is false then error number -128 -- Explicitly generate the "User canceled" error, as other dialogs do anyway when "Cancel"'s clicked.

-- Otherwise, 'choices' is a list containing the selected item(s).

‘choose from list’ and ‘display dialog’ can’t be combined in vanilla AppleScript. Reminders’s ‘reminder’ objects have a ‘due date’ property, but I don’t understand that part of your question.