Need help with a script to focus the next item

Hello all,

I am trying to make a script that will go from focused item to focused item.

Example project:

  • Task 1
  • Task 2
  • Task 3

When I am focused in on Task 1, executing the script would then focus in on the following item (Task 2, in this example). Basically, I want to use the script to move sequentially, from item to item in my TaskPaper document.

Here is my current attempt at the script (which is not working at all):

function TaskPaperContext(editor, options) {
    let outline = editor.outline
    let selection = editor.selection
    let currentItem = selection.startItem
    let followingItems = outline.evaluateItemPath('following::item', currentItem)
    let nextItems = followingItems[0]
    if (nextItems) {
        editor.focusedItem = nextItems
    }
}

Application('TaskPaper').documents[0].evaluate({
  script: TaskPaperContext.toString()
});

Any advice is appreciated. Thanks!

Jim

Can it be something like this? (untested code, but I think the idea is sound):

if (editor.focusedItem.nextSibling) {
  editor.focusedItem = editor.focusedItem.nextSibling
}

You’re a genius @jessegrosjean !

Thank you so much!

1 Like

Hi Jim
Can you please post ur final code, also plz if you comment about the lines what they do,→ be fantastic. I cant make any head/ tail of code yet
I hear you recently learning JXA, any pointers where you started wud be great
Cheers

1 Like

Hi @barrow — Sure!

Jesse’s code is it, except for the surrounding code that pushes it to TaskPaper.

Here is the whole thing:

function TaskPaperContext(editor, options) {
    if (editor.focusedItem.nextSibling) {
  editor.focusedItem = editor.focusedItem.nextSibling
 }
}

Application('TaskPaper').documents[0].evaluate({
  script: TaskPaperContext.toString()
});

I am far from an expert here. Jesse or @complexpoint are. They will be able to tell you more than I can.

That said, I will try, as it may help me learn as I discuss the code.

  • function is a like a execute command. It causes something to happen.
  • TaskPaperContext (with parameters in the parenthesis) is what function is executing on
  • if is conditional (do x, if it is possible)
  • editor.focusedItem.nextSibling tells the TaskPaper editor which content is affected
  • focusedItem is the item where the text cursor is
  • nextSibling is the next item on the same level of the outline

I hope that helps! Forgive me if I made any mistakes on the above. I am still a JavaScript newbie. It is proving to be much more challenging to learn than AppleScript.

For learning about JavaScript, ComplexPoint suggested Eloquent JavaScript, and I am studying it.

•••

I have made one minor adjustment to Jesse’s code.

editor.focusedItem.nextSibling stops when it hits the end of the outline level, as it is looking siblings (not children).

Example, which would not go past Task 2:

Project:
- Task 1
- Task 2
   - Task 3 (indented—aka, no longer a sibling)

I have replaced editor.focusedItem.nextSibling with editor.focusedItem.nextItem. Now my script goes through all items (projects, tasks and notes), item by item, and it never gets stuck. Color me happy. :slight_smile:

Hi Jim
Thank you for ur explanation, very helpful. i tired, was dealing with alien machine without manual, did not make any sense.
I am recent convert to mac, came across applescript in sept last year. After reading applescript 123, and going thru commented examples in various forums, i can understand (usually) wht the script does, and many times adapt it for my use, unless it was in AppleScriptObjC. I think i have some made headway with it in pdf/ finder tags scripts.
I believe, this wud be next challenge. I dont think i wud be knowledgable enough to write my own code, but if could understand what been written, and adapt, that wud work for me
Thank you Jim, Eloquent JavaScript wud keep me busy for few months
Have a good day Cheers

1 Like