Skip to content

Instantly share code, notes, and snippets.

@cattaka
Forked from hkurokawa/adb-screenshot.sh
Last active December 6, 2017 00:51
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cattaka/f0f6ad312bb6a0606ed9ae05e164c761 to your computer and use it in GitHub Desktop.
Save cattaka/f0f6ad312bb6a0606ed9ae05e164c761 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.
##
## 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.
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
exit_code=0
case $1 in
-t) dir=$2
shift 2
;;
-h) usage_exit
;;
esac
remote=${dir}/${file}
adb $* shell screencap -p ${remote} &&
adb $* pull ${remote} /tmp/ &&
adb $* shell rm ${remote} && printf "Copied ${file}\n"
check_ret
if [ -z $(which convert) ]; then
echo "ImageMagick seems not be installed on this computer. Just exit."
exit 0
fi
echo "Do you want to create a smaller version of the image? (To quit, just input 'q')"
read -p "Specify resize ratio [30%]: " size
case $size in
[qQ]) exit;;
*)
esac
if [ -z "${size}" ]; then
size="30%"
fi
small_file=${file%%.png}_s.png
convert -resize ${size} /tmp/${file} /tmp/${small_file} && printf "Created /tmp/${small_file} with the ${size} size of the original.\n"
check_ret
file /tmp/${small_file}
if [ -n "$(which osascript)" ]; then
osascript -e 'on run args' -e 'set thisFile to item 1 of args' -e 'set the clipboard to (read thisFile as «class PNGf»)' -e return -e end /tmp/${small_file} && echo "Copied to clipboard."
elif [ -n "$(which xclip)" ]; then
xclip -selection clipboard -t image/png -i /tmp/${small_file} && echo "Copied to clipboard."
else
echo "osascript or xclip seem not be installed on this computer. Just exit."
exit_code=1
fi
rm /tmp/${file} /tmp/${small_file}
exit $exit_code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment