The Omni Group Forums

The Omni Group Forums (http://forums.omnigroup.com/index.php)
-   OmniFocus Extras (http://forums.omnigroup.com/forumdisplay.php?f=44)
-   -   Basic new item script (http://forums.omnigroup.com/showthread.php?t=24249)

Chris Ryan 2012-05-20 08:48 PM

Basic new item script
 
I've look through the forums (and several OF scripting sites) and perhaps I'm not searching well but I just want to know how to create a new item, something like "'tell OmniFocus make new item with title 'title' context 'context' project 'project'".

Brian 2012-05-21 12:47 PM

The easiest way to do this is to use the "parse tasks" command. This lets you piggy-back on top of the code that parses emails and turns them into OmniFocus actions.

See [URL="http://forums.omnigroup.com/showthread.php?t=21781"]this thread[/URL] for an example of this approach in action. Hope this helps!

Chris Ryan 2012-05-21 12:57 PM

Thanks for your response. I'm going to have to read that post a few times; I've never heard of Alfred and it all seems very complex compared to what I want to do. So you're saying there's no direct way to create an OmniFocus item from AppleScript?

whpalmer4 2012-05-21 12:59 PM

No, Brian was saying that there's a AppleScript facility called parse tasks, which was used by the author of the script in the thread he pointed out to you to do something similar. In the mean time, I wrote a post telling you how to do it the harder way :-)

Making an action in the Inbox is pretty straightforward; it's a little more complex when you want to put the action in a project, because you have to specify where it goes.

This will make a task in the Inbox:
[code]

tell application "OmniFocus"
tell default document
make new inbox task with properties {name:"Sample task", note:"This is a short note"}
end tell
end tell

[/code]

This will look up a context and make a task in the Inbox and assign the context:

[code]
property pContext : "Mail"

tell application "OmniFocus"
tell default document
set MyContext to my GetMyContext(pContext)
make new inbox task with properties {name:"Sample task", note:"This is a short note", context:MyContext}
end tell
end tell

-- Search all contexts for strContext, return first one found with strContext in name. Handles nested contexts.
on GetMyContext(strContext)
tell application "OmniFocus"
tell default document
set lstContexts to flattened contexts where name contains strContext
if (length of lstContexts > 0) then
return (first item of lstContexts)
else
return (missing value)
end if
end tell
end tell
end GetMyContext
[/code]

Finally, here's one that makes a new task, assigns a context, and installs it in a project:

[code]

property pContext : "Mail"
property pProject : "Miscellaneous"

tell application "OmniFocus"
tell default document
set MyContext to my GetMyContext(pContext)
set MyProject to my GetMyProject(pProject)
if (MyProject is not missing value) then
tell MyProject
set myTask to make new task at end with properties {name:"Sample task", note:"This is a short note", context:MyContext}
end tell
else
make new inbox task with properties {name:"Sample task", note:"This is a short note", context:MyContext}
end if
end tell
end tell

-- Search all contexts for strContext, return first one found with strContext in name. Handles nested contexts.
on GetMyContext(strContext)
tell application "OmniFocus"
tell default document
set lstContexts to flattened contexts where name contains strContext
if (length of lstContexts > 0) then
return (first item of lstContexts)
else
return (missing value)
end if
end tell
end tell
end GetMyContext

-- Search all project names for strProject, return first one found with strProject in name.
on GetMyProject(strProject)
tell application "OmniFocus"
tell default document
set lstProjects to flattened projects where name contains strProject
if (length of lstProjects > 0) then
return (first item of lstProjects)
else
return (missing value)
end if
end tell
end tell
end GetMyProject


[/code]

whpalmer4 2012-05-21 01:10 PM

If you wanted to do the same thing as the last version with the parse tasks verb, it might look like this:
[code]
tell application "OmniFocus"
tell default document
set strParseTasks to "Sample task @ Mail > Miscellaneous " & return & "This is a short note"
set theResult to parse tasks with transport text strParseTasks with as single task
end tell
end tell
[/code]

whpalmer4 2012-05-21 01:12 PM

The format understood by parse tasks is documented in the online help for OmniFocus, under the title "Processing Mail messages into actions". With a little fiddling about, you can set up OmniFocus to parse selected email messages and turn them into actions so that others can be making your list of things to do longer even as you are attempting to make it shorter :-)

Chris Ryan 2012-05-21 02:50 PM

Thanks very much. Is this documented anywhere (besides the dictionary)?

whpalmer4 2012-05-21 02:58 PM

Various scripts that people have sussed out, forum posts...one very helpful thing is to get Late Night Software's Script Debugger and use the Explorer functionality to browse all the data structures exposed by AppleScript. Some of this stuff is challenging to decipher if you have to figure it out by reading the dictionary, but much clearer when you can see what it looks like.

Fnorthrop 2012-06-20 02:18 PM

I had a problem with the scripts in that the GetMyContext and GetMyProject functions were passed to OmniFocus instead of the functions in the script, Omnifocus returned an error. I moved the calls out of the tell statements and it worked like a charm. Thanks.

RobTrew 2012-06-20 11:32 PM

A transcription glitch ? whpalmer4's scripts run as is if copied and pasted …

I wonder if the [I]my[/I] keyword got dropped ?

( You can call a script-defined function within the [I]tell … end tell[/I] scope as long as you precede it with [I]my[/I], to specify that it is a script level function, rather than a method of the current object )[INDENT][CODE]my GetMyContext(pContext)[/CODE][/INDENT]


All times are GMT -8. The time now is 04:53 AM.

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