Skip to content

Instantly share code, notes, and snippets.

@aminalhazwani
Last active July 8, 2018 21:00

Revisions

  1. aminalhazwani renamed this gist Jul 8, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. aminalhazwani created this gist Jan 8, 2015.
    169 changes: 169 additions & 0 deletions create-reminder.action
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,169 @@
    # Set this according to your email accounts names and Reminders list name
    set firstMailAccount to "Google"
    set secondMailAccount to "Yahoo"
    set thirdMailAccount to "iCloud"
    set RemindersList to "Need Reply"

    # Set Red Flag
    set FlagIndex to 0

    tell application "Mail"

    set theSelection to selection
    # Do nothing if no email is selected in Mail
    try
    set theMessage to item 1 of theSelection
    on error
    return
    end try

    set theSubject to theMessage's subject
    # Make sure reminder doesn't already exist so we don't create duplicates
    tell application "Reminders"

    set theNeedlesName to name of reminders whose name is theSubject and completed is false
    if theNeedlesName is not {} then
    # Make sure dialog is in focus when called as a service
    # without the below, Mail would be the active application
    tell me
    activate
    end tell
    set theButton to button returned of (display dialog "The selected email matches an existing reminder: " & theNeedlesName & ". Would you like to mark the reminder as complete and clear any remaining flags of this message?" with title "Create Reminder from E-Mail" buttons {"Mark complete", "Cancel"} default button 1)

    if theButton is "Mark complete" then
    tell application "Mail"
    # Unflag email/message
    set flag index of theMessage to -1
    end tell
    # Find correct reminder based on subject and mark as complete
    set theNeedle to last reminder whose name is theSubject and completed is false
    set completed of theNeedle to true
    return
    else if the_button is "Cancel" then
    return
    end if

    end if
    end tell

    # Present user with a list of follow-up times (in minutes)
    (choose from list {"Later Today", "This Evening", "Tomorrow", "This Weekend", "Next Week", "In a Month", "Someday"} default items "Later Today" OK button name "Create" with prompt "Set follow-up time" with title "Create Reminder from E-Mail")

    set reminderDate to result as rich text

    # Exit if user clicks Cancel
    if reminderDate is "false" then return

    if reminderDate = "Later Today" then
    # Add 2h to the current time of the same day
    set remindMeDate to (current date)
    set time of remindMeDate to (time of (current date)) + 7200

    else if reminderDate = "This Evening" then
    # Set time to 9:00pm of the same day
    set remindMeDate to (current date)
    set time of remindMeDate to 60 * 60 * 21

    else if reminderDate = "Tomorrow" then
    # Add 1 day and set time to 9:00am
    set remindMeDate to (current date) + 1 * days
    set time of remindMeDate to 60 * 60 * 9

    else if reminderDate = "This Weekend" then
    # This weekend means Saturday
    set currentWeekDay to weekday of (current date) as string
    if currentWeekDay = "Monday" then
    set remindMeDate to (current date) + 5 * days
    else if currentWeekDay = "Tuesday" then
    set remindMeDate to (current date) + 4 * days
    else if currentWeekDay = "Wednesday" then
    set remindMeDate to (current date) + 3 * days
    else if currentWeekDay = "Thursday" then
    set remindMeDate to (current date) + 2 * days
    else if currentWeekDay = "Friday" then
    set remindMeDate to (current date) + 1 * days
    # If it's Saturday set the reminder on Sunday
    else if currentWeekDay = "Saturday" then
    set remindMeDate to (current date) + 1 * days
    # If it's Sunday set the reminder Sunday evening 9:00pm
    else if currentWeekDay = "Sunday" then
    set remindMeDate to (current date)
    set time of remindMeDate to 60 * 60 * 21
    end if

    set time of remindMeDate to 60 * 60 * 9

    else if reminderDate = "Next Week" then
    # Set the reminder on Monday next week
    set currentWeekDay to weekday of (current date) as string
    if currentWeekDay = "Monday" then
    set remindMeDate to (current date) + 7 * days
    else if currentWeekDay = "Tuesday" then
    set remindMeDate to (current date) + 6 * days
    else if currentWeekDay = "Wednesday" then
    set remindMeDate to (current date) + 5 * days
    else if currentWeekDay = "Thursday" then
    set remindMeDate to (current date) + 4 * days
    else if currentWeekDay = "Friday" then
    set remindMeDate to (current date) + 3 * days
    else if currentWeekDay = "Saturday" then
    set remindMeDate to (current date) + 2 * days
    else if currentWeekDay = "Sunday" then
    set remindMeDate to (current date) + 1 * days
    end if

    set time of remindMeDate to 60 * 60 * 9

    else if reminderDate = "In a Month" then
    set remindMeDate to (current date) + 43200 * minutes

    else if reminderDate = "Someday" then
    # Set the reminder in 3 months
    set remindMeDate to (current date) + 129600 * minutes

    end if

    # Flag selected email/message in Mail
    set flag index of theMessage to FlagIndex

    # Get the unique identifier (ID) of selected email/message
    set theOrigMessageId to theMessage's message id

    # Encode % with %25 because otherwise the URL will be screwed up in Reminders and you won't be able to just click on it to open the linked message in Mail
    set theUrl to {"message:%3C" & my replaceText(theOrigMessageId, "%", "%25") & "%3E"}

    # Determine correct Reminder's list based on account the email/message is in
    if name of account of mailbox of theMessage is firstMailAccount then
    set RemindersList to RemindersList
    else if name of account of mailbox of theMessage is secondMailAccount then
    set RemindersList to RemindersList
    else if name of account of mailbox of theMessage is thirdMailAccount then
    set RemindersList to RemindersList
    else
    # Default list name in Reminders
    set RemindersList to "Reminders"
    end if

    end tell

    tell application "Reminders"

    tell list RemindersList
    # Create new reminder with proper due date, subject name and the URL linking to the email in Mail
    make new reminder with properties {name:theSubject, remind me date:remindMeDate, body:theUrl}
    end tell

    end tell

    # String replace function used to replace % with %25
    on replaceText(subject, find, replace)
    set prevTIDs to text item delimiters of AppleScript
    set text item delimiters of AppleScript to find
    set subject to text items of subject

    set text item delimiters of AppleScript to replace
    set subject to "" & subject
    set text item delimiters of AppleScript to prevTIDs

    return subject
    end replaceText