Script :: Pruning out empty rows from the front document

Some material that I paste into Bike brings more empty rows than I want.

This script prunes out empty rows from the front document – either from the whole document, or (if the selection is extended) only from selected rows.

To test in Script Editor, set the language selector at top left to JavaScript (rather than AppleScript)

Expand disclosure triangle to view JS source
(() => {
    "use strict";

    // Delete childless empty rows in the visible part of
    // the front document.
    //
    // If the selection is extended,
    // then only childless gaps in *selected* rows
    // are removed.
    //
    // Otherwise, *all* childless empty rows in any visible
    // part of the document are pruned out.
    // Reversible with ⌘Z

    // Rob Trew @2022
    // Ver 0.04

    // const main :: IO ()
    const main = () => {
        const
            bike = Application("Bike"),
            doc = bike.documents.at(0);

        return doc.exists() ? (() => {
            const
                selectionExtended = Boolean(
                    doc.selectedText()
                ),
                childlessEmptyRows = doc.rows.where((
                    selectionExtended ? (
                        inSelection
                    ) : (x => x)
                )({
                    _and: [
                        {visible: true},
                        {name: ""},
                        {containsRows: false}
                    ]
                })),
                n = childlessEmptyRows.length;

            return (
                // Effect
                bike.delete(childlessEmptyRows),
                // Value (message string)
                [
                    [`Deleted ${n} empty rows in`],
                    selectionExtended ? (
                        ["extended selection of "]
                    ) : [],
                    [`document: "${doc.name()}"`],
                    0 < n ? ["(⌘Z to undo)"] : []
                ]
                .flat()
                .join("\n")
            );
        })() : "No documents open in Bike";
    };


    // inSelecton :: Dict -> Dict
    const inSelection = match =>
    // JXA Where/Whose condition for Bike Rows
    // further restricted to selected rows only.
        ({
            _and: [
                {selected: true},
                match
            ]
        });

    // MAIN ---
    return main();
})();

See: Using Scripts - Bike

3 Likes

Updated to prune only childless empty rows.

As before, reversible with ⌘Z

1 Like

Keyboard Maestro version:

2 Likes