Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Screenshot Imgur Upload Script
- #
- # Takes a screenshot (desktop, active window, area) after a little pause
- # and uploads it to imgur (optionally) and copies the image's URL to the clipboard (optionally)
- #
- # Dependencies: scrot, curl, zenity, xclip, sox, libnotify-bin
- #
- # Author: Michael Koch (m<DOT>koch<AT>emkay443<DOT>de)
- # Version: 2014-10-21
- # License: GNU General Public License v3 (http://www.gnu.de/documents/gpl-3.0.en.html)
- ###################
- ## CONFIGURATION ##
- ###################
- # scrot binary file path
- scrot_cmd="/usr/bin/scrot"
- # xclip binary file path
- clipboard_enabled=true
- xclip_cmd="/usr/bin/xclip"
- # notify-send binary file path
- notification_enabled=true
- notification_cmd="/usr/bin/notify-send"
- # Command to play the given sound file on success
- sound_enabled=true
- sound_cmd="/usr/bin/play"
- sound_file="/usr/share/sounds/freedesktop/stereo/complete.oga"
- # Enable the following option if you want to have a symlink to your screenshots folder on your desktop
- link_on_desktop_enabled=false
- # Screenshot file extension (must be compatible)
- file_extension="png"
- file_basedir="$HOME/Bilder/screenshots"
- # Pause time in seconds before a screenshot is taken
- # Useful for taking screenshots of opened menues
- sleep_time="2"
- # Disable the following option
- # if you don't want to paste the screenshot's URL to the specified upload log
- uploadlog_enabled=true
- uploadlog_file_name="imgur-upload.txt"
- # Command line option names
- option_type_desktop="--desktop"
- option_type_window="--window"
- option_type_area="--area"
- option_no_upload="--no-upload"
- option_force_upload="--force-upload"
- option_no_save="--no-save"
- #########################################################################################
- # WARNING: You should be careful when changing any code below. Modify at your own risk! #
- #########################################################################################
- ######################
- ## HELPER FUNCTIONS ##
- ######################
- function printParmError {
- echo -e "Usage: $0 <TYPE> [OPTIONS]"
- echo -e "The screenshot's type can be one of the following:"
- echo -e "$option_type_desktop\tTakes a screenshot of the full desktop"
- echo -e "$option_type_window\tTakes a screenshot of the activated window"
- echo -e "$option_type_area\t\tTakes a screenshot of a user-selected area"
- echo
- echo -e "These options are also possible:"
- echo -e "$option_no_upload\tDon't ask for upload, just save to disk"
- echo -e "$option_force_upload\tDon't ask for upload, always upload"
- echo -e "$option_no_save\tDon't save the image on disk"
- echo -e "\t(the above option doesn't make sense together with --no-upload)"
- exit 1
- }
- function doUpload {
- $notification_cmd --hint=int:transient:1 "Upload" "The screenshot is being uploaded..."
- # Try to upload the screenshot to imgur and put the results to the variable curl_response
- curl_response=$(curl -F "image"=@"$file_path" -F "key"="5d317f0bee23b282473522e1aa68f621" http://imgur.com/api/upload.xml)
- # Print error message if errors occur
- if [[ "$curl_response" == *overload* ]]; then
- $notification_cmd --hint=int:transient:1 "Imgur server overloaded" "The screenshot couldn't be uploaded\nThe servers are overloaded"
- elif [[ "$curl_response" == *404* ]]; then
- $notification_cmd --hint=int:transient:1 "Imgur Server not reachable" "The screenshot couldn't be uploaded\nThe servers are not reachable (error 404).\nCheck your network connection"
- elif [[ "$curl_response" == *error* ]]; then
- $notification_cmd --hint=int:transient:1 "Imgur Server not reachable" "The screenshot couldn't be uploaded\nThe servers are not reachable (unknown error).\nCheck your network connection"
- else
- # Strip unneccessary stuff and put the image's URL to the clipboard
- curl_response=$(echo -e "$curl_response" | grep -Eo '<[a-z_]+>http[^<]+'|sed 's/^<.\|_./\U&/g;s/_/ /;s/<\(.*\)>/\1:\ /' | awk 'NR == 1' | sed -e 's/Original Image: //' | tr -d '\n')
- if [ $clipboard_enabled ]; then echo $curl_response | $xclip_cmd -sel clip; fi
- if [ $uploadlog_enabled ]; then
- # If there is no upload log, create one
- # and append the URL to it
- if [[ ! -e "$uploadlog_path" ]]; then
- touch "$uploadlog_path"
- fi
- echo -e "$file\n\n$curl_response\n\n\n" > $uploadlog_path
- fi
- # Notify the user about a successful upload
- if [ $notification_enabled ]; then $notification_cmd --hint=int:transient:1 "Imgur Upload" "Upload was successful\n\nLink: <a href='$curl_response'>$curl_response</a>"; fi
- if [ $sound_enabled ]; then $sound_cmd $sound_file; fi
- fi
- }
- ####################
- # HELPER VARIABLES #
- ####################
- day="$(date '+%d')"
- month="$(date '+%m')"
- year="$(date '+%Y')"
- time="$(date '+%H-%M-%S')"
- ##################
- ## MAIN PROGRAM ##
- ##################
- if [ ! -z "$1" ]; then
- # Check screenshot type
- case $1 in
- "$option_type_desktop")
- screenshot_type="desktop"
- ;;
- "$option_type_window")
- screenshot_type="window"
- ;;
- "$option_type_area")
- screenshot_type="area"
- ;;
- *)
- echo "Parameter error!"
- printParmError
- exit 1
- ;;
- esac
- # Check command-line options
- for option in $*; do
- if [[ $option == *$option_no_upload* ]]; then
- no_upload=true
- elif [[ $option == *$option_force_upload* ]]; then
- force_upload=true
- elif [[ $option == *$option_no_save* ]]; then
- no_save=true
- fi
- done
- # File and folder management
- if [ $no_save ]; then
- echo a
- file_path="/tmp/screenshot-$time.$file_extension"
- uploadlog_path="/dev/null"
- else
- if $link_on_desktop_enabled; then
- if [[ ! -e "$HOME/Desktop" ]]; then
- if [[ -e "$HOME/Arbeitsfläche" ]]; then
- ln -s "$HOME/Arbeitsfläche" "$HOME/Desktop"
- else
- mkdir "$HOME/Desktop"
- fi
- fi
- if [[ ! -e "$HOME/Desktop/screenshots" ]]; then
- ln -s "$basepath" "$HOME/Desktop/screenshots"
- fi
- fi
- file_dir="$file_basedir/$year/$month/$day"
- if [[ ! -e "$file_dir" ]]; then
- mkdir -p "$file_dir"
- fi
- unlink "$file_basedir/This Month" 2> /dev/null
- unlink "$file_basedir/Today" 2> /dev/null
- ln -s "$file_basedir/$year/$month" "$file_basedir/This Month"
- ln -s "$file_basedir/$year/$month/$day" "$file_basedir/Today"
- file_path="$file_dir/screenshot-$time.$file_extension"
- uploadlog_path="$file_dir/$uploadlog_file_name"
- fi
- # Take the screenshot
- sleep $sleep_time
- case $screenshot_type in
- "desktop")
- $scrot_cmd -q 0 "$file_path"
- ;;
- "window")
- $scrot_cmd -u -b -q 0 "$file_path"
- ;;
- "area")
- $scrot_cmd -s -b -q 0 "$file_path"
- ;;
- *)
- exit 1
- ;;
- esac
- # Upload screenshot
- if [ ! $no_upload ]; then
- if [ $force_upload ]; then
- doUpload
- elif zenity --question --text "Do you want to upload the screenshot to imgur?" --title "Question"; then
- doUpload
- fi
- fi
- # Remove
- if [ $no_save ]; then
- rm "$file_path"
- fi
- exit 0
- else
- printParmError
- fi
Advertisement
Add Comment
Please, Sign In to add comment