NextActions

This script will mark a list of the uppermost tasks not tagged "done" for each project or subproject as next actions, and launch a search that displays those items. Items tagged "today" will be included in the next actions display and there is an option to manually tag entries as "next" to be included in the display. Notes are ignored unless there is a task that is a next action subordinate to the note. If a next action task has subordinate tasks, the top subordinate task will also be tagged as next, to an arbitrary depth. An entry above a subproject will be included whether or not it is a next action, which is an issue with the TaskPaper data format that's currently under discussion.

-- An Applescript to create a list of next actions from a TaskPaper document.
-- The topmost task under a project or (subproject if not single_next_actions) is marked with a next action tag.
-- All programmatic next action tags are deleted and regenerated in each run.
-- Only tasks are marked as next actions and tasks marked "done" are skipped.
-- Subtasks under tasks or notes, and subprojects, are handled.
-- Subtasks under tasks marked done are not skipped unless they themselves are marked done.
-- Items can also be tagged manually to show up on the next actions list using a different tag.
-- The programmatic and manual next action tags can be changed by editing the top two lines below.
-- The search string at the bottom of the script also includes items tagged "today".
-- The tags included in the final display list can be modified by editing the search string.

set na_tag to "na"
set manual_na to "next"
set single_next_actions to true

global na_tag
global single_next_actions

tell application "TaskPaper"
    tell front document
        repeat with each in search with query ("@" & na_tag & " -a -d")
            tell each
                delete tag named na_tag
            end tell
        end repeat
        repeat with each in projects
            if single_next_actions then
                if the level of each is 0 then my tag_next_action(each)
            else
                my tag_next_action(each)
            end if
        end repeat
        set the search field string to "" -- Hack!
        set the search field string to "@" & na_tag & " or @" & manual_na & " or @today"
    end tell
end tell

on tag_next_action(each_entry)
    using terms from application "TaskPaper"
        repeat with each in entries of each_entry
            if single_next_actions or (entry type of each) ≠ project type then
                if my tag_next_action(each) then
                    return true
                else if (entry type of each) = task type and not (exists tag named "done" of each) then
                    tell each
                        make tag with properties {name:na_tag}
                    end tell
                    return true
                end if
            end if
        end repeat
        if (entry type of each_entry) = task type and not (exists tag named "done" of each_entry) then
            tell each_entry
                make tag with properties {name:na_tag}
            end tell
            return true
        end if
        return false
    end using terms from
end tag_next_action