TannieSpace

geekery, drawing and then some

Create a new task in a project

For completion sake, I decided to post the bit to create a new task in a new or existing project in OmniFocus through AppleScript. I have this in my script that creates tasks from my mail.

If you know a bit of AppleScript, this should help out. If I have the energy, I might post the entire script later and explain the parts.

In this case, it took longest to figure out the following part:

tell MyProject set theTask to make new task with properties¬
{name:MyTaskTopic, context:MyContext, note:ThisNote} end tell

Apparently, when creating a new task you have to tell the project. If you want to set the context, you instead tell the task to set its context property to something.

This does make sense, in the way that you always tell the containing item to do something.

The part I use in my script:

if MyContext is equal to "NONE" and MyProject is equal to "NONE" then
set theTask to make new inbox task ¬
with properties {name:MyTaskTopic, note:ThisNote} set ProjectString to "INBOX"

else
if MyContext is equal to "NONE" and ¬
MyProject is not equal to "NONE" then
tell MyProject
set theTask to make new task ¬
with properties {name:MyTaskTopic, note:ThisNote}
end tell

else
if MyProject is equal to "NONE" and ¬
MyContext is not equal to "NONE" then set theTask to make new inbox task ¬
with properties {name:MyTaskTopic, context:MyContext, note:ThisNote}
set ProjectString to "INBOX"

else
tell MyProject set theTask to make new task ¬
with properties {name:MyTaskTopic, context:MyContext, note:ThisNote}
end tell
end if

try set Datum to setDueDate
set due date of theTask to setDueDate
on error
set due date of theTask to ((current date) + 604800)
end try

tell theTask
set note to return & return
tell note
set theURL to "message://< " & message_id & ">"
set linkText to theURL insert linkText at before first character
set value of attribute "link" of style of paragraph 1 to theURL insert message_content at before last character
end tell
end tell

which works like a charm.


Comments powered by Disqus