I have the AppleScript at the end of this post configured to run in the background every day to allow me to record how many tasks I’m completing. It’s actually been running since April 2010, but broke with OmniFocus 4 with the following error:
OmniFocus got an error: AppleEvent timed out. (-1712)
If I run the script manually via Script Editor, it worked fine.
The script is run via the following LaunchAgent in ~/Library/LaunchAgents
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>ca.abdevelopment.CompletedTasks</string>
<key>ProgramArguments</key>
<array>
<string>open</string>
<string>-g</string>
<string>/Users/andrew/Documents/Scripts/Percentage of completed tasks.app</string>
</array>
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>8</integer>
<key>Minute</key>
<integer>0</integer>
</dict>
<!--<key>StartInterval</key>
<integer>10</integer>-->
</dict>
</plist>
The contents of the script:
set today to current date
set currentDate to (year of today) & "-" & ((month of today) * 1) & "-" & (day of today) as string
tell application "OmniFocus"
tell first document
set totalTasks to 0
set completedTasks to 0
set pendingTasks to 0
repeat with p in flattened projects
-- Treat on hold projects as not committed to
if status of p is not on hold status then
set totalTasks to totalTasks + (count tasks of p)
repeat with t in flattened tasks of p
if completed of t is true then
set completedTasks to completedTasks + 1
else if completed of t is false and defer date of t is less than or equal to (current date) then
set pendingTasks to pendingTasks + 1
end if
end repeat
set totalTasks to pendingTasks + completedTasks
end if
end repeat
set percentComplete to completedTasks / totalTasks
my write_to_file((currentDate & "," & pendingTasks & "," & completedTasks & "," & totalTasks & "," & percentComplete & "
") as string, "Macintosh HD:Users:Andrew:Documents:Completed Tasks.csv", true)
end tell
end tell
on write_to_file(this_data, target_file, append_data)
try
set the target_file to the target_file as string
set the open_target_file to open for access file target_file with write permission
if append_data is false then set eof of the open_target_file to 0
write this_data to the open_target_file starting at eof
close access the open_target_file
return true
on error
try
close access file target_file
end try
return false
end try
end write_to_file
It turns out that when run from launchd, AppleScript will timeout waiting for the user to allow the app to control OmniFocus via a dialog. I think this is because all access permissions were reset when switching to the new app. To fix? Just open the saved app once via the Finder and allow any permission dialogs to come up.
Hopefully this helps someone else!