Script :: Toggle a prefix character in selected lines

Bike may at some point acquire row type attributes like note etc, and a style-sheet mechanism for making row type differences visible, but in the meanwhile:

here is a script for toggling single character prefixes( like >, for example) at the start of each selected line:

toggledPrefix


To test in Script Editor.app,

  • set the language selector at top left to JavaScript (rather than AppleScript)
  • copy and past the whole of the script behind the expansion triangle below,
  • select some lines in Bike
  • and click Run.
Expand disclosure triangle to view JS Source
(() => {
    "use strict";

    // Toggle a given prefix character in selected lines
    // of Bike 1.2

    // Rob Trew @2022
    // Ver 0.01

    // --------------------- OPTION ----------------------
    // Which single-character prefix to add or clear ?

    const prefixChar = ">";

    // const
    //     prefixChar = Application("Keyboard Maestro Engine")
    //     .getvariable("local_PrefixChar");

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

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

            return Boolean(n) ? (() => {
                const [f, change] = (
                    selectedRows.at(0).name()
                    .startsWith(prefixChar)
                ) ? (
                    [dePrefixed(prefixChar), "CLEARED"]
                ) : [prefixed(prefixChar), "ADDED"];

                return (
                    zipWith(row => s => row.name = s)(
                        selectedRows()
                    )(
                        selectedRows.name().map(f)
                    ),
                    [
                        `${change} '${prefixChar}' prefix`,
                        `in ${n} selected lines.`
                    ]
                    .join("\n")
                );
            })() : "No rows selected in Bike";
        })() : "No documents open in Bike";
    };

    // -------------------- PREFIXES ---------------------

    // prefixed :: Char -> String -> String
    const prefixed = c =>
        s => `${c} ${dePrefixed(c)(s)}`;


    // dePrefixed :: Char -> String -> String
    const dePrefixed = c =>
        s => c === s[0] ? (
            s.slice(" " === s[1] ? 2 : 1)
        ) : s;

    // --------------------- GENERIC ---------------------

    // zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
    const zipWith = f =>
        // A list constructed by zipping with a
        // custom function, rather than with the
        // default tuple constructor.
        xs => ys => xs.map(
            (x, i) => f(x)(ys[i])
        ).slice(
            0, Math.min(xs.length, ys.length)
        );

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

(You can choose a different single-character prefix by editing the value of the prefixChar string constant at the top of the script)


Using Scripts - Bike


Keyboard Maestro makes it easier to:

  • bind keystrokes to scripts like this, and
  • adjust options (e.g. here, make different copies for different prefixes)
  • get feedback in notifications

BIKE :: Toggle a prefix character in selected lines – Keyboard Maestro Macro

2 Likes

I wanted this just this morning. :slight_smile: Thanks.

1 Like