From 6d05beed0b8d6dd76063d26ef759624a0f657670 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Fri, 13 May 2022 06:46:53 +0300 Subject: [PATCH 1/7] update flameshot script to be global - wayland support - experimental local screenshots if domain & key are left empty --- flameshot_example.sh | 27 --------------- screenshot_example.sh | 80 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 27 deletions(-) delete mode 100644 flameshot_example.sh create mode 100644 screenshot_example.sh diff --git a/flameshot_example.sh b/flameshot_example.sh deleted file mode 100644 index e40981e..0000000 --- a/flameshot_example.sh +++ /dev/null @@ -1,27 +0,0 @@ -#!/bin/bash -IMAGEPATH="$HOME/Pictures/" # Where to store screenshots before they're deleted -IMAGENAME="ass" # Not really important, tells Flameshot what file to send and delete -KEY="" # Your ass upload token -DOMAIN="" # Your upload domain (without http:// or https://) - -flameshot config -f "$IMAGENAME" # Make sure that Flameshot names the file correctly -flameshot gui -r -p "$IMAGEPATH" > /dev/null # Prompt the screenshot GUI, also append the random gibberish to /dev/null - -FILE="$IMAGEPATH$IMAGENAME.png" # File path and file name combined - -# Check if file exists to handle Curl and rm errors -# then upload the image and copy the response URL -if [ -f "$FILE" ]; then - echo "$FILE exists." - URL=$(curl -X POST \ - -H "Content-Type: multipart/form-data" \ - -H "Accept: application/json" \ - -H "User-Agent: ShareX/13.4.0" \ - -H "Authorization: $KEY" \ - -F "file=@$IMAGEPATH$IMAGENAME.png" "https://$DOMAIN/" | grep -Po '(?<="resource":")[^"]+') - # printf instead of echo as echo appends a newline - printf "%s" "$URL" | xclip -sel clip - rm "$IMAGEPATH$IMAGENAME.png" # Delete the image locally -else - echo "Aborted." -fi diff --git a/screenshot_example.sh b/screenshot_example.sh new file mode 100644 index 0000000..3799103 --- /dev/null +++ b/screenshot_example.sh @@ -0,0 +1,80 @@ +#!/bin/bash + +# File Options +IMAGEPATH="$HOME/Pictures/" # Where to store screenshots before they are deleted +IMAGENAME="ass" # Not really important, tells this script which image to send and delete +FILE="$IMAGEPATH$IMAGENAME" # for future convenience, please do not edit + + +# Authentication +# If left empty, a local screenshot will be taken and copied to your clipboard. +KEY="" # Your ass upload token +DOMAIN="" # Your upload domain (without http:// or https://) + +# helper function to take flameshot screenshots +takeFlameshot () { + flameshot config -f "${IMAGENAME}" + flameshot gui -r -p "${IMAGEPATH}" > /dev/null +} + +# helper function to take screenshots on wayland using grim + slurp for region capture +takeGrimshot () { + grim -g "$(slurp)" "${FILE}.png" > /dev/null +} + +# decide on the screenshot tool(s) based on display backend +if [[ "${XDG_SESSION_TYPE}" == x11 ]]; then + echo -en "Display backend detected as Xorg (x11), using Flameshot\n" # append new line so that Flameshot messages start at a new line + takeFlameshot # call for the flameshot function +elif [[ "${XDG_SESSION_TYPE}" == wayland ]]; then + echo -en "Display backend detected as Wayland, using grim & slurp" + takeGrimshot # call for the grim function +else + echo -en "Unknown display backend.\n Assuming Xorg and using Flameshot." + takeFlameshot > ./Flameshot.log # will be full of gibberish, but we try it anyway + echo -en "Done. Make sure you check for any errors and report them.\nLogfile located in $(pwd)" +fi + +# check if the screenshot file actually exists before proceeding with uploading/copying to clipboard +if [[ -f "${FILE}.png" ]]; then + # check if KEY and DOMAIN are correct, + # if they are, upload the file to the ass instance + # if not, copy the image to clipboard and exit + if [[ ( -z ${KEY+x} && -v ${DOMAIN+x} ) ]]; then + URL=$(curl -X POST \ + -H "Content-Type: multipart/form-data" \ + -H "Accept: application/json" \ + -H "User-Agent: ShareX/13.4.0" \ + -H "Authorization: $KEY" \ + -F "file=@$FILE" "https://$DOMAIN/" | grep -Po '(?<="resource":")[^"]+') + if [[ "${XDG_SESSION_TYPE}" == x11 ]]; then + # printf instead of echo as echo appends a newline + printf "%s" "$URL" | xclip -sel clip # it is safe to use xclip on xorg, so we don't need wl-copy + elif [[ "${XDG_SESSION_TYPE}" == wayland ]]; then + # if desktop session is wayland instead of xclip, use wl-copy + printf "%s" "$URL" | wl-copy + else + echo -en "Invalid desktop session!\n Exiting." + exit 1 + fi + rm "${FILE}" # Delete the image locally + exit 1 + else + # If domain & key are not set, assume it is a local screenshot and copy the image directly to clipboard + if [[ "${XDG_SESSION_TYPE}" == x11 ]]; then # if display backend is x11, use xclip + # TODO: find a way to copy image to clipboard on qt environments, like KDE Plasma + echo -en "Local screenshots are only available on Wayland (for now).\nExiting..." + exit 1 + elif [[ "${XDG_SESSION_TYPE}" == wayland ]]; then # if display backend is wayland, use wl-clipboard (available on AUR or can be self-compiled) + wl-copy < "${FILE}" + exit 1 + else + echo -en "Local screenshots are only available on Wayland (for now).\nExiting..." + #TODO: find a way to copy image to clipboard on qt environments, like KDE Plasma + exit 1 + fi + fi +else + # Abort screenshot if $FILE.png does not exist + echo "Screenshot aborted." +fi From 6b7610a41421b16b09827f5734361a477bb41798 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Fri, 13 May 2022 07:22:19 +0300 Subject: [PATCH 2/7] implement local screenshots on x11 --- screenshot_example.sh | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/screenshot_example.sh b/screenshot_example.sh index 3799103..347dd06 100644 --- a/screenshot_example.sh +++ b/screenshot_example.sh @@ -51,30 +51,26 @@ if [[ -f "${FILE}.png" ]]; then # printf instead of echo as echo appends a newline printf "%s" "$URL" | xclip -sel clip # it is safe to use xclip on xorg, so we don't need wl-copy elif [[ "${XDG_SESSION_TYPE}" == wayland ]]; then - # if desktop session is wayland instead of xclip, use wl-copy + # if desktop session is wayland instead of xclip, use wl-copy (wl-clipboard) printf "%s" "$URL" | wl-copy else echo -en "Invalid desktop session!\n Exiting." exit 1 fi - rm "${FILE}" # Delete the image locally - exit 1 + rm "${FILE}.png" # Delete the image locally else # If domain & key are not set, assume it is a local screenshot and copy the image directly to clipboard if [[ "${XDG_SESSION_TYPE}" == x11 ]]; then # if display backend is x11, use xclip - # TODO: find a way to copy image to clipboard on qt environments, like KDE Plasma - echo -en "Local screenshots are only available on Wayland (for now).\nExiting..." - exit 1 + cat "${FILE}.png" | xclip -sel clip -target image/png elif [[ "${XDG_SESSION_TYPE}" == wayland ]]; then # if display backend is wayland, use wl-clipboard (available on AUR or can be self-compiled) wl-copy < "${FILE}" - exit 1 else - echo -en "Local screenshots are only available on Wayland (for now).\nExiting..." - #TODO: find a way to copy image to clipboard on qt environments, like KDE Plasma - exit 1 + echo -en "Unknown display backend... Assuming Xorg and using xclip..." + cat "${FILE}.png" | xclip -sel clip -target image/png fi fi + rm "${FILE}.png" else # Abort screenshot if $FILE.png does not exist - echo "Screenshot aborted." + echo -en "Target file was not found.\nAbording screenshot." fi From bda4800fc8a638cdfd98ced92da842e29bdae24f Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Fri, 13 May 2022 07:27:13 +0300 Subject: [PATCH 3/7] performance fix for the shell checker --- ...shot_example.sh => sample_screenshotter.sh | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) rename screenshot_example.sh => sample_screenshotter.sh (50%) diff --git a/screenshot_example.sh b/sample_screenshotter.sh similarity index 50% rename from screenshot_example.sh rename to sample_screenshotter.sh index 347dd06..e0a80a3 100644 --- a/screenshot_example.sh +++ b/sample_screenshotter.sh @@ -66,7 +66,83 @@ if [[ -f "${FILE}.png" ]]; then wl-copy < "${FILE}" else echo -en "Unknown display backend... Assuming Xorg and using xclip..." + #!/bin/bash + +# File Options +IMAGEPATH="$HOME/Pictures/" # Where to store screenshots before they are deleted +IMAGENAME="ass" # Not really important, tells this script which image to send and delete +FILE="$IMAGEPATH$IMAGENAME" # for future convenience, please do not edit + + +# Authentication +# If left empty, a local screenshot will be taken and copied to your clipboard. +KEY="" # Your ass upload token +DOMAIN="" # Your upload domain (without http:// or https://) + +# helper function to take flameshot screenshots +takeFlameshot () { + flameshot config -f "${IMAGENAME}" + flameshot gui -r -p "${IMAGEPATH}" > /dev/null +} + +# helper function to take screenshots on wayland using grim + slurp for region capture +takeGrimshot () { + grim -g "$(slurp)" "${FILE}.png" > /dev/null +} + +# decide on the screenshot tool(s) based on display backend +if [[ "${XDG_SESSION_TYPE}" == x11 ]]; then + echo -en "Display backend detected as Xorg (x11), using Flameshot\n" # append new line so that Flameshot messages start at a new line + takeFlameshot # call for the flameshot function +elif [[ "${XDG_SESSION_TYPE}" == wayland ]]; then + echo -en "Display backend detected as Wayland, using grim & slurp" + takeGrimshot # call for the grim function +else + echo -en "Unknown display backend.\n Assuming Xorg and using Flameshot." + takeFlameshot > ./Flameshot.log # will be full of gibberish, but we try it anyway + echo -en "Done. Make sure you check for any errors and report them.\nLogfile located in $(pwd)" +fi + +# check if the screenshot file actually exists before proceeding with uploading/copying to clipboard +if [[ -f "${FILE}.png" ]]; then + # check if KEY and DOMAIN are correct, + # if they are, upload the file to the ass instance + # if not, copy the image to clipboard and exit + if [[ ( -z ${KEY+x} && -v ${DOMAIN+x} ) ]]; then + URL=$(curl -X POST \ + -H "Content-Type: multipart/form-data" \ + -H "Accept: application/json" \ + -H "User-Agent: ShareX/13.4.0" \ + -H "Authorization: $KEY" \ + -F "file=@$FILE" "https://$DOMAIN/" | grep -Po '(?<="resource":")[^"]+') + if [[ "${XDG_SESSION_TYPE}" == x11 ]]; then + # printf instead of echo as echo appends a newline + printf "%s" "$URL" | xclip -sel clip # it is safe to use xclip on xorg, so we don't need wl-copy + elif [[ "${XDG_SESSION_TYPE}" == wayland ]]; then + # if desktop session is wayland instead of xclip, use wl-copy (wl-clipboard) + printf "%s" "$URL" | wl-copy + else + echo -en "Invalid desktop session!\n Exiting." + exit 1 + fi + rm "${FILE}.png" # Delete the image locally + else + # If domain & key are not set, assume it is a local screenshot and copy the image directly to clipboard + if [[ "${XDG_SESSION_TYPE}" == x11 ]]; then # if display backend is x11, use xclip cat "${FILE}.png" | xclip -sel clip -target image/png + elif [[ "${XDG_SESSION_TYPE}" == wayland ]]; then # if display backend is wayland, use wl-clipboard (available on AUR or can be self-compiled) + wl-copy < "${FILE}" + else + echo -en "Unknown display backend... Assuming Xorg and using xclip..." + xclip -sel clip -target image/png < "${FILE}.png" + fi + fi + rm "${FILE}.png" +else + # Abort screenshot if $FILE.png does not exist + echo -en "Target file was not found.\nAbording screenshot." +fi + fi fi rm "${FILE}.png" From 88c391b02b1dbf7ce5241f1058b5da02eb453e60 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Fri, 13 May 2022 07:31:12 +0300 Subject: [PATCH 4/7] performance fix 2 --- sample_screenshotter.sh | 78 +---------------------------------------- 1 file changed, 1 insertion(+), 77 deletions(-) diff --git a/sample_screenshotter.sh b/sample_screenshotter.sh index e0a80a3..86f9893 100644 --- a/sample_screenshotter.sh +++ b/sample_screenshotter.sh @@ -61,75 +61,7 @@ if [[ -f "${FILE}.png" ]]; then else # If domain & key are not set, assume it is a local screenshot and copy the image directly to clipboard if [[ "${XDG_SESSION_TYPE}" == x11 ]]; then # if display backend is x11, use xclip - cat "${FILE}.png" | xclip -sel clip -target image/png - elif [[ "${XDG_SESSION_TYPE}" == wayland ]]; then # if display backend is wayland, use wl-clipboard (available on AUR or can be self-compiled) - wl-copy < "${FILE}" - else - echo -en "Unknown display backend... Assuming Xorg and using xclip..." - #!/bin/bash - -# File Options -IMAGEPATH="$HOME/Pictures/" # Where to store screenshots before they are deleted -IMAGENAME="ass" # Not really important, tells this script which image to send and delete -FILE="$IMAGEPATH$IMAGENAME" # for future convenience, please do not edit - - -# Authentication -# If left empty, a local screenshot will be taken and copied to your clipboard. -KEY="" # Your ass upload token -DOMAIN="" # Your upload domain (without http:// or https://) - -# helper function to take flameshot screenshots -takeFlameshot () { - flameshot config -f "${IMAGENAME}" - flameshot gui -r -p "${IMAGEPATH}" > /dev/null -} - -# helper function to take screenshots on wayland using grim + slurp for region capture -takeGrimshot () { - grim -g "$(slurp)" "${FILE}.png" > /dev/null -} - -# decide on the screenshot tool(s) based on display backend -if [[ "${XDG_SESSION_TYPE}" == x11 ]]; then - echo -en "Display backend detected as Xorg (x11), using Flameshot\n" # append new line so that Flameshot messages start at a new line - takeFlameshot # call for the flameshot function -elif [[ "${XDG_SESSION_TYPE}" == wayland ]]; then - echo -en "Display backend detected as Wayland, using grim & slurp" - takeGrimshot # call for the grim function -else - echo -en "Unknown display backend.\n Assuming Xorg and using Flameshot." - takeFlameshot > ./Flameshot.log # will be full of gibberish, but we try it anyway - echo -en "Done. Make sure you check for any errors and report them.\nLogfile located in $(pwd)" -fi - -# check if the screenshot file actually exists before proceeding with uploading/copying to clipboard -if [[ -f "${FILE}.png" ]]; then - # check if KEY and DOMAIN are correct, - # if they are, upload the file to the ass instance - # if not, copy the image to clipboard and exit - if [[ ( -z ${KEY+x} && -v ${DOMAIN+x} ) ]]; then - URL=$(curl -X POST \ - -H "Content-Type: multipart/form-data" \ - -H "Accept: application/json" \ - -H "User-Agent: ShareX/13.4.0" \ - -H "Authorization: $KEY" \ - -F "file=@$FILE" "https://$DOMAIN/" | grep -Po '(?<="resource":")[^"]+') - if [[ "${XDG_SESSION_TYPE}" == x11 ]]; then - # printf instead of echo as echo appends a newline - printf "%s" "$URL" | xclip -sel clip # it is safe to use xclip on xorg, so we don't need wl-copy - elif [[ "${XDG_SESSION_TYPE}" == wayland ]]; then - # if desktop session is wayland instead of xclip, use wl-copy (wl-clipboard) - printf "%s" "$URL" | wl-copy - else - echo -en "Invalid desktop session!\n Exiting." - exit 1 - fi - rm "${FILE}.png" # Delete the image locally - else - # If domain & key are not set, assume it is a local screenshot and copy the image directly to clipboard - if [[ "${XDG_SESSION_TYPE}" == x11 ]]; then # if display backend is x11, use xclip - cat "${FILE}.png" | xclip -sel clip -target image/png + xclip -sel clip -target image/png < "${FILE}.png" elif [[ "${XDG_SESSION_TYPE}" == wayland ]]; then # if display backend is wayland, use wl-clipboard (available on AUR or can be self-compiled) wl-copy < "${FILE}" else @@ -142,11 +74,3 @@ else # Abort screenshot if $FILE.png does not exist echo -en "Target file was not found.\nAbording screenshot." fi - - fi - fi - rm "${FILE}.png" -else - # Abort screenshot if $FILE.png does not exist - echo -en "Target file was not found.\nAbording screenshot." -fi From 39906e7f7cf91aaa5341c563c526c80bdcd93c49 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Fri, 13 May 2022 07:48:03 +0300 Subject: [PATCH 5/7] ready for production --- sample_screenshotter.sh | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/sample_screenshotter.sh b/sample_screenshotter.sh index 86f9893..0c21a9c 100644 --- a/sample_screenshotter.sh +++ b/sample_screenshotter.sh @@ -11,6 +11,7 @@ FILE="$IMAGEPATH$IMAGENAME" # for future convenience, please do not edit KEY="" # Your ass upload token DOMAIN="" # Your upload domain (without http:// or https://) + # helper function to take flameshot screenshots takeFlameshot () { flameshot config -f "${IMAGENAME}" @@ -27,12 +28,12 @@ if [[ "${XDG_SESSION_TYPE}" == x11 ]]; then echo -en "Display backend detected as Xorg (x11), using Flameshot\n" # append new line so that Flameshot messages start at a new line takeFlameshot # call for the flameshot function elif [[ "${XDG_SESSION_TYPE}" == wayland ]]; then - echo -en "Display backend detected as Wayland, using grim & slurp" + echo -en "Display backend detected as Wayland, using grim & slurp\n" takeGrimshot # call for the grim function else - echo -en "Unknown display backend.\n Assuming Xorg and using Flameshot." + echo -en "Unknown display backend.\n Assuming Xorg and using Flameshot.\n" takeFlameshot > ./Flameshot.log # will be full of gibberish, but we try it anyway - echo -en "Done. Make sure you check for any errors and report them.\nLogfile located in $(pwd)" + echo -en "Done. Make sure you check for any errors and report them.\nLogfile located in $(pwd)\n" fi # check if the screenshot file actually exists before proceeding with uploading/copying to clipboard @@ -40,13 +41,14 @@ if [[ -f "${FILE}.png" ]]; then # check if KEY and DOMAIN are correct, # if they are, upload the file to the ass instance # if not, copy the image to clipboard and exit - if [[ ( -z ${KEY+x} && -v ${DOMAIN+x} ) ]]; then + if [[ ( -n "${KEY}" && -n "${DOMAIN}" ) ]]; then + echo -en "KEY & DOMAIN are set\nAttempting to upload to your ass instance.\n" URL=$(curl -X POST \ -H "Content-Type: multipart/form-data" \ -H "Accept: application/json" \ -H "User-Agent: ShareX/13.4.0" \ -H "Authorization: $KEY" \ - -F "file=@$FILE" "https://$DOMAIN/" | grep -Po '(?<="resource":")[^"]+') + -F "file=@${FILE}.png" "https://$DOMAIN/" | grep -Po '(?<="resource":")[^"]+') if [[ "${XDG_SESSION_TYPE}" == x11 ]]; then # printf instead of echo as echo appends a newline printf "%s" "$URL" | xclip -sel clip # it is safe to use xclip on xorg, so we don't need wl-copy @@ -54,23 +56,26 @@ if [[ -f "${FILE}.png" ]]; then # if desktop session is wayland instead of xclip, use wl-copy (wl-clipboard) printf "%s" "$URL" | wl-copy else - echo -en "Invalid desktop session!\n Exiting." + echo -en "Invalid desktop session!\n Exiting.\n" exit 1 fi + echo -en "Process complete.\nRemoving image.\n" rm "${FILE}.png" # Delete the image locally else + echo -en "KEY & DOMAIN are not set\nAttempting local screenshot.\n" # If domain & key are not set, assume it is a local screenshot and copy the image directly to clipboard if [[ "${XDG_SESSION_TYPE}" == x11 ]]; then # if display backend is x11, use xclip xclip -sel clip -target image/png < "${FILE}.png" elif [[ "${XDG_SESSION_TYPE}" == wayland ]]; then # if display backend is wayland, use wl-clipboard (available on AUR or can be self-compiled) wl-copy < "${FILE}" else - echo -en "Unknown display backend... Assuming Xorg and using xclip..." + echo -en "Unknown display backend...\nAssuming Xorg and using xclip...\n" xclip -sel clip -target image/png < "${FILE}.png" fi + echo -en "Process complete.\nRemoving image.\n" + rm "${FILE}.png" fi - rm "${FILE}.png" else # Abort screenshot if $FILE.png does not exist - echo -en "Target file was not found.\nAbording screenshot." + echo -en "Target file was not found.\nAbording screenshot.\n" fi From e201da1e5535f3415a6b6089dd02859073a00505 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Sat, 14 Oct 2023 19:23:22 +0300 Subject: [PATCH 6/7] refactor: modularize screenshot script --- sample_screenshotter.sh | 144 ++++++++++++++++++++++------------------ 1 file changed, 79 insertions(+), 65 deletions(-) diff --git a/sample_screenshotter.sh b/sample_screenshotter.sh index 0c21a9c..c577ce1 100644 --- a/sample_screenshotter.sh +++ b/sample_screenshotter.sh @@ -1,81 +1,95 @@ -#!/bin/bash +#!/usr/bin/env bash -# File Options -IMAGEPATH="$HOME/Pictures/" # Where to store screenshots before they are deleted -IMAGENAME="ass" # Not really important, tells this script which image to send and delete -FILE="$IMAGEPATH$IMAGENAME" # for future convenience, please do not edit +# Script Configuration +IMAGEPATH="$HOME/Pictures" +IMAGENAME="ass" +FILE="${IMAGEPATH}/${IMAGENAME}.png" +LOG_DIR=$(pwd) +CONFIG_FILE="config.sh" +# Load configuration if available +# this is useful if you want to source keys from a secret file +if [ -f "$CONFIG_FILE" ]; then + source "${CONFIG_FILE}" +fi -# Authentication -# If left empty, a local screenshot will be taken and copied to your clipboard. -KEY="" # Your ass upload token -DOMAIN="" # Your upload domain (without http:// or https://) +# Function to take Flameshot screenshots +takeFlameshot() { + flameshot config -f "${IMAGENAME}" + flameshot gui -r -p "${IMAGEPATH}" >/dev/null +} +# Function to take Wayland screenshots using grim + slurp +takeGrimshot() { + grim -g "$(slurp)" "${FILE}" >/dev/null +} -# helper function to take flameshot screenshots -takeFlameshot () { - flameshot config -f "${IMAGENAME}" - flameshot gui -r -p "${IMAGEPATH}" > /dev/null +# Function to remove the taken screenshot +removeTargetFile() { + echo -en "Process complete.\nRemoving image.\n" + rm -v "${FILE}" } -# helper function to take screenshots on wayland using grim + slurp for region capture -takeGrimshot () { - grim -g "$(slurp)" "${FILE}.png" > /dev/null +# Function to upload target image to your ass instance +uploadScreenshot() { + echo -en "KEY & DOMAIN are set. Attempting to upload to your ass instance.\n" + URL=$(curl -X POST \ + -H "Content-Type: multipart/form-data" \ + -H "Accept: application/json" \ + -H "User-Agent: ShareX/13.4.0" \ + -H "Authorization: $KEY" \ + -F "file=@${FILE}" "https://$DOMAIN/" | grep -Po '(?<="resource":")[^"]+') + if [[ "${XDG_SESSION_TYPE}" == x11 ]]; then + printf "%s" "$URL" | xclip -sel clip + elif [[ "${XDG_SESSION_TYPE}" == wayland ]]; then + printf "%s" "$URL" | wl-copy + else + echo -en "Invalid desktop session!\nExiting.\n" + exit 1 + fi } -# decide on the screenshot tool(s) based on display backend +localScreenshot() { + echo -en "KEY & DOMAIN variables are not set. Attempting local screenshot.\n" + if [[ "${XDG_SESSION_TYPE}" == x11 ]]; then + xclip -sel clip -target image/png <"${FILE}" + elif [[ "${XDG_SESSION_TYPE}" == wayland ]]; then + wl-copy <"${FILE}" + else + echo -en "Unknown display backend. Assuming Xorg and using xclip.\n" + xclip -sel clip -target image/png <"${FILE}" + fi +} + +# Check if the screenshot tool based on display backend if [[ "${XDG_SESSION_TYPE}" == x11 ]]; then - echo -en "Display backend detected as Xorg (x11), using Flameshot\n" # append new line so that Flameshot messages start at a new line - takeFlameshot # call for the flameshot function + echo -en "Display backend detected as Xorg (x11), using Flameshot\n" + takeFlameshot elif [[ "${XDG_SESSION_TYPE}" == wayland ]]; then - echo -en "Display backend detected as Wayland, using grim & slurp\n" - takeGrimshot # call for the grim function + echo -en "Display backend detected as Wayland, using grim & slurp\n" + takeGrimshot else - echo -en "Unknown display backend.\n Assuming Xorg and using Flameshot.\n" - takeFlameshot > ./Flameshot.log # will be full of gibberish, but we try it anyway - echo -en "Done. Make sure you check for any errors and report them.\nLogfile located in $(pwd)\n" + echo -en "Unknown display backend. Assuming Xorg and using Flameshot\n" + takeFlameshot >"${LOG_DIR}/flameshot.log" + echo -en "Done. Make sure you check for any errors and report them.\nLogfile located in '${LOG_DIR}'\n" fi -# check if the screenshot file actually exists before proceeding with uploading/copying to clipboard -if [[ -f "${FILE}.png" ]]; then - # check if KEY and DOMAIN are correct, - # if they are, upload the file to the ass instance - # if not, copy the image to clipboard and exit - if [[ ( -n "${KEY}" && -n "${DOMAIN}" ) ]]; then - echo -en "KEY & DOMAIN are set\nAttempting to upload to your ass instance.\n" - URL=$(curl -X POST \ - -H "Content-Type: multipart/form-data" \ - -H "Accept: application/json" \ - -H "User-Agent: ShareX/13.4.0" \ - -H "Authorization: $KEY" \ - -F "file=@${FILE}.png" "https://$DOMAIN/" | grep -Po '(?<="resource":")[^"]+') - if [[ "${XDG_SESSION_TYPE}" == x11 ]]; then - # printf instead of echo as echo appends a newline - printf "%s" "$URL" | xclip -sel clip # it is safe to use xclip on xorg, so we don't need wl-copy - elif [[ "${XDG_SESSION_TYPE}" == wayland ]]; then - # if desktop session is wayland instead of xclip, use wl-copy (wl-clipboard) - printf "%s" "$URL" | wl-copy - else - echo -en "Invalid desktop session!\n Exiting.\n" - exit 1 - fi - echo -en "Process complete.\nRemoving image.\n" - rm "${FILE}.png" # Delete the image locally - else - echo -en "KEY & DOMAIN are not set\nAttempting local screenshot.\n" - # If domain & key are not set, assume it is a local screenshot and copy the image directly to clipboard - if [[ "${XDG_SESSION_TYPE}" == x11 ]]; then # if display backend is x11, use xclip - xclip -sel clip -target image/png < "${FILE}.png" - elif [[ "${XDG_SESSION_TYPE}" == wayland ]]; then # if display backend is wayland, use wl-clipboard (available on AUR or can be self-compiled) - wl-copy < "${FILE}" - else - echo -en "Unknown display backend...\nAssuming Xorg and using xclip...\n" - xclip -sel clip -target image/png < "${FILE}.png" - fi - echo -en "Process complete.\nRemoving image.\n" - rm "${FILE}.png" - fi +# Check if the screenshot file exists before proceeding +if [[ -f "${FILE}" ]]; then + if [[ -n "$KEY" && -n "$DOMAIN" ]]; then + # Upload the file to the ass instance + uploadImage + + # Remove image + removeTargetFile + else + # Take a screenshot locally + localScreenshot + + # Remove image + removeTargetFile + fi else - # Abort screenshot if $FILE.png does not exist - echo -en "Target file was not found.\nAbording screenshot.\n" + echo -en "Target file ${FILE} was not found. Aborting screenshot.\n" + exit 1 fi From 25a32b387986315c9e33f71757efc97899178e84 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Sat, 14 Oct 2023 19:38:26 +0300 Subject: [PATCH 7/7] feat: sanity checks in sample screenshotter --- sample_screenshotter.sh | 59 ++++++++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 9 deletions(-) diff --git a/sample_screenshotter.sh b/sample_screenshotter.sh index c577ce1..d6f3603 100644 --- a/sample_screenshotter.sh +++ b/sample_screenshotter.sh @@ -1,26 +1,67 @@ #!/usr/bin/env bash # Script Configuration -IMAGEPATH="$HOME/Pictures" -IMAGENAME="ass" -FILE="${IMAGEPATH}/${IMAGENAME}.png" -LOG_DIR=$(pwd) -CONFIG_FILE="config.sh" - -# Load configuration if available +# Load configuration file if available # this is useful if you want to source keys from a secret file +CONFIG_FILE="config.sh" if [ -f "$CONFIG_FILE" ]; then + # shellcheck disable=1090 source "${CONFIG_FILE}" fi +LOG_DIR=$(pwd) +if [ ! -d "$LOG_DIR" ]; then + echo "The directory you have specified to save the logs does not exist." + echo "Please create the directory with the following command:" + echo "mkdir -p $LOG_DIR" + echo -en "Or specify a different LOG_DIR\n" + exit 1 +fi + +IMAGE_PATH="$HOME/Pictures" +if [ ! -d "$IMAGE_PATH" ]; then + echo "The directory you have specified to save the screenshot does not exist." + echo "Please create the directory with the following command:" + echo "mkdir -p $IMAGE_PATH" + echo -en "Or specify a different IMAGE_PATH\n" + exit 1 +fi + +IMAGE_NAME="ass" +FILE="${IMAGE_PATH}/${IMAGE_NAME}.png" + +# Function to check if a tool is installed +check_tool() { + command -v "$1" >/dev/null 2>&1 +} + # Function to take Flameshot screenshots takeFlameshot() { - flameshot config -f "${IMAGENAME}" - flameshot gui -r -p "${IMAGEPATH}" >/dev/null + # check if flameshot tool is installed + REQUIRED_TOOLS=("flameshot") + + for tool in "${REQUIRED_TOOLS[@]}"; do + if ! check_tool "$tool"; then + echo "Error: $tool is not installed. Please install it before using this script." + exit 1 + fi + done + + flameshot config -f "${IMAGE_NAME}" + flameshot gui -r -p "${IMAGE_PATH}" >/dev/null } # Function to take Wayland screenshots using grim + slurp takeGrimshot() { + # check if grim and slurp are installed + REQUIRED_TOOLS=("grim" "slurp") + for tool in "${REQUIRED_TOOLS[@]}"; do + if ! check_tool "$tool"; then + echo "Error: $tool is not installed. Please install it before using this script." + exit 1 + fi + done + grim -g "$(slurp)" "${FILE}" >/dev/null }