View Single Post
I've been having an awful time trying to obtain all the projects in my omnifocus document in Applescript.

I have a script working now, which I've mentioned in another thread. Basically it iterates over all projects and folders that are selected in the sidebar, and changes their "next review date" to the "last changed date", if the project has been changed since the last "Mark Review" event occured. I run this script once at the beginning of my daily "get clean" review, and it's working great for me. As a sidebar, I've submitted an enhancement request for Omni to make this functionality available out-of-the-box.

The problem is that I have to select all the folders in my sidebar in order to get the script to act on all of my projects. Not a great hardship obviously, but I use this script every day and I'd like to make it more convenient. I want the script to iterate over every single project, without having to select everything in the sidebar. But I haven't been able to figure out how to obtain the set of all projects.

I'm an experienced programmer, but I'll confess sometimes Applescript, and the structures under the hood of OF, really mystify me. Any hints anyone has, to help me get unstuck? Any snippets that'll spell it out?

I had hope that some of the tips and hints in this thread were going to help - didn't get me there though:
http://forums.omnigroup.com/showthre...ight=flattened

Thanks!

Here's my script, which works now, abridged:

Code:
on run
    tell application "OmniFocus"
        tell front document
            tell document window 1
                
                set selectedTrees to selected trees of sidebar
                
                repeat with aTree in selectedTrees                     
                    
                    if class of value of aTree is project then
                        my processProject(aTree)
                    else if class of value of aTree is folder then
                        my processFolder(aTree)
                    end if
                    
                end repeat
                
            end tell
        end tell
    end tell
end run

on processFolder(theFolderAsTree)
    using terms from application "OmniFocus"
        
        repeat with aTree in trees of theFolderAsTree
            if class of value of aTree is project then
                my processProject(aTree)
            else if class of value of aTree is folder then
                my processFolder(aTree)
            end if
        end repeat
        
    end using terms from
end processFolder

on processProject(theProjectAsTree)
    using terms from application "OmniFocus"
        
        set aProject to value of theProjectAsTree
        
        set currentDateAtMidnight to date (short date string of (current date))
        
        set oldReviewDate to last review date of aProject
        set lastChangedDate to modification date of aProject
        set nextReviewDateAtMidnight to next review date of aProject
        if nextReviewDateAtMidnight > currentDateAtMidnight then
            
            if oldReviewDate is not equal to lastChangedDate then
                set next review date of aProject to lastChangedDate
            end if
        end if
        
    end using terms from
    
end processProject