Skip to content

Instantly share code, notes, and snippets.

@hmnd
Created June 9, 2019 06:16
Show Gist options
  • Save hmnd/a245c6ca1598e91e3531ee73515b46c4 to your computer and use it in GitHub Desktop.
Save hmnd/a245c6ca1598e91e3531ee73515b46c4 to your computer and use it in GitHub Desktop.
Script to upload screenshots to SFTP and copy URL of uploaded screenshot to clipboard
#!/bin/bash
# Shows error and exits
fatalError() {
rm $1
echo -e "\e[00;31mERROR: $1\e[00m"
exit 1
}
baseUrl="https://YOUR_URL"
sftpHost="YOUR_HOST.COM"
sftpUser="YOUR_USER"
sftpPrivKeyFile="$HOME/.ssh/id_rsa"
baseDir="/SCREENSHOT_DIR/"
subDirFormat="%Y-%m/" # Date format for subdir names
subDir="$(date + $subDirFormat)"
dir="$baseDir$subDir"
destDir="/DEST_DIR/"
fileName="$(cat /dev/urandom | LC_CTYPE=C tr -dc "[:alnum:]" | head -c 8).png" # change 8 to whatever length you want filenames to be
filePath="$dir$fileName"
mkdir -p $dir || fatalError "Could not create directory $dir" 1
# Take screenshot using maim
if [[ $1 == "maim" ]]; then
maim --hidecursor -s "$filePath"
if [ $? -ne 0 ]; then
fatalError $? 1
fi
# Take screenshot using gnome-screenshot
elif [[ $1 == "gnome" ]]; then
gnome-screenshot -a --file=$filePath
if [ "$(file -b --mime-type $filePath)" != "image/png" ]; then
fatalError $filePath
fi
# Take screenshot using flameshot
elif [[ $1 == "flameshot" ]]; then
flameshot gui -r >$filePath
if [ "$(file -b --mime-type $filePath)" != "image/png" ]; then
fatalError $filePath
fi
fi
# Uncomment to add a macOS-like dropshadow to screenshots
#convert $filePath \( +clone -background black -shadow 80x20+0+15 \) +swap -background transparent -layers merge +repage $filePath
# Upload screenshot to sftp
sftp -oIdentityFile="$sftpPrivKeyFile" $sftpUser@$sftpHost <<EOF
put $filePath $destDir
bye
EOF
url="$baseUrl$fileName"
# Copy to clipboard and display URL
echo -n "$url" | xclip -selection clipboard
notify-send "Screenshot uploaded" "$url copied to clipboard" --icon=edit-copy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment