The Omni Group
These forums are now read-only. Please visit our new forums to participate in discussion. A new account will be required to post in the new forums. For more info on the switch, see this post. Thank you!

Go Back   The Omni Group Forums > OmniFocus > OmniFocus 1 for Mac
FAQ Members List Calendar Today's Posts

 
New 'complete' AppleScript command Thread Tools Search this Thread Display Modes
Quote:
Originally Posted by Tim Wood
Code:
tell application "OmniFocus"
	tell default document
		complete "a" as context
	end tell
end tell
and get:

Code:
{{xml:"<span class=\"match\">a</span>bc", score:15, id:"bxMoRcqDm0g", name:"abc"},
{xml:"c<span class=\"match\">a</span>r", score:0, id:"krLrtHo7Pbe", name:"car"}}
That is, you get a score-sorted array of match records, each with:
  • id: the identifier of the matching object
  • name: the name of the matching object (currently the full hierarchical name, but we could add both this and the local name)
  • score: the numerical score of the match; bigger is better. The result is already sorted, but this might be useful. It isn't scaled to any max value, so a relevance indicator would be hard to do.
  • xml: a little XML blob of the 'name' marked up with <span> tags. The default span class name is "match". You can pass an optional 'span class' argument to the command to change this to whatever you want.
Hopefully you all will find this useful; let us know if there are other scripting hooks that'd be helpful too.
Ok, I'm clearly missing something. What I want to do is:
Code:
make new inbox task with properties {name:the_name, context:the_context}
But I'm having trouble getting a usable context from of the 'complete' command. I can get all the data out of the 'complete' command:
Code:
tell application "OmniFocus"
	tell document 1
		set context_string to "email"
		set first_match to first item of (complete context_string as context maximum matches 1)
		set the_id to id of first_match
		set the_name to name of first_match
	end tell
end tell
But none of the following seems to work:
Code:
set the_context to context the_id
set the_context to context the_name
set the_context to context named the_name
set the_context to context whose id is the_id
What don't I get?
 
Just guessing, no time to test at the moment, but have you tried just using the name string. That is:

Code:
set the_context to the_name
__________________
Cheers,

Curt
 
Quote:
Originally Posted by curt.clifton
Just guessing, no time to test at the moment, but have you tried just using the name string. That is:

Code:
set the_context to the_name
No dice.

Neither just using the id string. Always trows a coercion error trying to make <whatever> into type context.
 
This took some digging. OF documents use a tree of contexts. So you have to do a tree walk to find subcontexts. Here's a snippet of a new version of my Add OmniFocus Action script for Mail (not yet posted) that does the tree walk.

Code:
(*
	Returns the first context with the given name, or missing value if not found.
	theContexts: a list of OmniFocus contexts
	contextName: the name of the sought context
*)
on findContextNamed(theContexts, contextName)
	if (theContexts is {}) then return missing value
	set firstContext to item 1 of theContexts
	using terms from application "OmniFocus"
		if (name of firstContext is contextName) then return firstContext
		set subContext to my findContextNamed(every context of firstContext, contextName)
		if (subContext is not missing value) then return subContext
		return my findContextNamed(rest of theContexts, contextName)
	end using terms from
end findContextNamed
I call this with:

Code:
set theContext to my findContextNamed(every context, defaultContext)
where defaultContext is a string containing the context name. This call occurs inside a "tell first document of application..." block.
__________________
Cheers,

Curt
 
You don't need to search for the name, you can just grab the id of the match. Here's a relevant code snippet (from the built-in Quicksilver script, Send to OmniFocus.scpt):

Code:
set MyContextArray to complete ContextString as context maximum matches 1
if ((count of MyContextArray) > 0) then
    set MyContextID to id of first item of MyContextArray
    set ThisContext to context id MyContextID
else
    set ThisContext to (make context with properties {name:ContextString})
end if
Hope this helps!
 
Quote:
Originally Posted by Ken Case
Code:
    set ThisContext to context id MyContextID
Hope this helps!
(emphasis added)

Ah, the missing, magic "id". Thanks, Ken!
__________________
Cheers,

Curt
 
Quote:
Originally Posted by curt.clifton
Code:
on findContextNamed(theContexts, contextName)
	if (theContexts is {}) then return missing value
	set firstContext to item 1 of theContexts
	using terms from application "OmniFocus"
		if (name of firstContext is contextName) then return firstContext
		set subContext to my findContextNamed(every context of firstContext, contextName)
		if (subContext is not missing value) then return subContext
		return my findContextNamed(rest of theContexts, contextName)
	end using terms from
end findContextNamed
Almost. I still didn't quite get what I expected -- something to do with name matching for sub-contexts. But rather than chase it down, I just switched to an ID matching scheme. So:

Code:
tell application "OmniFocus"
	tell document 1
		set first_match to first item of (complete context_search_string as context maximum matches 1)
		set the_ID to id of first_match
		set found_context to my findContextByID(every context, the_id)
		make new inbox task with properties {name:"test", context:found_context}
	end tell
end tell

on findContextByID(theContexts, the_id)
	if (theContexts is {}) then return missing value
	set firstContext to item 1 of theContexts
	using terms from application "OmniFocus"
		if (id of firstContext is the_id) then return firstContext
		set subContext to my findContextByID(every context of firstContext, the_id)
		if (subContext is not missing value) then return subContext
		return my findContextByID(rest of theContexts, the_id)
	end using terms from
end findContextByID
Still seems crazy to me since OF has already done the work of finding the matching context.

Omniites: How about adding a 5th element to the returned records from the complete command that is just a reference to the matching context (or project)?
 
Quote:
Originally Posted by Ken Case
You don't need to search for the name, you can just grab the id of the match. Here's a relevant code snippet (from the built-in Quicksilver script, Send to OmniFocus.scpt):
Woops. Looks like the answer was posted whilst I was writing the question. I only just noticed. Thanks.
 
 




Similar Threads
Thread Thread Starter Forum Replies Last Post
AppleScript move command bholveck OmniOutliner 3 for Mac 3 2012-01-19 02:55 PM
Bug in OmniFocus Applescript PBPASTE command RobTrew OmniFocus Extras 0 2011-04-14 11:41 AM
AppleScript references with no set or command MLModel OmniOutliner 3 for Mac 1 2010-11-21 11:34 AM
Applescript Duplicate command: how to use it? Lutin OmniFocus Extras 6 2007-10-30 12:56 AM
AppleScript 'do script' command Bcpst OmniWeb Feature Requests 2 2006-05-06 11:08 PM


All times are GMT -8. The time now is 11:16 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.