Skip to content

Instantly share code, notes, and snippets.

@dingyi
Forked from tylermumford/com.apple.mail.scpt
Created January 26, 2013 00:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dingyi/4639127 to your computer and use it in GitHub Desktop.
Save dingyi/4639127 to your computer and use it in GitHub Desktop.
(*
The Hit List Mail Import
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/
Modified 2013 by Tyler Wayne. No rights reserved.
*)
################################################
# Change to specify any tags that should be added to the imported task
property TaskTags : ""
# 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 : 0
# 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 : -1
# 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 taskTitle to "Handle email \"" & (subject of theMessage) & "\" from " & my name_extract(sender of theMessage)
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 Email"
-- 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 msgDate + 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
# Extract name from sender string
# Expected form: Name <email@domain.com>
on name_extract(sender_string)
set address_start to offset of "<" in sender_string
if address_start is not less than 3 then
return text 1 thru (address_start - 2) of sender_string
else
return sender_string
end if
end name_extract
# For testing in AppleScript Editor
on run
import()
end run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment