LoginSignup
17
15

More than 5 years have passed since last update.

クリップボードの画像データを直接PNGファイルとして保存するコマンド

Last updated at Posted at 2016-06-30

GitHub - jcsalterego/pngpaste

Twitterでお気に入りの画像とか見つけて,素早くターミナル経由で保存したいときに超お役立ち :sparkles:
JPEGがPNGに変換されるのも地味にありがたい :dizzy:

インストール
mpyw@localhost:~$ brew install pngpaste
==> Downloading https://homebrew.bintray.com/bottles/pngpaste-0.2.1.el_capitan.b
######################################################################## 100.0%
==> Pouring pngpaste-0.2.1.el_capitan.bottle.tar.gz
🍺  /usr/local/Cellar/pngpaste/0.2.1: 5 files, 20.5K
ヘルプ
mpyw@localhost:~$ pngpaste -h
Usage: pngpaste [OPTIONS] <dest.png>
    -v  Version
    -h,-?   This usage
mpyw@localhost:~$ pngpaste ~/Pictures/inm/yjsnpi.png

ただこれそのままだとファイル上書き確認無しでやっちゃうので,上書き確認とか連番振りとかやりたい場合は~/.bashrc等でオーバーライドしてしまいましょう.

オーバーライドして機能拡張
pngpaste() {
    if [ "$1" = '-v' -o "$1" = '-h' -o "$1" = '-?' ]; then
        command pngpaste "$1"
        return
    fi
    while [ -f "$1" ]; do
        >&2 echo 'File already exists!'
        >&2 echo '(a)Abort  (o)Overwrite  (s)Sequence-Numbering'
        if ! IFS= read action; then
            return 1
        fi
        case "$action" in
            'a') 
                return
                ;;
            'o')
                command pngpaste "$1" && >&2 echo "Overwritten as $1"
                return
                ;;
            's')
                local ext=$([[ "$1" = *.* ]] && echo ".${1##*.}" || :)
                local name=$([[ "$1" = *.* ]] && echo "${1%.*}" || echo "$1")
                local i=0
                while true; do
                    (( i++ ))
                    local filename="$name-$(printf %02d "$i")$ext"
                    if [ ! -f "$filename" ]; then
                        break
                    fi
                done
                command pngpaste "$filename" && >&2 echo "Saved as $filename"
                return
                ;;
        esac
    done
    command pngpaste "$@" && >&2 echo "Saved as $1"
}
17
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
15