-
-
Save tzarskyz/4491121 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--Script for setting Reminders for LaunchBar and Alfred | |
--For Alfred, Applescript must NOT be set to run in Background otherwise date parsing does not work | |
--For LaunchBar, place the script in ~/Library/Scripts/LaunchBar | |
--by Michelle L. Gill, 10/07/2012 | |
--inspired by https://gist.github.com/3187630 | |
--Notes on valid reminders | |
--Order of the text must be "title #note $list @date time" but note and list are optional. Date and time are also optional but both must be included if one is included | |
--Times can be in 12- or 24-hour format, and 24-hour format is assumed if am/pm are absent | |
--Times can include minutes and seconds (colon separated like hours:minutes:seconds) but minutes and seconds aren't required | |
--Numerical dates are listed as month/day/year and year isn't required | |
--Relative days such as "Today" and "Tomorrow" can be used, as well as days of the week | |
--Relative days are case instensitive and can be abbreviated, i.e. "mon" for "Monday" | |
--If the day of the week is entered as "Sunday" and today is Sunday, 7 days from today will be returned | |
--"Next" can be added to return an additional seven days from the next day of the week | |
--Examples of valid reminders: | |
--Buy Milk #note about milk $Personal @10/7/2012 5:00:00 PM | |
--Buy Milk #note about Milk @10/7/2012 5:00:00 PM | |
--Buy Milk $Personal @10/7/2012 5:00:00 PM | |
--Buy Milk @10/7/2012 5:00:00 PM | |
--Buy Milk @10/7/12 5:00:00 PM | |
--Buy Milk @10/7/12 5:00:00 pm | |
--Buy Milk @10/7/12 5:00:00pm | |
--Buy Milk @10/7/12 5:00pm | |
--Buy Milk @10/7/12 5pm | |
--Buy Milk @10/7 5pm | |
--Buy Milk @Tomorrow 5pm | |
--Buy Milk @tomorrow 5pm | |
--Buy Milk @tom 5pm | |
--Buy Milk @Sunday 5pm this returns 10/14/2012 if today is Sunday, 10/7/2012 | |
--Buy Milk @sunday 5pm this returns 10/14/2012 if today is Sunday, 10/7/2012 | |
--Buy Milk @sun 5pm this returns 10/14/2012 if today is Sunday, 10/7/2012 | |
--Buy Milk @Next Sunday 5pm this returns 10/21/2012 if today is Sunday, 10/7/2012 | |
--Buy Milk @next sun 5pm this returns 10/21/2012 if today is Sunday, 10/7/2012 | |
--Buy Milk @today 17 this assumes time is in 24-hour format (i.e. 5pm) | |
--Buy Milk @today 5 this also assumes time is in 24-hour format (i.e. 5am) | |
--Buy Milk @5 this also assumes time is in 24-hour format (i.e. 5am) | |
--Buy Milk @5pm | |
--set this to the name of the default Reminders list | |
property myDefaultList : "Personal" | |
on alfred_script(str) | |
handle_string(str) | |
end alfred_script | |
on handle_string(str) | |
--separate the time string and get the value | |
set arrayWithDate to my theSplit(str, "@") | |
if arrayWithDate's length > 1 then | |
set theDate to my parseDate(getArrayValue(arrayWithDate, 2)) | |
end if | |
--separate the Reminders list name if present | |
set arrayWithListName to my theSplit(getArrayValue(arrayWithDate, 1), "$") | |
if arrayWithListName's length > 1 then | |
set listName to my getArrayValue(arrayWithListName, 2) as string | |
else | |
set listName to myDefaultList | |
end if | |
--separate the note for the reminder if present | |
set arrayWithBody to my theSplit(getArrayValue(arrayWithListName, 1), "#") | |
if arrayWithBody's length > 1 then | |
set reminderBody to my getArrayValue(arrayWithBody, 2) | |
else | |
set reminderBody to "" | |
end if | |
set reminderName to getArrayValue(arrayWithBody, 1) | |
tell application "Reminders" | |
set listName to list listName | |
try | |
set newReminder to make new reminder with properties {name:reminderName, container:listName, body:reminderBody, remind me date:theDate} | |
on error | |
set newReminder to make new reminder with properties {name:reminderName, container:listName, body:reminderBody} | |
end try | |
end tell | |
end handle_string | |
on theSplit(theString, theDelim) | |
set oldDelimiters to AppleScript's text item delimiters | |
set AppleScript's text item delimiters to theDelim | |
set theArray to every text item of theString | |
set theTrimmedArray to {} | |
repeat with currentSplitString in theArray | |
if currentSplitString as string is not equal to "" then | |
set currentTrimmedString to trim(currentSplitString) | |
copy currentTrimmedString to the end of theTrimmedArray | |
end if | |
end repeat | |
set AppleScript's text item delimiters to oldDelimiters | |
return theTrimmedArray | |
end theSplit | |
on getArrayValue(array, location) | |
return item location in array | |
end getArrayValue | |
on parseDate(theDateStr) | |
set theDate to current date | |
set time of theDate to 0 | |
--parse the time | |
--first check for am/pm | |
set theDateStr to getArrayValue(theSplit(theDateStr, "am"), 1) as string | |
set theDateStr to getArrayValue(theSplit(theDateStr, "AM"), 1) as string | |
set theDateStrOrig to theDateStr | |
set theDateStr to getArrayValue(theSplit(theDateStr, "pm"), 1) as string | |
set theDateStr to getArrayValue(theSplit(theDateStr, "PM"), 1) as string | |
if theDateStr is not equal to theDateStrOrig then | |
set time of theDate to (time of theDate) + (12 * 3600) | |
end if | |
set theDateTimeArray to theSplit(theDateStr, " ") | |
--now get the hours/min/sec | |
set theTimeStr to getArrayValue(theDateTimeArray, -1) as string | |
set theTimeArray to theSplit(theTimeStr, ":") | |
try | |
set time of theDate to (time of theDate) + (item 1 of theTimeArray) * 3600 | |
set time of theDate to (time of theDate) + (item 2 of theTimeArray) * 60 | |
set time of theDate to (time of theDate) + (item 3 of theTimeArray) | |
end try | |
--now parse the date if present | |
try | |
set theDateArray to items 1 thru -2 of theDateTimeArray | |
set theDateArrayLen to the count of items in theDateArray | |
--first see if this is a relative date involving "next" | |
if theDateArrayLen = 2 then | |
set theRelativeDateStr to getArrayValue(theDateArray, 1) as string | |
set theWeekdayStr to getArrayValue(theDateArray, 2) as string | |
if "next" is in theRelativeDateStr then | |
--setting forward 8 days which prevents one week from today being returned for "next sunday" if today is sunday | |
set time of theDate to (time of theDate) + (8 * 24 * 3600) | |
end if | |
set theDate to getWeekday(theDate, theWeekdayStr) | |
else | |
set theDateArraySplit to theSplit(getArrayValue(theDateArray, 1), "/") | |
set theDateArraySplitLen to the count of items in theDateArraySplit | |
if theDateArraySplitLen = 1 then | |
set theRelativeDayStr to getArrayValue(theDateArraySplit, 1) as string | |
if "tod" is in theRelativeDayStr then | |
set theDate to theDate | |
else if "tom" is in theRelativeDayStr then | |
set time of theDate to (time of theDate) + (24 * 3600) | |
else | |
--this prevents today's date from being returned if current day of week is entered | |
set time of theDate to (time of theDate) + (24 * 3600) | |
set theDate to getWeekday(theDate, theRelativeDayStr) | |
end if | |
else | |
set month of theDate to (item 1 of theDateArraySplit) as integer | |
set day of theDate to (item 2 of theDateArraySplit) as integer | |
try | |
set theNewYear to (item 3 of theDateArraySplit) as integer | |
if theNewYear < 100 then | |
set theCurrentYear to (year of theDate) as integer | |
set theNewYear to theNewYear + ((theCurrentYear / 100) as integer) * 100 | |
end if | |
set year of theDate to theNewYear | |
end try | |
end if | |
end if | |
end try | |
return theDate | |
end parseDate | |
on getWeekday(theDate, theWeekdayStr) | |
repeat until theWeekdayStr is in ((weekday of theDate) as string) | |
set time of theDate to (time of theDate) + (24 * 3600) | |
end repeat | |
return theDate | |
end getWeekday | |
on trim(someText) | |
--Trimming subroutine from: http://macscripter.net/viewtopic.php?id=18519 | |
repeat until someText does not start with " " | |
set someText to text 2 thru -1 of someText | |
end repeat | |
repeat until someText does not end with " " | |
set someText to text 1 thru -2 of someText | |
end repeat | |
return someText | |
end trim |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment