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

 
How can I create a task, using Objective C (or PyObjC)? Thread Tools Search this Thread Display Modes
In AppleScript, a new task can be created like this:
tell front document of application "OmniFocus"
make new inbox task with properties {name: theName}
end tell
But what would be the corresponding message to create a new task in Objective C (or PyObjC)?
 
I have partly solved the problem. With PyObjC a new inbox task object can be created like this:
omnifocus = ScriptingBridge.SBApplication.applicationWithBundl eIdentifier_('com.omnigroup.OmniFocus')

omnifocus.classForScriptingClass_('inbox task').alloc().initWithProperties_({'name': 'Computer created task'})
But, this is a "future OmniFocusInboxTask", and does not show up in the inbox.

How should I do to stop it being future, and start showing up in the present?
 
Sorry for the silence on the thread. I haven't tried to automate OF other than with AppleScript, but you seem to have crossed the syntax "bridge", as it were, so maybe I can help now.

What do you mean by "future" OmniFocusInboxTask? A "future" is a fairly arcane programming languages term, but I'm guessing you mean something else.
__________________
Cheers,

Curt
 
A couple of off-the-cuff ideas, from someone who's not familiar with ScriptingBridge:

Have you looked at the Python "appscript" module? It might be easier to use because there's only one layer of translation going on (Python to the AppleEvents object model)*rather than two (Python to Objective-C to AppleEvents).

My other thought is that you might have to insert the newly created object into a container before it becomes real. Applescript-accessible objects can't exist in a vacuum; they need to be "in" something (possibly just in the application object). ScriptingBridge might be waiting for you to provide the newly created task with a location before it actually sends the AppleEvent to create it.
 
Curt:
Quote:
Originally Posted by curt.clifton View Post
What do you mean by "future" OmniFocusInboxTask?
The object created by the code above, is described by PyObjC like this:
<future OmniFocusInboxTask with properties { name = "Task name"; }>
I suspect that "future" may be ScriptingBridge's way to indicate that the apple event to create the object is not yet sent. But that's just a theory.


Wim:
Quote:
My other thought is that you might have to insert the newly created object into a container before it becomes real
I think you are right about this. I've tested recreating the code in F-script, and the error message I get there is "object has not been added to a container yet".

But how do I assign it to a container?

In OmniFocus applescript dictionary I found this:
assigned container (item) : Inbox tasks (those contained directly by the document) have a provisionally set container that is made final by the 'compact' command. This allows you to set and get said container. The container must be either a task (not in the inbox or contained by an inbox task), a project or 'missing value'.
But despite trying a lot of different aproaches, I've not been able to make it work.

I'm not 100% sure, but the problem seams to be that
omnifocus.classForScriptingClass_('inbox task').alloc().initWithProperties_({'name': 'Computer created task'})
results in a object that is a ScriptingBridge proxy object. This object as such is unable to respond to messages like compact(), setAssignedContainer_() and addObject_(). Normaly, this would be easy to solve by adding a get(), to get the real object, but for some reason this results in an error.

Maybe it's a cache 22-like bug: To attach the object to a container you need to send a get to it, but since it's not attatched to any container this is not possible....

Another, equally likely, possibility is that I haven't got the sytax quite right.

If anyone has managed to create a new task in OmniFocus, using Objective C, Python, F-script or Ruby, it would be extremely interesting to see the code.

Until then, I might try the applescript in Python workaround Wim suggested.

Last edited by Tommy@heltenkelt.se; 2008-08-01 at 12:17 PM..
 
A stab in the dark, but is it possible to include the assigned container in the properties list of the original init call?
__________________
Cheers,

Curt
 
Quote:
Originally Posted by curt.clifton View Post
A stab in the dark, but is it possible to include the assigned container in the properties list of the original init call?
Interesting idea, but I can't get that to work.
 
Here is a workaround, using applescript within Python, as Wim suggested:

Code:
import os

def createInboxTask(name):
                cmd = """osascript<<END
                tell front document of application "OmniFocus"
                    set newTask to make new inbox task with properties {name: "(Placeholder)"}
                end tell
                END"""
                os.system(cmd)
                
                newTask = [task for task in of.application.defaultDocument().inboxTasks() if task.name()=='(Placeholder)'][0]
                newTask.setName_(name)
                return newTask
 
Actually, I was suggesting using the appscript module, not using AppleScript. I really like the appscript module.

Re ScriptingBridge, check out the Scripting Bridge Programming Guide for Cocoa section on Creating and Adding Scripting Objects.
 
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Create Task with Label kennedyma OmniFocus 1 for Mac 1 2011-06-01 08:05 AM
AppleScript help: Create sub-task, project from task iNik OmniFocus Extras 3 2007-12-05 08:53 AM
Script to create an email from a task in OF. Tinchohs OmniFocus Extras 3 2007-10-31 12:53 PM
Create Entourage Message from OF Task spnyc OmniFocus Extras 1 2007-10-20 07:48 PM
Objective-C 2.0? treaves Omni Frameworks 0 2007-04-19 09:23 AM


All times are GMT -8. The time now is 08:17 PM.


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