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 Extras
FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
Basic new item script Thread Tools Search this Thread Display Modes
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'".
 
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 this thread for an example of this approach in action. Hope this helps!
 
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?
 
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
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
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
 
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
 
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 :-)
 
Thanks very much. Is this documented anywhere (besides the dictionary)?
 
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.
 
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.
 
A transcription glitch ? whpalmer4's scripts run as is if copied and pasted …

I wonder if the my keyword got dropped ?

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
setting project + context via script (modifying a DEVONthink script) bernd OmniFocus Extras 2 2012-09-08 12:10 PM
Script: Clear start/due dates of selected item(s) dbyler OmniFocus Extras 0 2010-06-15 10:17 AM
Very basic time tracking script ganwell OmniFocus Extras 2 2009-11-27 10:08 AM
very basic help scottnh OmniFocus Syncing 6 2008-10-16 11:33 PM


All times are GMT -8. The time now is 05:52 AM.


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