This script is probably a bit specialised, but when toggling Code
type rows to Ordered
, I often want to retain their <code>
appearance, rather than defaulting back to the standard font.
This script toggles selected rows in Bike, alternating its output message between:
- N row(s) toggled to
code
. - N row(s) toggled to
ordered
, (with inline<code>
formatting).
To test in Script Editor, set the language selector at top left to JavaScript
(rather than AppleScript
)
See: Using Scripts | Bike
Expand disclosure triangle to view JS source
(() => {
"use strict";
// Toggle Selected Rows:
// "Code" ⇄ "Ordered" with inline Code formatting
// Rob Trew @2024
// Ver 0.1
// ---------------------- MAIN -----------------------
// main :: IO ()
const main = () => {
const doc = Application("Bike").documents.at(0);
return doc.exists()
? toggleCodeOrdered(
doc.rows.where({selected: true})
)
: "No document open in Bike.";
};
// ---------------------- BIKE -----------------------
// toggleCodeOrdered :: Bike Rows Specifier -> IO String
const toggleCodeOrdered = rows => {
// "Code" row type ⇄ "Ordered" with inline <code>
// Note that the type of rows is NOT a JS Array.
const nRows = rows.length;
return 0 < nRows
? (() => {
const
wasCode = "code" === rows.at(0).type(),
newType = wasCode
? "ordered"
: "code";
rows().forEach(x => {
const runs = x.textContent.attributeRuns;
// EFFECTS
// Row type
x.type = newType;
// Inline format
enumFromTo(0)(runs.length - 1)
.forEach(
i => runs.at(i).code = wasCode
);
});
return [
`${plural(nRows)("row")} toggled to '${newType}'`,
wasCode
? ",\n(with inline <code> formatting)."
: "."
]
.join("");
})()
: "No rows selected in Bike.";
};
// enumFromTo :: Int -> Int -> [Int]
const enumFromTo = m =>
// Enumeration of the integers from m to n.
n => Array.from(
{length: 1 + n - m},
(_, i) => m + i
);
// plural :: Int -> String -> String
const plural = n =>
// Singular or plural EN inflection
// of a given word, preceded by digits.
k => 1 === n
? `${n} ${k}`
: `${n} ${k}s`;
return main();
})();