Script :: toggling row type Body ⇄ Heading

I’m finding it helpful to have a quick on-off toggle for Heading status:


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

    // Toggle type of selected Bike rows Body ⇄ Heading
    // Rob Trew @2023
    // Ver 0.1

    const doc = Application("Bike").documents.at(0);

    return doc.exists()
        ? (() => {
            const
                rows = doc.rows.where({
                    selected: true
                });

            return 0 < rows.length
                ? (() => {
                    const
                        newType = (
                            "heading" === rows.at(0)
                            .type()
                        )
                            ? "body"
                            : "heading";

                    return (
                        rows().forEach(
                            x => x.type = newType
                        ),
                        newType
                    );
                })()
                : "No rows selected in Bike.";
        })()
        : "No document open in Bike.";
})();

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

See: Using Scripts - Bike

2 Likes

Any other toggles, where

 t ∈ {"heading", "quote", "code", "ordered", "unordered", "task", "note"}

could, of course, be derived from a pattern like this:

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

    // Toggle type of selected Bike rows
    // Default ⇄ Specialised
    // (e.g. "body" ⇄ typeName ∈ {"heading", "quote", "code"})

	// or drawn from {"unordered", "ordered", "task", "note"}

    // Rob Trew @2023
    // Ver 1.2

    // --------------------- OPTION ----------------------
    const rowTypeName = "quote";

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

        return doc.exists()
            ? toggleType(rowTypeName)(
                doc.rows.where({selected: true})
            )
            : "No document open in Bike.";
    };

    // ---------------------- BIKE -----------------------

    // toggleType :: String -> [Bike Row] -> IO String
    const toggleType = typeName =>
        rows => {
            const n = rows.length;

            return 0 < n
                ? (() => {
                    const
                        defaultType = "body",
                        newType = (
                            typeName === rows.at(0)
                            .type()
                        )
                            ? defaultType
                            : typeName;

                    return (
                        rows().forEach(
                            x => x.type = newType
                        ),
                        `${n} rows toggled to '${newType}'.`
                    );
                })()
                : "No rows selected in Bike.";
        };

    return main();
})();


2 Likes