Selecting descendant tree using AppleScript

Does anyone know if it would be possible to make a skript that selects every item in the project and deselects the project itself?

I have built a script that duplicates and moves the items that the project contains to a different project, but it would be so smooth if the script also deselected the selected project and selected every task in the project.

Would be so thankful if someone could tell me if it´s possible or not!

A long time since I’ve looked at these things, but I think you should be able to write something along these lines:

tell application "OmniFocus"
    tell content of front document window of default document
        set xs to selected trees
        if {} ≠ xs then
            set oSeln to (value of first item of first item of xs)
            if class of oSeln is project then
                set oProject to oSeln
            else
                set oProject to containing project of oSeln
            end if
            set strID to (id of oProject) as string
            set selected trees to descendant trees of (first tree where id = strID)
        end if
    end tell
end tell

Or in JavaScript for Automation, perhaps something like:

(() => {
    'use strict';

    const
        contentPanel = Application('OmniFocus')
        .defaultDocument
        .documentWindows.at(0)
        .content,
        selns = contentPanel.selectedTrees;

    return 0 < selns.length ? (() => {
        const
            subTree = contentPanel.trees.byId(
                selns.at(0)
                .value()
                .containingProject.id()
            ).descendantTrees;
        return (
            contentPanel.selectedTrees = subTree,
            'Selected ' + subTree.length + ' tasks'
        );
    })() : 'Nothing selected';
})();
1 Like

Wow! The AppleScript worked exactly the way I wanted. I’m very thankful for the help! Wouldn’t have been able to figure that out myself, so again, thank you!

1 Like