Skip to content

Instantly share code, notes, and snippets.

@tjluoma
Created August 7, 2021 04:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjluoma/14186f3c74fc1bf85bd865d0b656ef7e to your computer and use it in GitHub Desktop.
Save tjluoma/14186f3c74fc1bf85bd865d0b656ef7e to your computer and use it in GitHub Desktop.
Use 'wkhtmltoimage' to save an URL to a unique filename in a designated folder
#!/usr/bin/env zsh -f
# set this to whatever URL you want to capture
URL='https://www.abc.com'
# folder where do you want screenshots to go
# the folder will be created if it does not exist
DIR="$HOME/Desktop/Screenshots"
# could also be 'png' if you prefer
FORMAT='jpg'
###############################################################
## You should not need to change anything below this line
###############################################################
if [[ -e "$HOME/.path" ]]
then
source "$HOME/.path"
fi
if [[ ! -x /usr/local/bin/wkhtmltoimage ]]
then
echo "$NAME: '/usr/local/bin/wkhtmltoimage' is not installed."
echo "$NAME: get it from https://wkhtmltopdf.org/downloads.html"
exit 1
fi
# this lets us use strftime in zsh
zmodload zsh/datetime
# this gives us a timestamp like 2021-12-31--23.15.20
# for December 31st, 2021 at 11:15:20 p.m.
TIME=$(strftime "%Y-%m-%d--%H.%M.%S" "$EPOCHSECONDS")
# create folder if it doesn't exist
[[ ! -e "$DIR" ]] && mkdir -p "$DIR"
# this is the filename we'll use
# it starts with the directory
# and ends with the time and the format (jpg by default)
FILE="$DIR/$TIME.$FORMAT"
# this could be one long line, but I've formatted it
# to hopefully make it easier to read
/usr/local/bin/wkhtmltoimage \
--format "$FORMAT" \
"$URL" \
"$FILE"
exit 0
# EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment