Simple word count

A script which counts the number of words in the front Bike document:

In Script Editor, with the language drop-down at top-left set to JavaScript, you can test the code below, which gives a message like:

"208472 words in Moby Dick.bike"

(() => {
    "use strict";

    // Word count for Bike
    // Rob Trew @2022
    // Ver 0.1

    const
        bike = Application("Bike"),
        frontDoc = bike.documents.at(0),
        space = /\s+/gu;

    return frontDoc.exists(0) ? (() => {
        const
            n = frontDoc.rows.name()
            .join(" ")
            .split(space)
            .length;

        return `${n} words in ${frontDoc.name()}`;
    })() : "No documents open in Bike.";
})();

You could also drop it into an Execute JavaScript for Automation action in Keyboard Maestro, and choose a hotkey trigger for it:

BIKE : rough word count.kmmacros.zip (1.6 KB)


See: Customizing Bike

2 Likes

And FWIW a variant, very fractionally faster, could use the JavaScript Array.reduce pattern.

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

    // Word count for Bike
    // Rob Trew @2022
    // Ver 0.3
    const
        bike = Application("Bike"),
        frontDoc = bike.documents.at(0),
        space = /\s+/gu;

    return frontDoc.exists(0) ? (() => {
        const
            n = frontDoc.rows.name().reduce(
                (sofar, para) => sofar + (
                    para.split(space).length
                ),
                0
            );

        return `${n} words in ${frontDoc.name()}`;
    })() : "No documents open in Bike.";
})();

Array.prototype.reduce() - JavaScript | MDN

2 Likes