dingyi
1/26/2013 - 12:23 AM

gistfile1.applescript

(*

  The Hit List Mail Import (customized for shorter and concise subjects by TB)

	Copyright © 2012 Potion Factory LLC.
	Licensed under a Creative Commons Attribution 3.0 License: http://creativecommons.org/licenses/by/3.0/


	To customize this script, copy it first to:

		Your Home Folder/Library/Application Support/The Hit List/Import Scripts/

	If The Hit List is running inside a sandbox copy it to:

		Your Home Folder/Library/Containers/com.potionfactory.TheHitList/Data/Library/Application Support/The Hit List/Import Scripts/

*)

################################################

# Change to specify any tags that should be added to the imported task
property TaskTags : "@email @work"

# Set start date of the task to n days from the date the email was received. Set to -1 to not set a start date.
property TaskStartInDays : 1

# Set due date of the task to n days from the date the email was received. Set to -1 to not set a due date.
property TaskDueInDays : 2

# The priority of the task. 1 to 9, 0 being no priority.
property TaskPriority : 0

# Estimated time of the task. Example values: 5m, 1h, 1d.
property TaskEstimatedTime : ""

# Number of characters from the email body to include in the task notes
property NoteMessageBodyLength : 500

################################################

on import()
	try
		tell application "Mail"
			set theResult to {}
			set selectedMessages to the selection
			
			repeat with theMessage in selectedMessages
				-- Configure the task title
				set senderText to sender of theMessage
				if length of words of senderText ≥ 2 then
					set senderText to words 1 thru 2 of senderText
				end if
				
				set subjectText to subject of theMessage
				if length of subjectText ≥ 24 then
					set subjectText to rich text 1 thru 24 of subjectText
				end if
				
				set taskTitle to "Handle email \"" & (subjectText) & "\" from " & (senderText)
				if length of TaskTags > 0 then
					set taskTitle to taskTitle & " " & TaskTags
				end if
				
				-- Configure the email URL
				set messageURL to "message://" & "%3c" & my url_escape(message id of theMessage) & "%3e"
				
				set urlTitle to "Link to Message"
				
				-- Calculate start and due dates
				set msgDate to date received of theMessage
				
				if TaskStartInDays ≥ 0 then
					set taskStartDate to msgDate + TaskStartInDays * days
				else
					set taskStartDate to null
				end if
				if TaskDueInDays ≥ 0 then
					set taskDueDate to (current date) + TaskDueInDays * days
				else
					set taskDueDate to null
				end if
				
				-- Create the record that will be passed on to The Hit List
				set taskDict to {title:taskTitle, |url|:messageURL, urlTitle:urlTitle, startDate:taskStartDate, dueDate:taskDueDate, priority:TaskPriority, estimatedTime:TaskEstimatedTime}
				
				copy taskDict to the end of theResult
			end repeat
			
			return theResult
		end tell
	on error
		return {}
	end try
end import


# Handler to percent escape encode text
on encode_char(this_char)
	set the ASCII_num to (the ASCII number this_char)
	set the hex_list to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
	set x to item ((ASCII_num div 16) + 1) of the hex_list
	set y to item ((ASCII_num mod 16) + 1) of the hex_list
	return ("%" & x & y) as string
end encode_char


# URL Escape
on url_escape(this_text)
	set the acceptable_characters to "abcdefghijklmnopqrstuvwxyz0123456789$+!/?;&@=#~.-_"
	set the encoded_text to ""
	repeat with this_char in this_text
		if this_char is in the acceptable_characters then
			set the encoded_text to (the encoded_text & this_char)
		else
			set the encoded_text to (the encoded_text & encode_char(this_char)) as string
		end if
	end repeat
	return the encoded_text
end url_escape


# For testing in AppleScript Editor
on run
	import()
end run