I’ve made some good progress in my quest to script OmniFocus. However there is one thing that I cannot figure out: how do you select a context in the sidebar tree with AppleScript?
The reason I want to do that is to automatically select or deselect a context based on the time of the day.
To select a tree in the sidebar:
tell application "OmniFocus"
tell default document
tell first document window
set selected of first tree of sidebar to true
end tell
end tell
end tell
Thank you for your help! If it can be of any help, I have written the following procedures to select an object in the tree by name (I had to do it this way, trying to access the name and id properties of the descendant trees doesn’t seem to work, at least in OmniFocus 2).
For example, if you have a context called “shoebox” you can call it with
SelectTreeObject("shoebox", "Select") to select the tree object.
SelectTreeObject("shoebox", "Deselect") to deselect the tree object.
SelectTreeObject("shoebox", "Toggle") to toggle the selected state.
I haven’t really tested the “Toggle” case so it may be buggy… Don’t forget to add “my” if you’re calling those from your own script (my SelectTreeObject…).
Here are the two procedures:
(*
SelectTreeObject
Arguments:
theObjectName: the tree object name (context name or project name)
bSelect: "Select" to select, "Deselect" to deselect, "Toggle" to toggle as string
Returns:
true if could be selected, false if not
*)
on SelectTreeObject(theObjectName, theAction as string)
set bFoundLeaf to false
tell application "OmniFocus"
set theDocWin to document window 1 of front document
set theSidebar to sidebar of theDocWin
set theRootLeaf to first leaf of theSidebar
set theFollowingSiblingsList to following siblings of theRootLeaf
repeat with theFollowingSibling in theFollowingSiblingsList
set theCount to count of descendant trees of theFollowingSibling
if name of theFollowingSibling is theObjectName then
if theAction is "Select" then
set selected of theFollowingSibling to true
else if theAction is "Deselect" then
set selected of theFollowingSibling to false
else if theAction is "Toggle" then
set bSelected to selected of theFollowingSibling
set selected of theFollowingSibling to (not bSelected)
end if
set bFoundLeaf to true
exit repeat
else if theCount is greater than 0 then
set bFoundLeaf to my SelectFollowingSiblings(theFollowingSibling, theObjectName, theAction)
if bFoundLeaf is true then
exit repeat
end if
end if
end repeat
end tell
return bFoundLeaf
end SelectTreeObject
on SelectFollowingSiblings(theLeaf, theNameToSelect as string, theAction as string)
set bFoundLeaf to false
using terms from application "OmniFocus"
set theDescendantTreesList to descendant trees of theLeaf
repeat with theDescendantTree in theDescendantTreesList
set theName to name of theDescendantTree
if theName is equal to theNameToSelect then
if theAction is "Select" then
set selected of theDescendantTree to true
else if theAction is "Deselect" then
set selected of theDescendantTree to false
else if theAction is "Toggle" then
set bSelected to selected of theDescendantTree
set selected of theDescendantTree to (not bSelected)
end if
set bFoundLeaf to true
exit repeat
end if
end repeat
end using terms from
return bFoundLeaf
end SelectFollowingSiblings