Script to select random task from current perspective


I’m trying to create a script that will randomly print/display/return a task from the current perspective. Can anybody lend a helping hand?

I found that is similar, but for OF1 from 2007. I also checked out OmniFocus’s AppleScript guide, and while there were many examples for adding tasks, I couldn’t find one for selecting all tasks into a list.

What I have so far that isn’t working is

tell application "OmniFocus"
	tell the default document to tell the front document window
		set perspective name to "Home"
		activate "OmniFocus"
		tell content
			set theVisibleTasks to tasks
		end tell
	end tell
end tell

which returns the error error “OmniFocus got an error: Can’t get every task of content of document window 1 of default document.” number -1728 from every task of content of document window 1 of default document"

Update: This looks like it could be a good reference:


I continued to Google and ended up writing the script myself. You can find it here:


This was great. Thanks. It works equally well when just changing the tell to

tell application id "com.omnigroup.OmniFocus3"

And although it only selects a random task from the top level in the perspective, it was easy to create a perspective with only tasks and no group hierarchies from which it could pick a random one. Thanks for this!!