I’ve been doing a lot of work with Multi-Markdown tables for my day job. And I’ve been somewhat frustrated with how much formatting (and reformatting) is required to keep them looking nice in a plain text Markdown file.
So I tool a page out of @drdrang’s playbook and created an AppleScript that converts the first table on the selected Apple Numbers sheet to a Multi-Markdown table:
(* Converts the first table on the active Number's sheet to a Multi-Markdown table. *) tell application "Numbers" set _md to "" -- If there's an open document, process it if (count of documents) > 0 then set _doc to the first item in documents set _sheet to the _doc's active sheet set _table to the first item in _sheet's tables tell _table -- Format the table's header set _isFirstRow to true set _rows to rows repeat with _row in _rows -- Generate a Markdown table row for the row's cells repeat with _cell in _row's cells set _val to _cell's value if _val ≠ missing value then set _md to (_md & "| " & _val as Unicode text) & " " else set _md to _md & "| " end if end repeat set _md to _md & "|" & linefeed -- Set the justification based on how the first row is styled. if _isFirstRow then repeat with _cell in _row's cells if _cell's alignment = auto align or _cell's alignment = justify or _cell's alignment = left then set _md to _md & "| :---- " else if _cell's alignment = center then set _md to _md & "| :---: " else if _cell's alignment = right then set _md to _md & "| ----: " end if end repeat set _md to _md & "|" & linefeed set _isFirstRow to false end if end repeat end tell end if return _md as Unicode text end tell
This script can be executed by a Keyboard Maestro action that types or pastes its output.
Note that the script is somewhat slow as I think AppleScript doesn’t perform string concatenations very quickly. On my Mac—which is admittedly quite ancient—it takes about 8 seconds to output a Multi-Markdown table from a Numbers table that has 175 rows.