Move Project To Folder

Once upon a time (OmniFocus 1) there was an Applescript to move a project to a folder. That script worked by accessing the OmniFocus SQLite cache, and no longer works for OF2.

Anyone have or know of a script for moving a project to a folder?

My use-case is Kanban-style folders (Backlog, Queue, Active, Done) for projects. My daily review puts the projects I’m focusing on in the Active folder and moves the rest into either Queue or Backlog. Being able to assign shortcut keys to this would greatly speed my daily review.

I will hijack the question slightly … Would you really want to do this all the time even with an AppleScript?

I too use a Kanban style management of projects. I keep the Kanban board separate and link URLs between tasks/projects on it and the OF task/project of interest.


JJW

As a simple test, the following AppleScript doesn’t allow me to set a project’s folder:

tell application "OmniFocus"
using terms from application "OmniFocus"
	tell front document
		set theProject to flattened project named "TEST"
		set newFolder to folder named "Queue"
		set folder of theProject to newFolder
	end tell
end using terms from
end tell

Here’s the error:

error “OmniFocus got an error: Can’t set folder of project to folder id “jS0aGZ7saRu” of document id “pZ7sUm_0A5l”.” number -10006 from folder of project

Yes; that way I’m using a single tool, synced to my phone, with the advantages of OF perspectives to focus on the day’s work.

Oh. Fair enough.


JJW

Solution Found

tell application "OmniFocus"
tell front document
	tell document window 1 -- (first document window whose index is 1)
		set theSelectedItems to selected trees of sidebar
		set theProject to value of item 1 of theSelectedItems
	end tell
	move theProject to before [or after] the (first [or last] section of (first folder whose name is "Bottom Level Folder") of (first folder whose name is "Second Level Folder") of (first folder whose name is "Top Level Folder"))
end tell
end tell

This script fails when the selected folder is empty. Any suggestions?

I just now had to figure this out for my own script!

tell application "OmniFocus" to tell front document to move aProj to (end of sections of (first folder whose name is aFolder))

I have no idea what I’m doing when it comes to AppleScript, but it appears that you cannot refer to “first section” when the list of sections is empty, but you can refer to “end of sections”. Anyhow, I just moved an item to an empty folder with the code above.

A small improvement to your excellent inspiration @kleinfelter. This script asks for the destination folder name. Add a shortcut to the script in either [FastScripts][1] or [LaunchBar][2] and you can launch it from the keyboard.

repeat
	display dialog "To which folder would you like to move this project" default answer "Folder"
	try
		if the text returned of result is not "" then
			set the theFolder to the text returned of the result
			exit repeat
		end if
	on error
		beep
	end try
end repeat

tell application "OmniFocus"
	tell front document
		tell document window 1 -- (first document window whose index is 1)
			set selectedProjects to selected trees of sidebar
		end tell
		repeat with anItem in selectedProjects
			set theProject to value of item 1 of selectedProjects
			try
				move theProject to (beginning of sections of (first folder whose name is theFolder))
			on error
				beep
				display dialog "No folder called " & theFolder & " exists."
			end try
		end repeat
	end tell
end tell

Added error message per @DrJJWMac’s suggestion.
[1]: http://www.red-sweater.com/fastscripts/
[2]: http://www.obdev.at/products/launchbar/index.html

1 Like

The script might include a test of whether a Folder named theFolder even exists. For example …

try
    move theProject ...
on error
    -- create a folder called theFolder at the top level OR
    -- beep with dialog "No Folder called " & theFolder & " exists!"
end try


JJW