The Omni Group
These forums are now read-only. Please visit our new forums to participate in discussion. A new account will be required to post in the new forums. For more info on the switch, see this post. Thank you!

Go Back   The Omni Group Forums > OmniFocus > OmniFocus Extras
FAQ Members List Calendar Search Today's Posts Mark Forums Read

Filtering by available tasks with AppleScript Thread Tools Search this Thread Display Modes
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:

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

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!

Might be worth looking at these links:
PS available tasks is an element of a context (yields a filtered list for tasks of that context).

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

(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)))

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).

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
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()
		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
Or, of course, for the equivalent approximation:

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

Thanks a lot for all your suggestions, Rob! I definitely did not realize that available tasks are contained in contexts - once you pointed this out, I found that I can use
available tasks of flattened contexts whose flagged is true
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!
I've found the explorer in Script Debugger by Late Night Software to be quite useful for poking about to see how everything fits together.
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 > 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.

Attached Thumbnails
Click image for larger version

Name:	DictionaryEntry.png
Views:	1127
Size:	159.0 KB
ID:	2650  
Thanks again both of you. I appreciate the suggestions!

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
Thread Thread Starter Forum Replies Last Post
Filtering(?) for today's tasks omnibob OmniPlan General 0 2012-08-03 09:09 PM
applescript for common OF tasks steve OmniFocus 1 for Mac 4 2012-02-15 05:11 AM
AppleScript Project's Resource Filtering MattHive AppleScripting Omni Apps 2 2010-04-20 10:13 PM
Filtering out tasks that haven't started yet rross101 OmniFocus 1 for Mac 5 2007-08-22 12:30 AM
Filtering Projects That need tasks to complete SpiralOcean OmniFocus 1 for Mac 2 2007-05-02 08:17 PM

All times are GMT -8. The time now is 08:11 PM.

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