OmniFocus, ChatGPT, and AppleScript

I was looking at my Forecast view in OmniFocus this morning, and the list was fully expanded. Because I use defer dates a lot, the expanded Forecast can look pretty intimidating. I can never remember the key command to collapse and expand, so I end up manually doing it in the GUI. I thought this might be a good use of AppleScript, especially since OmniFocus has a scripts folder that you can use to store scripts and then drag them into the toolbar.

I asked ChatGPT to write a script to first collapse, and then another to expand, and then realized I should ask it to make the script toggle the state based on whatever state is currently active:

tell application "OmniFocus"
    tell front document
        tell content of front document window
            set theTrees to trees
            if theTrees is not {} then
                set firstTreeExpanded to expanded of item 1 of theTrees
                set newState to not firstTreeExpanded -- Toggle based on the current state of the first tree
                repeat with aTree in theTrees
                    set expanded of aTree to newState
                end repeat
            end if
        end tell
    end tell
end tell

Here’s what it looks like:

applescript-of-expand-collapse.mp4

It’s in the toolbar too:

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax