Counting the number of projects in a particular folder using Applescript

I’m trying to count the number of projects in a folder.

I have figured out how to count all projects and tasks.

tell application "OmniFocus"
	tell front document
		set Folderlist to count of every flattened project
		set taskList to count of flattened tasks
	end tell
end tell

I would appreciate it if somebody could help me to figure out the counting in a particular folder bit…

It might be worth looking at this:

OmniFocus Statistics: OmniJS PlugIn (Mac/iOS) - OmniFocus - The Omni Group User Forums

Thanks @draft8 I looked at that but I I don’t know JavaScript and I didn’t see a way to adapt it to check the contents of a particular folder.

I appreciate the feedback. 😃

Perhaps something like this (given a name change at the top):

on run
    set folderName to "Publications"
    
    tell application "OmniFocus"
        tell default document
            set matches to flattened folders where name is folderName
            
            if {} ≠ matches then
                set namedFolder to item 1 of matches
                count of flattened projects of namedFolder
            else
                "No folder found with name matching '" & folderName & "'"
            end if
        end tell
    end tell
end run

@draft8 So simple if you know how! 😃

Your Script-Fu is legendary! 🙇🏻‍♂️

Thank you.

2 Likes

FWIW in a JavaScript idiom for Script Editor (rather than omniJS automation), you could flip the language selector at top left (from AppleScript to JavaScript) and try something equivalent like:

(() => {
    "use strict";

    const folderName = "Publications";

    const
        omnifocus = Application("OmniFocus"),
        matches = omnifocus.defaultDocument
        .flattenedFolders.where({
            name: folderName
        });

    return 0 < matches.length ? (
        matches.at(0).flattenedProjects.length
    ) : `No folder found with name matching '${folderName}'`;
})();
1 Like