Last active
February 19, 2023 03:48
-
-
Save Tschrock/a20c1ea63d9369e3a2d45e9b20fa139a to your computer and use it in GitHub Desktop.
The shell script I use for automatically uploading screenshots
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
SCREENSHOTS_DIR="$HOME/Pictures/Screenshots/" | |
REMOTE_HOST="[email protected]" | |
REMOTE_PORT="9433" | |
REMOTE_PATH="/var/www/p.cp3.es/" | |
HTTP_HOST="https://p.cp3.es/" | |
GENERATE_NAME_BTN="Generate New Name" | |
generate_filename() { | |
printf "$(date +"%s")-$(openssl rand -hex 3)" | |
} | |
is_runnable() { | |
[ -s "$1" ] && [ -x "$1" ] || [ -x "$(command -v "$1")" ]; | |
} | |
notify() { | |
NOTIFY_ICON="$1" | |
NOTIFY_TITLE="$2" | |
NOTIFY_BODY="$3" | |
if is_runnable notify-send; then | |
notify-send -i "$NOTIFY_ICON" "$NOTIFY_TITLE" "$NOTIFY_BODY" | |
elif is_runnable zenity; then | |
[[ "$NOTIFY_BODY" == "http"* ]] && NOTIFY_BODY="<a href=\"$NOTIFY_BODY\">$NOTIFY_BODY</a>" | |
zenity --info --icon-name "$NOTIFY_ICON" --title "$NOTIFY_TITLE" --text "$NOTIFY_BODY" | |
else | |
echo "$NOTIFY_TITLE" | |
echo "$NOTIFY_BODY" | |
fi | |
} | |
success() { | |
notify network-transmit "$1" "$2" | |
} | |
warning() { | |
notify dialog-warning "$1" "$2" | |
} | |
error() { | |
notify error "$1" "$2" | |
exit 1 | |
} | |
info() { | |
notify info "$1" "$2" | |
} | |
# Usage: upload_file [fileName] | |
upload_file() | |
{ | |
FILE_PATH="$1" | |
if [ -z "$FILE_PATH" ]; then | |
error "Failed to upload file" "No file specified" | |
elif [ ! -f "$FILE_PATH" ]; then | |
error "Failed to upload file" "File does not exist" | |
fi | |
if [ -n "$2" ]; then | |
FILE_NAME="$2" | |
else | |
FILE_NAME="$(basename "$FILE_PATH")" | |
fi | |
SCP_SRC="$FILE_PATH" | |
SCP_DEST="$REMOTE_HOST:$REMOTE_PATH/$FILE_NAME" | |
if ! is_runnable scp; then | |
error "Missing Dependency" "Command 'scp' not found." | |
fi | |
SCP_OUTPUT=$(scp -P "$REMOTE_PORT" "$SCP_SRC" "$SCP_DEST" 2>&1) | |
if [ "$?" == "0" ]; then | |
FILE_URL="$HTTP_HOST$FILE_NAME" | |
if is_runnable xclip; then | |
printf "$FILE_URL" | xclip -selection "clipboard" | |
else | |
warning "Can't copy to clipboard" "Please install 'xclip'" | |
fi | |
success "SCP Upload Complete" "$FILE_URL" | |
else | |
error "Error uploading file" "$SCP_OUTPUT" | |
fi | |
} | |
print_help() | |
{ | |
echo "scpupload 1.2.0 | |
Usage: scpupload [-d | -a | -w | -f [file] | -h | --help] | |
Options: | |
-d upload the entire desktop | |
-a select an area to upload | |
-w upload the focused window | |
-f [file] upload a file | |
-h|--help shows this help" | |
} | |
mkdir -p "$SCREENSHOTS_DIR" | |
# Get file to upload and upload it | |
case "$1" in | |
-d) | |
echo "Capturing Whole Desktop" | |
FILE_NAME="ss$(generate_filename).png" | |
FILE_PATH="$SCREENSHOTS_DIR$FILE_NAME" | |
gnome-screenshot -f "$FILE_PATH" | |
upload_file "$FILE_PATH" | |
;; | |
-a) | |
echo "Capturing Area" | |
FILE_NAME="ss$(generate_filename).png" | |
FILE_PATH="$SCREENSHOTS_DIR$FILE_NAME" | |
gnome-screenshot -a -f "$FILE_PATH" | |
upload_file "$FILE_PATH" | |
;; | |
-w) | |
echo "Capturing Window" | |
FILE_NAME="ss$(generate_filename).png" | |
FILE_PATH="$SCREENSHOTS_DIR$FILE_NAME" | |
gnome-screenshot -w -f "$FILE_PATH" | |
upload_file "$FILE_PATH" | |
;; | |
-f) | |
echo "File Upload" | |
if ! is_runnable zenity; then | |
error "Missing Dependency" "Command 'zenity' not found." | |
fi | |
# Get the file to upload | |
if [ -n "$2" ]; then | |
FILE_PATH="$2" | |
else | |
FILE_PATH="$(zenity --file-selection)" | |
fi | |
FILE_NAME="$(basename "$FILE_PATH")" | |
FILE_EXT="${FILE_NAME##*.}" | |
# Get the upload name | |
FILE_NAME="$(zenity --entry --title "Enter upload filename" --text "Upload Filename" --entry-text "$FILE_NAME" --extra-button "$GENERATE_NAME_BTN")" | |
if [ -z "$FILE_NAAME" ]; then | |
info "File Upload Canceled" | |
exit 0 | |
fi | |
# Check if we're generating a new name | |
if [ "$FILE_NAME" == "$GENERATE_NAME_BTN" ]; then | |
FILE_NAME="file$(generate_filename).$FILE_EXT" | |
fi | |
if [[ "$FILE_NAME" == *"{rand}"* ]]; then | |
RAND="$(openssl rand -hex 3)" | |
FILE_NAME="${FILE_NAME//{rand\}/$RAND}" | |
fi | |
if [[ "$FILE_NAME" == *"{date}"* ]]; then | |
DATE="$(date +"%s")" | |
FILE_NAME="${FILE_NAME//{date\}/$DATE}" | |
fi | |
# Upload the file | |
upload_file "$FILE_PATH" "$FILE_NAME" | |
;; | |
-h|--help) | |
print_help | |
exit 0 | |
;; | |
*) | |
echo -e "Error: No option selected\n" | |
print_help | |
exit 1 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment