Skip to content

Instantly share code, notes, and snippets.

@bryanberger
Created April 22, 2024 18:59
Show Gist options
  • Save bryanberger/4d15b89bdf8ac8e55979c02750fb91a0 to your computer and use it in GitHub Desktop.
Save bryanberger/4d15b89bdf8ac8e55979c02750fb91a0 to your computer and use it in GitHub Desktop.
adb screenshot to desktop
#!/bin/bash
echo "$(basename "$0")"
KEEP_ON_PHONE=false
FILE_NAME="screenshot_$(date +"%Y-%m-%d_%H-%M-%S")"
TARGET_DIR_PHONE="//sdcard"
TARGET_DIR_COMP="$(pwd)"
printUsage() {
echo "Usage: screenshot [-k] [-f <FILE_NAME>] [-p <TARGET_DIR_PHONE>] [-c <TARGET_DIR_COMP>]"
echo " -k KEEP_ON_PHONE: Keep thecap file on the phone. Default '${KEEP_ON_PHONE}'."
echo " -f FILE_NAME: Name of thecap file. No extension. Default screen_recording_<date>_<time>.mp4."
echo " -p TARGET_DIR_PHONE: Target directory on the phone. Default '${TARGET_DIR_PHONE}'."
echo " -c TARGET_DIR_COMP: Target directory on the computer. Default pwd."
}
while getopts "hkf:p:c:" OPT; do
case "${OPT}" in
k)
KEEP_ON_PHONE=true
;;
f)
FILE_NAME="${OPTARG}"
;;
p)
TARGET_DIR_PHONE="${OPTARG}"
;;
c)
TARGET_DIR_COMP="${OPTARG}"
;;
*)
printUsage
exit 1
;;
esac
done
FULL_PATH_PHONE="${TARGET_DIR_PHONE}/${FILE_NAME}.png"
FULL_PATH_COMP="${TARGET_DIR_COMP}/${FILE_NAME}.png"
mkdir -p "${TARGET_DIR_COMP}"
function finish() {
# Pull the recorded screencap from the device
adb pull "${FULL_PATH_PHONE}" "${FULL_PATH_COMP}"
echo "Screenshot taken and saved!"
# Optionally remove the screen recording file from the device
if ! ${KEEP_ON_PHONE} ; then
adb shell "rm '${FULL_PATH_PHONE}'"
fi
}
trap finish SIGINT
echo "Press Ctrl+C to stop screenshot."
adb exec-out screencap -p "${FULL_PATH_PHONE}"
# Call the finish function explicitly when the script exits
finish
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment