Copy only visible lines in a TaskPaper 3 selection

TaskPaper 3 is an outliner, and its default ⌘C copies selected lines together with any hidden or folded descendants which they may have, allowing for outline copy-paste.

Here is a simpler supplementary ⌘⌥C, which only copies the the visible lines in the selection, for moments when you just want to capture a visual snapshot of a particular folding or filtering view of the TaskPaper 3 outline editor state (and don't want to bring any hidden descendants into the clipboard).

Copy only VISIBLE lines in a TaskPaper3 selection.kmmacros (20.4 KB)

JAVASCRIPT FOR AUTOMATION source

// COPY ONLY THE SELECTED LINES WHICH ARE VISIBLE IN TASKPAPER 3
// (not copying lines which are folded or filtered out of view)

(function () {
    'use strict';

    // FUNCTION FOR TASKPAPER CONTEXT
    
    function TaskPaperContext(editor) {

        // Only the visible component of the selection

        return editor
            .selection
            .selectedItems
            .filter(function (item) {
                return editor.isDisplayed(item);
            })
            .map(function (item) {
                return Array(item.depth)
                    .join('\t') + item.bodyString;
            })
            .join('\n');

    }
    
    // JAVASCRIPT FOR AUTOMATION CONTEXT

    var ds = Application('com.hogbaysoftware.TaskPaper3')
        .documents,
        doc = ds.length ? ds[0] : undefined;

    if (doc) {

        // COPY ONLY THE VISIBLE LINES WHICH ARE SELECTED
        var a = Application.currentApplication(),
            sa = (a.includeStandardAdditions = true, a),
            
            strLines = doc.evaluate({
                script: TaskPaperContext.toString()
            });

        sa.setTheClipboardTo(
            strLines
        );

        return strLines;
    }

})();
2 Likes