How to insert a project before a specific project?

Hello all,

I have a script that inserts a project and its tasks at the root of the outline.

I would like to insert the project before a specific project (Links:).

Any advice on how to modify my script?

function TaskPaperContext(editor, options) {
const d = new Date();
const dtd = d.getMonth() + 1 + "/" + (d.getDate() + 1) + " through " + (d.getMonth() + 1) + "/" + (d.getDate() + 7);
    let outline = editor.outline
    let project = outline.createItem('→ Habits Tracker → ' + dtd + ":")
    project.appendChildren([
        outline.createItem('- Drink 40oz of water → ○○○○○○○'),
        outline.createItem('- Walk thirty minutes → ○○○○'),
        outline.createItem('- Intermittent fasting → ○○○'),
        outline.createItem('- Brainstorm → ○○○'),
        outline.createItem('- Meditate → ○○○'),
        outline.createItem('- Read → ○○○'),
        outline.createItem('- Write → ○○○')
    ])
    let root = editor.outline.root
    root.insertChildrenBefore(project, root.firstChild)
    editor.moveSelectionToItems(project, 0, project, 0)
}

Application('TaskPaper').documents[0].evaluate({
  script: TaskPaperContext.toString()
});

I think outline.insertItemsBefore(items, referenceItem) should do that for you:

[Outline](Outline · GitBook)

1 Like

Thanks @complexpoint

Trying to figure this out. firstChild inserts it under the Links: project.

Do I replace firstChild? ancestors, previousItem and previousBranch don’t seem to work.

Or is my evaluateItemPath wrong?

Current code experiment:

let root = editor.outline.evaluateItemPath('/project Links')[0]
root.insertChildrenBefore(project, root.firstChild)

Not sure that I’m clear yet about what is intended to go where.

The argument pattern is

insertItemsBefore(items, referenceItem)

Where items is, of course, the material to be placed, and referenceItem is the (e.g. project) which marks the destination point.

The intention is to insert the created project (Habits Tracker…in the example) before the Links project.

Example (before the insertion):

Project 1:
Project 2:
Links:

Example (after the insertion):

Project 1:
Project 2:
Habits Tracker…:
Links:

So what should referenceItem be to place the new project before Links?

Links itself, I think.

@jessegrosjean — any suggestions? I am a bit lost here.

Hi @Jim,

I don’t use a ‘/’ at the beginning of my item paths, and I think the trailing colon at the end of the project name needs to be included.

I would try this code (untested):

let links = editor.outline.evaluateItemPath('project Links:')[0]
links.insertChildrenBefore([project], root.firstChild)

And, not sure if it helps, but you might want to take a look at my Populate Daily Habits script.

1 Like

Thanks @batkins — testing your code, and it puts the project under Links, instead of before it:

I will take at your Populate Daily Habits script. Thanks!

This should insert before I think:

let links = outline.evaluateItemPath('project Links:')[0]
root.insertChildrenBefore(project, links)	

Or a bit more generic to work when links isn’t child of root:

let links = outline.evaluateItemPath('project Links:')[0]
links.parent.insertChildrenBefore(project, links)	
1 Like

It does. You are my hero Jesse!

Thank you everyone. You all have a lot of patience with me—much appreciated!

Hey Jim, this looks like it could be good reference for those trying to figure out how to build a script. Would you mind updating your code to the finish code? I will then add this to the script wiki.

Thank you!

2 Likes

Thrilled to do so. I appreciate the Wiki, and your efforts with it!

function TaskPaperContext(editor, options) {
const d = new Date();
const dtd = d.getMonth() + 1 + "/" + (d.getDate() + 1) + " through " + (d.getMonth() + 1) + "/" + (d.getDate() + 7);
    let outline = editor.outline
    let project = outline.createItem('→ Habits Tracker → ' + dtd + ":")
    project.appendChildren([
        outline.createItem('- Drink 40oz of water → ○○○○○○○'),
        outline.createItem('- Walk thirty minutes → ○○○○'),
        outline.createItem('- Intermittent fasting → ○○○'),
        outline.createItem('- Brainstorm → ○○○'),
        outline.createItem('- Meditate → ○○○'),
        outline.createItem('- Read → ○○○'),
        outline.createItem('- Write → ○○○')
    ])
    let links = editor.outline.evaluateItemPath('project Links:')[0]
    links.parent.insertChildrenBefore(project, links)
    editor.moveSelectionToItems(project, 0, project, 0)
}

Application('TaskPaper').documents[0].evaluate({
  script: TaskPaperContext.toString()
});
2 Likes