Skip to content

Instantly share code, notes, and snippets.

@hkurokawa
Last active August 30, 2023 06:27
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save hkurokawa/75b44564cc1491b5f4a2 to your computer and use it in GitHub Desktop.
Save hkurokawa/75b44564cc1491b5f4a2 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.
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
}
copy_to_clipboard() {
if which osascript > /dev/null 2>&1 ; then
osascript -e 'on run args' -e 'set the clipboard to POSIX file (first item of args)' -e end $(pwd)/${small_file} && echo "Copied to clipboard."
elif which xclip > /dev/null 2>&1 ; then
xclip -selection clipboard -t image/png -i $(pwd)/${small_file} && echo "Copied to clipboard."
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"
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} ${file} ${small_file} && printf "Created ${small_file} with the ${size} size of the original.\n"
check_ret()
if ! which osascript > /dev/null 2>&1 || ! which xclicp > /dev/null 2>&1 ; then
echo "osascript or xclip seems not be installed on this computer (maybe it is not Mac OS or Linux and xclip is not installed?). Just exit."
exit 0
fi
read -p "Do you want to copy the resized file into clipboard? (y/n) " yn
case $yn in
[Nn]*) break;;
* ) copy_to_clipboard
;;
esac
@CreatorB
Copy link

on line 56 [qQ]) exit;; should be ([qQ]) exit;;

@hkurokawa
Copy link
Author

Sorry. I didn't notice your comment for a while? And why?

@wicky05
Copy link

wicky05 commented Jan 29, 2021

error in command

-z was unexpected at this time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment