How to find and open a project or folder from a text string

I am trying to create an Applescript that can take a text string input (e.g. “My
Project” or “My Folder”) and then:

a) see if there is a folder with that name in Omnifocus and open it,
or if not,

b) see if there is a project with that name in Omnifocus and open it

and if neither folder nor project exist, pop up an alert saying so.

I can’t for the life of me work out how to do this. The issue seems to be how do I find a folder or project ID based just off a text string search.

Any help would be greatly appreciated.

If you’re willing to use Omni automation, the following entry in the API would be a starting point:

FolderArray : Array

An Array containing Folder objects.

Instance Functions

function byName(name: String) → Folder or null

Returns the first Folder contained directly in this array with the given name.