View Full Version : Setting nested context from script
donhoffman
08-10-2007, 03:40 PM
Are there any examples of how to set a nested context from the scripting interface? E.g., Work->Email
Thanks,
Don
RobTrew
08-13-2007, 02:42 PM
You need a reference to the corresponding element in the contexts tree of the document
One way would be something like:
tell application "OmniFocus"
set oContextList to contexts of first document
set oContext to my FindContext("Email", oContextList)
-- ...
-- Assume creation of, or reference to an Omnifocus task (oTask) somewhere here
-- ...
set context of oTask to oContext
end tell
-- get a pointer to any defined context with this name
on FindContext(strName, oContexts)
tell application "OmniFocus"
ignoring case
repeat with oContext in oContexts
-- set strContextName to name of oContext
if (name of oContext) = strName then
return oContext
else
set oSubContexts to contexts of oContext
if (count of (oSubContexts)) > 0 then
set ContextFound to my FindContext(strName, oSubContexts)
-- Check whether a context has in fact been found
try
set SomeName to name of ContextFound
return ContextFound
end try
end if
end if
end repeat
end ignoring
end tell
end FindContext
RobTrew
08-14-2007, 02:43 AM
Probably quicker, in fact, to use the verb "Complete" which is provided in the Omnifocus applescript dictionary, and which will find nested contexts directly.
tell application "OmniFocus"
tell first document
set oTask to make new inbox task with properties {name:"Testing"}
try
set context of oTask to my FindContext("Email")
end try
end tell
end tell
on FindContext(strContext)
tell application "OmniFocus"
tell first document
set oContextList to complete strContext as context maximum matches 1
if (oContextList is {}) then
else
return context id (id of item 1 of oContextList)
end if
end tell
end tell
end FindContext
RobTrew
08-14-2007, 03:54 AM
Finally, if you had two nested contexts with the same name, e.g.
Work > Email
and
Home > Email
you could specify the particular path by using a colon as a separator.
For example, using the FindContext() function defined in the previous post:
try
set context of oTask to my FindContext("Work:Email")
end try
vBulletin® v3.7.0, Copyright ©2000-2008, Jelsoft Enterprises Ltd.