Skip to content

Instantly share code, notes, and snippets.

@CraigSiemens
Created March 25, 2023 18:54
Show Gist options
  • Save CraigSiemens/c14f3b0be86cc3c0920f19591f0c7971 to your computer and use it in GitHub Desktop.
Save CraigSiemens/c14f3b0be86cc3c0920f19591f0c7971 to your computer and use it in GitHub Desktop.
Takes a screenshot of each page in the current zoom window.
#!/bin/bash
###################################################################################################
# USAGE
###################################################################################################
print_usage() {
echo -e "Takes screenshots of the pages in the current zoom window"
echo -e "\nUsage:"
echo -e " ./zoom-screenshots.sh [-d DELAY_BETWEEN_SCREENSHOTS] [-o OUTPUT_DIR]"
echo -e "\nFlags (optional):"
echo -e " - d: Sets the amount of time to wait between taking each screenshot."
echo -e " - o: The directory to write the screenshots to."
echo -e "\nNotes:"
echo -e " Terminal requires some permissions in System Preferences > Security & Privacy for this script to work."
echo -e " - Accessibility: to use the osascript command to get the zoom window's frame and switch pages"
echo -e " - Screen Recording: to use the screencapture command"
}
###################################################################################################
# PARSE PARAMETERS
###################################################################################################
DELAY_BETWEEN_SCREENSHOTS=""
OUTPUT_DIR="$HOME/Desktop/Screenshots-$(date '+%Y-%m-%d')"
while getopts 'd:o:h' flag; do
case "${flag}" in
d)
DELAY_BETWEEN_SCREENSHOTS="${OPTARG}"
;;
o)
OUTPUT_DIR="${OPTARG}"
;;
h)
print_usage
exit 0
;;
*)
print_usage
exit 1
;;
esac
done
###################################################################################################
# ASK FOR THE PAGE COUNT
###################################################################################################
echo -e "⚠️ Make sure zoom is on the first page before continuing.\n"
echo "How many pages are there?"
read -r pages
###################################################################################################
# GET THE RECTANGLE OF THE ZOOM WINDOW
###################################################################################################
echo -e "\nGetting zoom window rectangle"
rect=$(osascript <<EOD
tell application "zoom.us"
activate
delay 1
end tell
tell application "System Events"
set frontProcess to a reference to (first application process whose frontmost is true)
set allWindows to a reference to windows of frontProcess
set frontWindow to item 1 of allWindows
set windowPosition to position of frontWindow
set y to item 2 of windowPosition
set x to item 1 of windowPosition
set windowSize to size of frontWindow
set w to item 1 of windowSize
set h to item 2 of windowSize
"" & x & "," & y & "," & w & "," & h
end tell
EOD
)
echo "Rect: $rect"
###################################################################################################
# CREATE OUTPUT DIRECTORY
###################################################################################################
echo -e "\nCreating screenshot output directory"
echo "$OUTPUT_DIR"
mkdir -p "$OUTPUT_DIR"
###################################################################################################
# TAKE SCREENSHOTS
###################################################################################################
echo -e "\nTaking screenshots"
for page in $(seq 1 "$pages")
do
echo -n " $page"
# Capture the screenshot of the rectangle
screencapture "-R$rect" "$OUTPUT_DIR/page${page}.png"
# Send the keyboard shortcut for zoom to go to the next page
osascript <<EOD
tell application "System Events" to keystroke "n" using {control down}
EOD
# Delay if the delay flag was set
if [ -n "$DELAY_BETWEEN_SCREENSHOTS" ]; then
sleep "$DELAY_BETWEEN_SCREENSHOTS"
fi
done
###################################################################################################
# OPEN OUTPUT DIRECTORY
###################################################################################################
open "$OUTPUT_DIR"
echo -e "\n\nDone"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment