How do I assign a unique task or project to a variable in Applescript?

Context: I am working on a custom OmniFocus URL handler in Applescript.

The OmniFocus URL handler (omnifocus:///task/[item id]) opens tasks and projects in the Context perspective. It does not select the item within that window, and it does not reveal the item if it is hidden (e.g., On Hold).

I am still learning from and reviewing variants of Rob Trew’s OmniFocus/DEVONthink scripts, so if my question is already answered in there please feel free to point that out! Also, Omni’s “trees” seem to be based on a mixed metaphor and this may be contributing to my confusion.

My Question: How do I assign a unique task or project to a variable in Applescript?

I find it easy to go from objects to properties in Applescript, but more difficult to go from a unique property (such as an ID) to the object that has this property. To work around this problem, I often assign an object to a variable and then see what gets written into that variable. Sometimes I can backwards-engineer my way to a solution with that information. Not so with the case of OmniScript trees.

Here is the script, followed by some of my attempts to solve the problem:

tell application "OmniFocus"
	tell front document
		tell (first document window whose index is 1)

			## Return the selected trees in OmniFocus
			set id_string to (id of (selected trees of content)) as string
			-- returns: "mj4Y-QBYvwP"

			set this_tree to (selected trees of content)
			-- returns: {tree 4 of tree 1 of content of document window id 213 of document id "mj4Y-QBYvwP" of application "OmniFocus"}

			set id_string_alt to (id of (this_tree)) as string
			-- returns: error "Can’t make id of {«class OTtr» 4 of «class OTtr» 1 of «class FCcn» of «class FCdw» id 213 of document id \"mj4Y-QBYvwP\" of application \"OmniFocus\"} into type string." number -1700 from id of {«class OTtr» 4 of «class OTtr» 1 of «class FCcn» of «class FCdw» id 213 of document id "mj4Y-QBYvwP"} to string

		end tell
	end tell
end tell

Why does set id_string_alt to (id of (this_tree)) as string fail when set id_string to (id of (selected trees of content)) as string succeeds? All I can surmise is that there is some difference between what the variable is and what the actual object is. What is that difference?

Attempts to solve the problem so far:

Am I trying to use this_tree before it is set? No:

delay 5
--No effect.

I thought that perhaps some type conversion might place when assigning the tree to a variable, but I can’t tell what the type is appropriate for the tree:

type of (selected trees of content)
-- returns: error "OmniFocus got an error: Can’t get type of every selected tree of content of document window 1 of document 1 whose index = 1." number -1728

All told, I’m out of my (rather shallow) depths on this one. Any suggestions or explanations?

I am puzzled. Is this what you are wanting to do?

property pPerspective : “Contexts”

tell front document window of default document to set its perspective name to pPerspective

Otherwise, I think the distinction here is that this_tree is a record of (list) objects and as such has no id. By comparison, (selected trees of content) is one list object, and one property of that list object is its id.

But then, I could be completely confused even still. Can you say … blind leading the blind?

Is this what you are wanting to do?

Thanks, but not quite. This changes the perspective, but does not let me do anything with a task or project.

What I want to do is:

  1. Set a variable that can act as a placeholder for whatever task or project is currently selected.
  2. Later, get properties of that task or project as needed using the variable rather than the phrase (selected trees of content). By the time the script asks for those properties, the task in question might not be selected any more so I need a way to refer to it.

So, for a pseudocode example:

set selected_thing to whatever task is currently selected

do some things that change the selected task

get the id of selected_thing

I see now.

I don’t know.

I have similar problems for some scripts that I would want to write. I also get lost too quickly in how exactly to frame the syntax.

In this regard, I wish folks here would post more baby-step users guide for how to build basic routines with AppleScript for OF. Otherwise, with the limited resources I can find, trying to write simple AppleScripts for OF is like trying to do basic chemistry reactions when you only have reference books on advanced theoretical quantum chemistry.