I'm relatively new to AppleScript and have just finished my first useful script. I've always wanted to repeat OmniFocus tasks based on calendar events and have written this script to hold me over until that functionality is added to OmniFocus. Is there a way to speed up this script? It takes ~10 seconds right now. Can I do anything to improve it?
Note: It requires the CalendarLib EC library.
-------------------------------------------------------------------------------------------
use scripting additions
set d1 to current date
set day of d1 to 1
set d2 to current date
set day of d2 to daysInMonth(d1)
set createdCount to 0
set theStore to fetch store
set theCal to fetch calendar "Work" cal type cal cloud event store theStore -- change calendar here
set theEvents to fetch events starting date d1 ending date d2 searching cals theCal event store theStore
repeat with anEvent in theEvents
set eventInfo to event info for event anEvent
set eventStart to eventInfo's event_start_date
set eventEnd to eventInfo's event_end_date
tell application "OmniFocus"
tell front document
set theTask to "List some items on eBay" -- change task title here
set theProject to (first flattened project where its name = "Work Actions") -- change project here
set theContext to (first flattened context where its name = "Work") -- change context here
if not (exists (first flattened task where its name = theTask and its context = theContext and its defer date = eventStart and its due date = eventEnd - (hours * 2))) then
tell theProject to make new task with properties {name:theTask, context:theContext, defer date:eventStart, due date:eventEnd - (hours * 2)}
set createdCount to createdCount + 1
end if
end tell
end tell
end repeat
if createdCount = 1 then
set tasks to "task"
else if createdCount ≠ 1 then
set tasks to "tasks"
end if
display dialog "Created " & createdCount & " new \"" & theTask & "\" " & tasks & "." buttons {"OK"} default button "OK"
-------------------------------------------------------------------------------------------
# HANDLERS
-------------------------------------------------------------------------------------------
on daysInMonth(aDate)
local aDateCopy
copy aDate to aDateCopy
set day of aDateCopy to 1
if month of aDateCopy is December then
set year of aDateCopy to ((year of aDateCopy) + 1)
set month of aDateCopy to 1
else
set month of aDateCopy to ((month of aDateCopy) + 1)
end if
return day of (aDateCopy - 1 * days)
end daysInMonth
-------------------------------------------------------------------------------------------