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?