Created
December 24, 2025 08:12
-
-
Save pHo9UBenaA/28bf407afa23b0fd03a17e1adc09b88e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # Prerequisite: | |
| # Install draw.io beforehand: | |
| # brew install --cask drawio | |
| set -euo pipefail | |
| DRAWIO="/Applications/draw.io.app/Contents/MacOS/draw.io" | |
| OUTPUT_BASE="$HOME/Downloads/tmp" | |
| DRAWIO_FILES=( | |
| "<path>/<to>/file1" | |
| "<path>/<to>/file2" | |
| ) | |
| # Count the number of pages in a draw.io file by counting "<diagram ...>" tags | |
| # Purpose: prevent the issue where the last page keeps being exported | |
| # even when the -p page range is out of bounds | |
| count_pages() { | |
| local file="$1" | |
| # grep exits with code 1 when nothing is found, so absorb it with "|| true" | |
| local n | |
| n="$(grep -o '<diagram' "$file" 2>/dev/null | wc -l | tr -d ' ')" | |
| echo "${n:-0}" | |
| } | |
| mkdir -p "$OUTPUT_BASE" | |
| for f in "${DRAWIO_FILES[@]}"; do | |
| if [[ ! -f "$f" ]]; then | |
| echo "WARN: not found: $f" >&2 | |
| continue | |
| fi | |
| base="$(basename "$f" .drawio)" | |
| out_dir="$OUTPUT_BASE/$base" | |
| mkdir -p "$out_dir" | |
| pages="$(count_pages "$f")" | |
| if [[ "$pages" -le 0 ]]; then | |
| echo "WARN: cannot detect pages (0). skip: $f" >&2 | |
| continue | |
| fi | |
| echo "INFO: $f pages=$pages -> $out_dir" | |
| for ((page=1; page<=pages; page++)); do | |
| # Always pass an output *directory* to draw.io (not a file path) | |
| # The output file name is assumed to be "<input basename>.png" | |
| "$DRAWIO" -x -f png -p "$page" -o "$out_dir" "$f" >/dev/null 2>&1 || { | |
| echo "WARN: export failed page=$page: $f" >&2 | |
| continue | |
| } | |
| generated="$out_dir/${base}.png" | |
| if [[ ! -f "$generated" ]]; then | |
| echo "WARN: output missing page=$page: $f" >&2 | |
| continue | |
| fi | |
| printf -v idx "%02d" "$page" | |
| mv -f "$generated" "$out_dir/${base}-${idx}.png" | |
| done | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment