Fold more, fold less

The TaskPaper 3 scripting interface gives direct support for increasing or decreasing the overall fold level of the document.

Here is one way of increasing the folding level. You can make a second copy and derive a decrease folding level script by editing the value of inc from true to false in the withOptions section.

(The script returns the resulting folding level as an integer, in case you want that for something like a Keyboard Maestro notification).

(function (dctOptions) {
    'use strict';

    function TaskPaperContext(editor, opt) {
        return editor[
            (opt.inc ? 'in' : 'de') + 'creaseExpansionLevel'
            ]() && editor.getExpansionLevel();
    }
    
    var ds = Application("TaskPaper")
        .documents,
        varResult = ds.length ? ds[0].evaluate({
            script: TaskPaperContext.toString(),
            withOptions: dctOptions
        }) : undefined;
        
    return varResult;
})({
    inc: true // edit to true for 'increase'
});
2 Likes

When I run this script, it returns:

“TypeError: editor[\n (opt.inc ? ‘in’ : ‘de’) + ‘creaseFoldingLevel’\n ] is not a function. (In 'editor\n (opt.inc ? ‘in’ : ‘de’) + ‘creaseFoldingLevel’\n ', ‘editor[\n (opt.inc ? ‘in’ : ‘de’) + ‘creaseFoldingLevel’\n ]’ is undefined)\n\nTaskPaperContext\nevaluateScript@file:///Applications/TaskPaper.app/Contents/Resources/dist/birch.js:53025:16\n\n\tUse the Help > SDKRunner to debug”

What’s wrong with it?

Thanks for spotting that.

I’ve updated the code (above) for the current version of the API.

Incidentally, you may notice that the functions provided by that script are now built into the TaskPaper user interface:

View > (Expand | Collapse) All By Level:

  • ⌘⇧9
  • ⌘⇧0

Thx, I found it in menu.