adb screencap function

So regardless of being a failure at pretty much everything, I still like to tinker and tweak my android phones pretty regularly, and one thing I rather hate is the need to open DDMS/Monitor to take a screenshot.

Well, a few days ago I found a great tip on this site:

And quickly decided to expand on it a bit for my own personal use, and thought I’d share my final implementation here in case anyone might use it.

Just add the following function to your .bashrc or .zshrc file and either re-start your shell, or re-source the file (e.g. source $HOME/.zshrc) to be able to use the “adbss” command.

# setup a simple adb screenshot function
adbss () {
    local file="$(/bin/date +"%d-%b-%Y_%I.%M.%S").png"
    local dir="$HOME/Downloads/adb_screencaps"
    if [[ ! -d $dir ]]; then
        mkdir -p "${dir}"
    adb shell screencap -p | perl -pe 's/\x0D\x0A/\x0A/g' > "$dir/$file"

And of course to change the directory the screencaps are stored in, just edit the “local dir=…” line with the path to the directory of your choosing.

Anyway, I’ve tested this with the default BASH 3.2.48 shipped with OS X 10.8, BASH 4.2.52 (built by homebrew), and ZSH 5.0.2 (also built by homebrew) and it works fine in all three.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s