Skip to content

Instantly share code, notes, and snippets.

@davebeach
Forked from hkurokawa/adb-screenshot.sh
Last active June 11, 2017 19:50
Show Gist options
  • Save davebeach/85189ca400ca3492b9b723ac44541535 to your computer and use it in GitHub Desktop.
Save davebeach/85189ca400ca3492b9b723ac44541535 to your computer and use it in GitHub Desktop.
A shell script to take a screen shot of the android device, resize it and copy to clipboard
#! /bin/bash
## This script is for taking a screen shot of an Android device.
## If possible, it tries to resize the image file and then copy to the clipboard.
##
## Note the script generates screenshot_yyyyMMddHHmmss.png and screenshot_yyyyMMddHHmmss_s.png
## under /sdcard on the device (you can specify another location with '-t' option)
## and copies it to the current working directory.
##
## The script passes unrecognized arguments to adb command, which means you can specify "-e" or "-d"
## to select which device to take a screenshot of.
## From GIST by hkurokawa GIST adb-screenshot.sh
if [ -z $(which adb) ]; then
echo "Error. adb must be installed and in PATH." 1>&2
exit 1
fi
usage_exit() {
echo "Usage: $0 [-t target_dir] [arguments to adb]..."
exit 1
}
check_ret() {
if [ $? -ne 0 ]; then
echo "An error occurred. Exit." 1>&2
exit 1
fi
}
file=screenshot_$(date "+%Y%m%d%H%M%S").png
dir=/sdcard
case $1 in
-t) dir=$2
shift 2
;;
-h) usage_exit
;;
esac
remote=${dir}/${file}
adb $* shell screencap -p ${remote} &&
adb $* pull ${remote} &&
adb $* shell rm ${remote} && printf "Copied ${file}\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment