Script :: Toggle Comment (note)

I made a simple script that many plain text editors have for toggling comments.
(Of course it has to be ⌘/, which takes up the Show Status Bar shortcut)

tell front document of application "Bike"
	set ls to selection rows
	repeat with each in ls
		if type of each is body then
			set type of each to note
		else if type of each is note then
			set type of each to body
		end if
	end repeat
end tell
3 Likes

FWIW a variant in JS.

(Its behaviour differs only when rows of different types have been selected – toggles all selected rows together, basing the toggle direction on the type of the first selected row).

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

    const
        specialType = "note",
        defaultType = "body";

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

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

            return 0 < n
                ? (() => {
                    const
                        toggledType = specialType === (
                            selectedRows.at(0).type()
                        )
                            ? defaultType
                            : specialType;

                    return (
                        selectedRows().forEach(
                            row => row.type = toggledType
                        ),
                        `${n} row(s) -> ${toggledType}`
                    );
                })()
                : `Nothing selected in '${doc.name()}'`;
        })()
        : "No document open in Bike.";
})();

To test in Script Editor, set language selector at top left to JavaScript.

See: Bike – Using Scripts


i.e. equivalent, in AppleScript terms, to something like:

Expand disclosure triangle to view AppleScript source
tell front document of application "Bike"
	if it exists then
		set selectedRows to a reference to (rows where selected is true)
		
		if 0 < (count of selectedRows) then
			if body is type of (item 1 of selectedRows) then
				set toggleType to note
			else
				set toggleType to body
			end if
			
			set type of selectedRows to toggleType
			toggleType as text
		else
			"Nothing selected in '" & name & "'."
		end if
	else
		"No document open in Bike."
	end if
end tell
2 Likes