Cycling through available task filter states

In OF2 for Mac I have used an AppleScript to cycle through the task filter states “available”, “incomplete”, “complete” in any perspective, using the selected task state filter identifier variable. In OF3, that only seems to work in the Tags tab, but not in Perspective tabs.

If I run this command when the Tags tab is selected:

tell application "OmniFocus"
set theDoc to document window 1 of document 1
get available task state filter identifiers of the content of theDoc
end tell

the output is

{"next", "available", "incomplete", "all", "complete"}

When a perspective is selected, the only option returned is {"all"}, even though I can still change the filtering manually using the “Filter by Availability” menu.

Is this the expected behavior? Is there some way to work around this in AppleScript? Thanks in advance for the help.

2 Likes

It looks like in OF3 get available task state filter identifiers only works for the built-in perspectives (e.g., Projects, Tags, Forecast, Review, Inbox), but not for custom perspectives. For custom perspectives, it always just returns “all”, as you already wrote, and, as a consequence, setting selected task state filter identifier to for instance “available” no longer works for custom perspectives.

This definitely looks like a bug to me. It is a particularly unfortunate regression because switching task state filters via keyboard shortcuts is extremely useful, especially because the built-in way is so cumbersome (you have to reach for the mouse, click a button, move the mouse cursor down half the screen, click on a pop-up menu, and then select the desired state from a list).

If anyone has an idea for a workaround, I would be very grateful, too. In the meantime, I have submitted a bug report about the issue.

A slightly clunky GUI scripting workaround, relevant code snippets below:

set availableTaskStates to {"next", "available", "complete", "remaining"}  --"all" no longer available
set nextTaskState to choose from list availableTaskStates

if nextTaskState contains "next" then set nextTaskState to "Availability: F"
if nextTaskState contains "available" then set nextTaskState to "Availability: A"
if nextTaskState contains "complete" then set nextTaskState to "Availability: C"
if nextTaskState contains "remaining" then set nextTaskState to "Availability: R"

tell application "System Events"
    keystroke "v" using { command down, shift down }
    repeat 8 times
        keystroke tab
    end repeat
    keystroke nextTaskState
    delay 2.5
    keystroke "v" using { command down, shift down }
end tell

This only works if you’ve upgraded the perspective and the availability selection is the first criteria listed. With downgraded perspectives you can still toggle through the options but the number of tabs required differs between perspectives. Theoretically, you should be able to GUI script it with field names but that’s a bit beyond my skills.

EDIT: Argh, just tried this out of habit on a builtin perspective and, obviously, it doesn’t work. Going to fiddle around and see if I can add some code to determine if the perspective is custom or builtin and then act accordingly.

EDIT 2: This is so convoluted I almost want to delete this entire response and replace it with “umm, never mind”.

To determine if the perspective is custom or builtin:

if class of (selected sidebar tab of first document window of default document) is item or id of (selected sidebar tab of first document window of default document) contains "process" then
	set perType to "builtin"
else
	set perType to "custom"
end if

To force the correct number of fields to tab through in the View Options popover:

set search term of first document window of default document to ((search term of first document window of default document) & space)