Two scripts: Move a project up or down in the TaskPaper outline

A while back, Jesse helped me figure out how to create two JavaScripts—one to move a project up and one to move a project down.

This allows me to organize the TaskPaper outline from the keyboard (triggering the scripts through Keyboard Maestro).

Although this started as a project to learn more about JavaScript, I have been using the completed scripts since then, and they have proven to be extremely useful.

Rather than keep them to myself, I felt that it was time to share them here on the forum. I hope that you find them useful. Enjoy!

Move a project ↓ in the TaskPaper outline:

function TaskPaperContext(editor, options) {
	function currentProject(entry) {
		let each = entry;
		while (each) {
			if (isProject(each)) {
				return each;
			}
			each = each.parent;
		}
	}

	function isProject(entry) {
		return entry.getAttribute('data-type') == 'project'
	}

	let outline = editor.outline
	let currentItem = editor.selection.startItem
	if (!currentItem) {
		return
	}
	
	let project = currentProject(currentItem);
	if (!project) {
		return
	}
	
	let projectsAncestor = project.parent;
	if (!projectsAncestor) {
		return
	}

	let nextProject = project.nextSibling;
 	if (!nextProject) {
        return
    }

	projectsAncestor.insertChildrenBefore(project, nextProject.nextSibling);
	editor.moveSelectionToItems(currentItem)

}

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

Move a project ↑ in the TaskPaper outline:

function TaskPaperContext(editor, options) {
	function currentProject(entry) {
		let each = entry;
		while (each) {
			if (isProject(each)) {
				return each;
			}
			each = each.parent;
		}
	}

	function isProject(entry) {
		return entry.getAttribute('data-type') == 'project'
	}

	let outline = editor.outline
	let currentItem = editor.selection.startItem
	if (!currentItem) {
		return
	}

	let project = currentProject(currentItem);
	if (!project) {
		return
	}

	let projectsAncestor = project.parent;
	if (!projectsAncestor) {
		return
	}

	let previousProject = project.previousSibling;
	if(!previousProject || !isProject(previousProject)) {
		return
	}

	projectsAncestor.insertChildrenBefore(project, previousProject);
	editor.moveSelectionToItems(currentItem)

}

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