The Omni Group Forums

The Omni Group Forums (http://forums.omnigroup.com/index.php)
-   OmniFocus Extras (http://forums.omnigroup.com/forumdisplay.php?f=44)
-   -   Filtering by available tasks with AppleScript (http://forums.omnigroup.com/showthread.php?t=26905)

DGrady 2012-11-13 06:38 AM

Filtering by available tasks with AppleScript
 
Hi all, I'm trying to get a list of all available flagged tasks using AppleScript and having some trouble figuring out how to do it. I can get all flagged tasks without a problem using this:

[CODE]
tell application "OmniFocus"
tell front document
set report to ""
repeat with f in (flattened tasks whose completed is false and flagged is true)
set report to report & "• " & name of f & return
end repeat
end tell
end tell

report
[/CODE]

Sometimes a task is flagged and also blocked (because I can't start it until a specific date, for example), and this list includes such tasks. I'd like to filter them out as well. I see that there's an "available task" noun in the OF scripting dictionary, but I'm unsure how to use it - could anyone give an example or suggestion? Thanks!

-Daniel

RobTrew 2012-11-13 09:43 AM

Might be worth looking at these links:
[url]http://bit.ly/OF-QL[/url]
[url]http://www.complexpoint.macmate.me/Site/Find_in_OmniFocus_by_Query.html[/url]

RobTrew 2012-11-13 10:40 AM

PS [I]available tasks[/I] is an element of a [B]context[/B] (yields a filtered list for tasks of that context).

If you wanted to bypass the context route, you might have to add the [I]flagged[/I] condition to the end of something like this :-)

[I][INDENT](blocked is false) and (completed is false) and ((start date is missing value) or (start date < now)) and ((its containing project is missing value) or (status of its containing project is active) and ((start date of its containing project < now) or (start date of its containing project is missing value))) and ((its context is missing value) or ((allows next action of its context is true) and (hidden of its context is false))) [/INDENT][/I]

[COLOR="White"]--[/COLOR]

RobTrew 2012-11-13 11:10 AM

You could also experiment with something roughly along these lines (to query the cache for tasks which are not blocked or completed, and are flagged).

[CODE]property pstrDBPath : "$HOME/Library/Caches/com.omnigroup.OmniFocus/OmniFocusDatabase2"
property pstrFieldDelimiter : "~|~"
property plstObjects : {}

on run
if not CacheFound() then return

set strQuery to "select name from task where dateCompleted is null and flagged = 1 and blocked = 0"
set plstObjects to paragraphs of runquery(strQuery)

set {dlm, my text item delimiters} to {my text item delimiters, linefeed & "• "}
set strReport to "• " & plstObjects as string
set my text item delimiters to dlm

strReport
end run


on runquery(strQuery)
set strCmd to "sqlite3 -separator '" & pstrFieldDelimiter & "' " & pstrDBPath & space & quoted form of strQuery
-- tell application "Finder" to set the clipboard to strQuery
do shell script strCmd
end runquery

on GetCachePath()
try
tell application "Finder" to tell (application file id "OFOC")
return "$HOME/Library/Caches/" & its id & "/OmniFocusDatabase2"
end tell
on error
error "OmniFocus not installed..."
end try
end GetCachePath

on FileExists(strPath)
(do shell script "test -e \"" & strPath & "\"; echo $?") = "0"
end FileExists

on CacheFound()
if not FileExists(pstrDBPath) then set pstrDBPath to GetCachePath()
if not FileExists(pstrDBPath) then
error "OmniFocus cache not found at " & return & pstrDBPath
return false
end if
return true
end CacheFound[/CODE]

RobTrew 2012-11-13 12:02 PM

Or, of course, for the equivalent approximation:

[CODE]tell application id "OFOC"
tell front document
set {dlm, my text item delimiters} to {my text item delimiters, linefeed & "• "}
set strReport to "• " & ((name of flattened tasks where completed is false and blocked = false and flagged = true) as string)
set my text item delimiters to dlm
end tell
end tell

strReport[/CODE]

DGrady 2012-11-20 11:00 AM

Thanks a lot for all your suggestions, Rob! I definitely did not realize that [I]available tasks[/I] are contained in [B]context[/B]s - once you pointed this out, I found that I can use [CODE]available tasks of flattened contexts whose flagged is true[/CODE] to get exactly what I want. I see from some of your other work that this is probably not a very efficient way to get this list, and that I could use sqlite to query the cache directly even when OF isn't running. Fortunately for me, I specifically do not want my script to run unless OF is already open, and my database is apparently simple enough that this still runs quickly.

Out of curiosity, do you have any suggestions for how I could have discovered this information on my own? I always have a lot of trouble using the built-in scripting dictionaries, and I feel that it's mainly because of the lack of examples. In this case, when I went to the OmniFocus dictionary looking for something to help, I found "OmniFocus suite > available task" almost immediately, but I still don't see how I could have known that available tasks are contained in contexts, just from reading the entry for "available task". Is there something obvious that I'm overlooking here?

Thanks again; your responses have already been a big help!

whpalmer4 2012-11-20 11:12 AM

I've found the explorer in Script Debugger by [URL="http://www.latenightsw.com/"]Late Night Software[/URL] to be quite useful for poking about to see how everything fits together.

RobTrew 2012-11-20 12:37 PM

1 Attachment(s)
Yes, I agree – Script Debugger makes it all very discoverable, particularly through its live object browser.

Otherwise, its still very useful to open the application's AS dictionary through the Applescript Editor (File > Open Dictionary > OmniFocus.app) and to do a search for the term which interests you, but it does take some close reading – in this case you have to notice that it only features as an element of the context object.

[IMG]http://forums.omnigroup.com/attachment.php?attachmentid=2650&stc=1&d=1353447403[/IMG]

DGrady 2012-11-20 01:41 PM

Thanks again both of you. I appreciate the suggestions!


All times are GMT -8. The time now is 02:44 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.