Skip to content

Instantly share code, notes, and snippets.

@tzarskyz
Created December 5, 2013 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tzarskyz/7810922 to your computer and use it in GitHub Desktop.
Save tzarskyz/7810922 to your computer and use it in GitHub Desktop.
property myDefaultList : ""
on alfred_script(q)
set qt to q as text
set itemList to extractList(qt)
set expandedQT to expandDelay(expandDay(removeList(qt)))
tell application "Reminders"
activate
tell application "System Events"
keystroke "t" using command down
keystroke "n" using command down
keystroke expandedQT & return
key code 53
end tell
if itemList is not equal to qt then
set defaultList to name of default list
set listOfItems to reminders in list defaultList whose completed is false
set createdItem to last item of listOfItems
tell list itemList
make new reminder with properties {name:(name of createdItem) as string, remind me date:(remind me date of createdItem) as date}
end tell
delete createdItem
end if
-- tell application "System Events"
-- set isRunning to (count of (every process whose bundle identifier is "com.Growl.GrowlHelperApp")) > 0
-- end tell
-- if isRunning then
-- set titleList to name of default list
-- if itemList is not equal to qt then
-- set titleList to itemList
-- end if
-- tell application id "com.Growl.GrowlHelperApp"
-- set the allNotificationsList to {"New Reminders Task"}
-- set the enabledNotificationsList to {"New Reminders Task"}
-- register as application ¬
-- "New Reminders Task" all notifications allNotificationsList ¬
-- default notifications enabledNotificationsList ¬
-- icon of application "Reminders"
-- notify with name ¬
-- "New Reminders Task" title ¬
-- titleList & ": New Reminder Created" description ¬
-- expandedQT ¬
-- application name "New Reminders Task"
-- end tell
--end if
end tell
end alfred_script
on extractList(q)
if (rightStringFromRight(q, "#")) is q and length of myDefaultList is not 0 then
return myDefaultList
end if
return rightStringFromRight(q, "#")
end extractList
on removeList(q)
if (rightStringFromRight(q, "#")) is not equal to q then
return leftStringFromRight(q, "#")
end if
return q
end removeList
on expandDay(q)
set shortDays to {"tom", "mon", "tue", "wed", "thu", "fri", "sat", "sun"}
set longDays to {"tomorrow", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"}
set expandedQ to q
repeat with i from 1 to count (longDays)
set longDay to item i of longDays
set dayStrExists to count (findAll(q, longDay))
if dayStrExists > 0 then
return expandedQ
end if
end repeat
repeat with i from 1 to count (shortDays)
set shortDay to item i of shortDays
set longDay to item i of longDays
set replacedQ to findAndReplaceShortDay(shortDay, longDay, q)
if replacedQ = -1 then
log "no match"
else
set expandedQ to replacedQ
exit repeat
end if
end repeat
return expandedQ
end expandDay
on findAndReplaceShortDay(dayShortStr, dayStr, q)
set dayStrExists to count (findAll(q, dayStr))
if dayStrExists > 0 then
return -1
end if
set firstPart to leftStringFromRight(q, dayShortStr)
if firstPart = q then
-1
else
firstPart & "" & dayStr & rightStringFromRight(q, dayShortStr)
end if
end findAndReplaceShortDay
on expandDelay(q)
if (rightStringFromRight(q, "+")) is not equal to q then
set delayInMins to rightStringFromRight(q, "+")
set firstPart to leftStringFromRight(q, "+")
set targetDate to current date
set targetDate's minutes to (targetDate's minutes) + (delayInMins)
return firstPart & (time string of targetDate)
else
return q
end if
end expandDelay
-- *** 3rd party functions from http://applescript.bratis-lover.net/library/string/ ***
-- e.g. leftStringFromRight("ab:ca:bc", ":") --> "ab:ca"
on leftStringFromRight(str, del)
-- ljr (http://applescript.bratis-lover.net/library/string/)
local str, del, oldTIDs
set oldTIDs to AppleScript's text item delimiters
try
set str to str as string
if str does not contain del then return str
set AppleScript's text item delimiters to del
set str to str's text items 1 thru -2 as string
set AppleScript's text item delimiters to oldTIDs
return str
on error eMsg number eNum
set AppleScript's text item delimiters to oldTIDs
error "Can't leftStringFromRight: " & eMsg number eNum
end try
end leftStringFromRight
-- e.g. rightStringFromRight("ab:ca:bc", ":") --> "bc"
on rightStringFromRight(str, del)
-- ljr (http://applescript.bratis-lover.net/library/string/)
local str, del, oldTIDs
set oldTIDs to AppleScript's text item delimiters
try
set str to str as string
if str does not contain del then return str
set AppleScript's text item delimiters to del
set str to str's last text item
set AppleScript's text item delimiters to oldTIDs
return str
on error eMsg number eNum
set AppleScript's text item delimiters to oldTIDs
error "Can't rightStringFromRight: " & eMsg number eNum
end try
end rightStringFromRight
on findAll(str, findString)
-- ljr (http://applescript.bratis-lover.net/library/string/)
local ASTID, str, findString, tmp, len, len2, pos, res
set ASTID to AppleScript's text item delimiters
try
set AppleScript's text item delimiters to findString
if str does not contain findString then return {}
considering case
script k
property res : {}
property tmp : str's text items
end script
end considering
set len to count k's tmp
set len2 to count findString
set pos to 0
repeat with i from 1 to len - 1
set thisPos to (count k's tmp's item i)
set thisPos to thisPos + pos
set pos to thisPos + len2
set end of k's res to (thisPos + 1)
end repeat
set AppleScript's text item delimiters to ASTID
return k's res
on error eMsg number eNum
set AppleScript's text item delimiters to ASTID
error "Can't findAll: " & eMsg number eNum
end try
end findAll
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment