Selected Tasks to Group in Project

Is it possible to make the selected tasks be put in a specific group in a project? I have daily projects that have groups that I use in that project daily and I am trying to assign them to those subgroups quicker if it is possible. I have been dragging and dropping or control+command and arrow keys but it is a little slower than I would like.

This is what I have but seems to be getting stuck.

tell application "OmniFocus"
  tell front document
    -- Get the selected tasks in the front document window
    set selectedTasks to value of (selected trees of content of front document window where class of its value is task)
    
    -- Check if there are selected tasks
    if (count of selectedTasks) > 0 then
      -- Get the first selected task to determine the current project
      set theTask to first item of selectedTasks
      set theProject to containing project of theTask
      
      -- Find or create the group named "—5-Miscellaneous—" within the current project
      set groupName to "—5-Miscellaneous—"
      set theGroup to missing value
      repeat with aTask in tasks of theProject
        if name of aTask is groupName then
          set theGroup to aTask
          exit repeat
        end if
      end repeat
      
      -- If the group doesn't exist, create it
      if theGroup is missing value then
        set theGroup to make new task with properties {name:groupName} at end of tasks of theProject
      end if
      
      -- Move each selected task to the group
      repeat with theTask in selectedTasks
        move theTask to end of tasks of theGroup
      end repeat
    else
      display dialog "No tasks selected."
    end if
  end tell
end tell

I sketched this OmniAutomation draft as a proof of concept. Let me know what do you think. You can run it in Script Editor, for example (setting Language tab to javascript)

(() => {
    'use strict';

    // OMNI JS CODE ---------------------------------------
    const omniJSContext = () => {
        const main = () => {
            // USER CONFIGURATION ---
            const 
                groupName = "Misc";

            // MAIN LOGIC -----------
            const 
                seln = document.windows[0].selection.tasks,
                proj = seln[0].containingProject,
                groupTask = proj.flattenedTasks.byName(
                    groupName
                ) || new Task(groupName, proj);
            return (
                moveTasks(seln, groupTask),
                new Alert(
                    `Regrouped selection`,
                    `Successfully organized ${seln.length} task(s).`
                ).show()
            )
        };

        return main();
    };


    // OmniJS Context Evaluation ------------------------------------------------
    return 0 < Application('OmniFocus').documents.length ? (
        Application('OmniFocus').evaluateJavascript(
            `(${omniJSContext})()`
        )
    ) : 'No documents open in OmniFocus.'
})();

What would happen if you select tasks in the Inbox ?

Thank you very much for this. I still need to sit down for 2 or 3 days and get my head wrapped around OmniAutomation.

Ideally the same thing since you can have actions grouped in the Inbox as well.

I have spent an while trying to get this to work. I don’t know what I am doing well eneough to make this happen. Thank you for your help and sending this my way. I am sure once I see the basic structure I can figure out what to tweak to make sub groups and subgroups of subgroups but not quite there yet.

Subgroup-Misc.omnijs (1.6 KB)

Here is an example of what my daily groups look like that I am trying to quickly move actions into with shortcuts if it helps at all.

You’re welcome.

We can make a plug-in out of the script if you want, but it’s not in Plug-in format yet.

You should paste my script in Script Editor; do that and tell me if this is what you expect.

(This is JavaScript For Automation script)

I did try that and got an error Expected expression but found “>”.

Screenshot 2024-05-22 at 11.40.44 AM

I need to sit down for a while and really learn OmniFocus Automation.

Could you show me a screenshot of your Script Editor window ?

Also, which OS are you using ?


Screenshot 2024-05-22 at 14.44.19

Set language tab to JavaScript please

Oh I get it you set it to Javascript in the window.

I just saw that as I was uploading it. I thought I had to set it in global preferences. My appoligies I have clearly never done this before and didn’t even know you could do that. Thank you!

Sure, no worries. I am here to help.

That is awesome and yes exactly what I was looking for, thank you very much! It is so nice that it also doesn’t create another group with the same name each time you run it which is of course what I was hopping for. I will play around with it and see how to make it so I can include subgroups.

—4-Work—
—Residual/Passive Income—

Glad to hear that.

What do you exactly mean by that ?

In this screen shot anything with the name “Task” would be something I would highlight and then have moved into the sub:sub groups.
So under —5-Miscellaneous— I would put actions into the subgroup of —Chores—. I don’t go any deeper than one subgroup.

Hopefully that makes sense.

Okay that was way easier than I expected it to be! It already just works regardless of if it is in a subgroup or not. Thank you so very much this will save a lot of time!!!

Good to hear that you like it. Do know that if you have tasks with the same name, the first one would be considered as destination for that grouping operation.

1 Like

I played around with it and found that out, very cool! You made an amazing automation that I will use several times a day. I set up several macros with Keyboard Maestro and assigned them to my F Keys. So nice to have this. This is one of those things that makes me wonder why I didn’t try to do this years ago.

I also love that it creates the group if it doesn’t exist already. Very grateful for your help and posting this, it’s so good!!

1 Like

Happy to hear that. Do let me know if you need a cross-platform Plug-in based on this JavaScript for Automation script.