The Omni Group Forums

The Omni Group Forums (http://forums.omnigroup.com/index.php)
-   OmniFocus Extras (http://forums.omnigroup.com/forumdisplay.php?f=44)
-   -   Need help creating a script for creating a next action (http://forums.omnigroup.com/showthread.php?t=11169)

gcrump 2009-01-28 08:55 AM

Need help creating a script for creating a next action
 
I could greatly use some help creating a script that would be a nice addition to my work flow. The use case is during my morning review an next action will appear and I realize that I have to do something before that. I want to write a script that will take the project name from the highlited task, presents a dialog asking me for a new task name and then creates a new task assigned to that project. The key though is I want that new task to be the next action. So I guess I need to make sure then when adding to the task is inserts it at the begining of the avalabile tasks within the project. Not at the end like Quick Entry does. Can anyone script this out to give me a starting point?

Thanks a bunch in advance,

George

curt.clifton 2009-01-28 12:19 PM

George,

That's a tricky problem. What should happen if the selected task is nested within action groups? Do you want the script to "climb" all the way up to the project and add a new first task at that level?

gcrump 2009-01-28 01:14 PM

Was planning on the task being the top level, I don't do a lot of nesting. Does that help?

whpalmer4 2009-01-28 01:17 PM

[QUOTE=curt.clifton;54331]George,

That's a tricky problem. What should happen if the selected task is nested within action groups? Do you want the script to "climb" all the way up to the project and add a new first task at that level?[/QUOTE]

Curt, isn't the matter simplified a bit by the fact that it was already known to be a next action? Seems like "make new task in front of current_task" or the equivalent would work, no?

gcrump 2009-01-29 02:50 AM

Where I am struggling with this script is in two areas, first is in setting the context. In a subsequent dialog I prompt for the context. I have seen the complete context code but can't seem to figure out how to get it to work.

If I can figure that part out I was thinking the code for creating the task would be something like...

[CODE]
set theSelectedTask to value of item 1 of theSelectedItems
set newTask to make new task with properties {name:theTaskName, context:theContext}
set assigned container of newTask to theProject
move newTask to before task 1 of theProject
[/CODE]

Which all compiles OK, not sure if it will work until I figure out how to set the context.

Here is the complete code such that it is
[CODE]
tell application "OmniFocus"
tell front document
tell (first document window whose index is 1)
set theSelectedItems to selected trees of content
if ((count of theSelectedItems) < 1) then
display alert "You must first select a single task." message "Select a single task before running this script." as warning
return
end if
if ((count of theSelectedItems) > 1) then
display alert "You must select only one task." message "Select a single task before running this script." as warning
return
end if

display dialog "Enter the new next action for this project" buttons {"Cancel", "Add"} default button 2 default answer "" with title "New Next Action"
if button returned of result is "Add" then
set theTaskName to text returned of result
display dialog "Enter the context" buttons {"Cancel", "Add"} default button 2 default answer "" with title "New Context"
if button returned of result is "Add" then
set newContext to text returned of result
end if

set MyContextList to complete newContext as context maximum matches 1
set MyContextID to id of first item of MyContextList
set theContext to context id MyContextID


set theSelectedTask to value of item 1 of theSelectedItems
set theProject to the name of the containing project of theSelectedTask
set newTask to make new task with properties {name:theTaskName, context:theContext}
set assigned container of newTask to theProject
move newTask to before task 1 of theProject
end if
end tell
end tell
end tell
[/CODE]

The error I am getting now is in the line
set MyContextList to complete newContext as context maximum matches 1

and the error is OmniFocus got an error: "email" doesn’t understand the complete message. "email" was the value of new Context.

As you can see any help would be appreciated.

Thanks,

George

iNik 2009-01-29 02:11 PM

George tagged me into the ring to help on this one.

Here's what I came up with, complete with LaunchBar integration to avoid the extra song and dance of the dialog. It also selects the new task on completion to make it easy to add context and whatnot to it.

[CODE]on handle_string(s)
of_followup(s)
end handle_string

on run
of_followup("")
end run


on of_followup(taskname)
tell application "OmniFocus" to tell document 1
tell content of document window 1 to get value of descendant trees whose selected is true
set selectedItems to result
-- put a repeat here once it's troubleshot
set xItem to item 1 of result
get class of item 1 of result
if result is task then
set theProject to containing project of xItem
set theContext to context of xItem
if taskname is "" then
display dialog "New task name is...?" default answer "Copy of " & (name of xItem)
set taskname to text returned of result
end if
make new task at beginning of tasks of theProject with properties {name:taskname} --, context:(context of xItem)}
set newTask to result
tell content of document window 1 to select {newTask}
else
beep
end if
end tell
end of_followup[/CODE]

curt.clifton 2009-01-29 04:23 PM

[QUOTE=whpalmer4;54336]Curt, isn't the matter simplified a bit by the fact that it was already known to be a next action? Seems like "make new task in front of current_task" or the equivalent would work, no?[/QUOTE]

Looks like Nik has stepped in with a solution, but at the risk of going off-topic...

I was trying to see if there is a more general problem/solution here. Rather than coding for the special case of having the next action selected, I was thinking about having any action selected, then adding a new action at the top of the project/group. Why solve one problem when you can solve several at a time!

whpalmer4 2009-01-29 06:04 PM

True enough, but I'm sure you'll agree there are plenty of problems resistant to solution in a general form, but nonetheless quite usefully solved for limited cases :)

gcrump 2009-01-29 07:17 PM

Nik, Thanks for the assist. That works real well.

invictus26 2009-02-21 06:10 PM

This is a great script but I'm having a problem with it. When I'm in a perspective that the next action doesn't appear in, the script fails saying that it can't find the object to select. I guess I should be in a review context or something, but it might be good to have it work wherever you are.

Also, I was wondering if there is any way to add the context in a dialog. It would be nice for my situation where the action doesn't show up.

Thanks again for the script!


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

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