📜 ⬆️ ⬇️

How do i take screenshots in linux

In this post I want to perpetuate my bike, which I invented for easy creation of screenshots. Yes, I know about the existence of such programs as shutter, but they are all uncomfortable for one reason or another, and since there was interest and time, I took up this problem.



How does my bike work


The user clicks PrintScreen, a menu appears in which the following questions are asked:
  1. Save full screen or only part (window)?
  2. Save the file with a snapshot somewhere for long memory or save in unreliable / tmp?
  3. Enter the file name yourself, generate random or generate a template?
  4. If according to the template, it is proposed to choose a template or enter it yourself
  5. Select a delay in seconds before taking a picture.
  6. Whether to copy the URL of the published image to clipboard
  7. Show whether the dialogue with the path to the file on the disk, as well as the URL of the image

Breaking through this bunch of questions, the program:
  1. Specifies the file name (if needed)
  2. Makes a screenshot of the screen or waits until the user selects a region or window (man scrot)
  3. If the user has entered a file name that already exists, a confirmation will be displayed for overwriting. In case of cancellation, the user will have to call the script himself again (he himself is not yet so smart as to ask for another file name)
  4. The log file records the path to the saved file and the URL of the published image.
  5. Screenshot URL is copied to clipboard (if necessary)
  6. A dialog is displayed with the path to the file on disk and the URL of the published image (if necessary)

')
A script called without arguments will ask questions again each time. However, having remembered the user's answers, he caches them and writes them to a file (by default /tmp/screenshot.cache.sh ). If you run the script with the cached argument, the script will try to read the cached responses, and if it succeeds, it will not ask questions.

Thus, by setting PrintScreen to call the script with loading the cache, and Win + PrintScreen to calling without loading the cache, I can always change the settings by answering the questions again, and then taking pictures further much faster.

Disclaimer


My ArchLinux distribution and I use openbox as a window manager. The lucky owners of KDE / Gnome / Xfce / your_variant may have a different installation and use process.

Construction Materials


  1. dmenu (show menu)
  2. scrot (screenshot)
  3. zenity (asking questions from the user)
  4. xclip (copy link to clipboard)
  5. imagepaste (upload image at image hosting)
  6. script (put all the components together)
  7. terminus-font (fonts in the menu, optional)

The first four programs should be (in theory) available in the repositories of your distribution. The last item is optional. You can use other fonts if terminus is not suitable for you. The fifth point is my own craft, the meaning of which is reduced to uploading a picture to sites like ImageShack.us with one command from the command line. Assembly of this miracle, generally speaking, is not so complicated, but the description of this process is in any case beyond the scope of this note, so you are free to replace this component with alternative solutions .

Step-by-step instruction

This is how the installation would look like it would start to end in my distribution.
Install the standard packages:
 $ pacman -S dmenu scrot zenity xclip terminus-font 

Let Xam know the path to these fonts (plus, it would be nice to write this command in ~ / .xinitrc):
 $ xset +fp /usr/share/fonts/local/ 

We believe that imagepaste (or equivalent) is already installed, so skip this step.
Install the script:
 $ mkdir ~/bin $ cd !$ $ wget https://bitbucket.org/balta2ar/screenshot/raw/45ad932db13b/screenshot.sh $ chmod u+x ./screenshot.sh 

My ~ / bin folder is used for my kneaded crafts. In the script you probably want to change:
LOG - the path to the log file
FONT - menu font
IMAGEPASTE_CMD - command for publishing pictures
BASEDIR is the base directory for screenshots you want to save.

Do not forget to create a log file:
 $ touch /var/log/screenshot.log $ sudo chown `whoami`:users !$ 

And the last thing is to hang up the script call on the hotkey. I used the possibilities of openbox for this, namely obkey for setting hotkeys. You can, however, immediately get into ~ / .config / openbox / rc.xml and add:
 <keybind key="Print"> <action name="Execute"> <command>/home/bz/bin/screenshot.sh cached</command> </action> </keybind> <keybind key="W-Print"> <action name="Execute"> <command>/home/bz/bin/screenshot.sh</command> </action> </keybind> 


pros



Minuses



Entire script

 #!/bin/bash init() { ACTIONS[0]="window/region,screen" ACTIONS[1]="drop,save" ACTIONS[2]="create random,ask,incremental pattern" ACTIONS[3]="screenshot-%05d.png" ACTIONS[4]="0,5,10,30,60" ACTIONS[5]="yes,no" ACTIONS[6]="no,yes" PROMPT[0]="Capture region" PROMPT[1]="Picture file" PROMPT[2]="File name" PROMPT[3]="File name pattern" PROMPT[4]="Delay" PROMPT[5]="Copy to clipboard" PROMPT[6]="Show links in a dialog" IND_CAPTURE_REGION=0 IND_FILESAVE=1 IND_FILENAME=2 IND_PATTERN=3 IND_DELAY=4 IND_COPY_CLIPBOARD=5 IND_SHOW_LINKS=6 LOG="/var/log/screenshot.log" FONT='-xos4-terminus-*-*-*-*-16-*-*-*-*-*-*-*' DMENU="dmenu -fn $FONT" IMAGEPASTE_CMD="imp" SCREENSHOT_CMD="scrot" SCREENSHOT_OPTIONS= OPTIONS= COUNT=${#ACTIONS[*]} BASEDIR="/home/`whoami`/pic/screenshot/scrot" CACHE="/tmp/screenshot.cache.sh" PATTERN_INDEX=0 } load_cache() { source $CACHE } save_cache() { echo -n > $CACHE for (( i = 0; i < ${#OPTIONS[*]}; i++ )) do echo "OPTIONS[$i]=\"${OPTIONS[$i]}\"" >> $CACHE done echo "PATTERN_INDEX=\"$PATTERN_INDEX\"" >> $CACHE } random_filename() { TMP=`mktemp --suffix=.scrot` BASE=`basename $TMP` FILENAME="$BASEDIR/$BASE.png" } ask_filename() { FILENAME=`zenity --entry --text "Enter filename" --entry-text "$BASEDIR/"` } set_next_pattern_filename() { FILENAME="$BASEDIR/$(printf ${OPTIONS[$IND_PATTERN]} $PATTERN_INDEX)" PATTERN_INDEX=$(($PATTERN_INDEX + 1)) while [ -e "$FILENAME" ]; do FILENAME="$BASEDIR/$(printf ${OPTIONS[$IND_PATTERN]} $PATTERN_INDEX)" PATTERN_INDEX=$(($PATTERN_INDEX + 1)) done } ask_options() { for i in $(seq 0 $(($COUNT - 1))) do # if we are about to ask patern and # it wasn't the choise in prev question, continue if [ "$i" -eq "$IND_PATTERN" -a "${OPTIONS[$IND_FILENAME]}" != "incremental pattern" ] then continue fi OP=`echo ${ACTIONS[$i]} | tr "," "\n" | $DMENU -p "${PROMPT[$i]}"` if [ ! -n "$OP" ]; then exit 0 fi OPTIONS[$i]=$OP done } parse_options() { # parse options and ask user questions, if any ITEM=${OPTIONS[$IND_CAPTURE_REGION]} case "$ITEM" in window/region) SCREENSHOT_OPTIONS="-b -s" ;; screen) ;; esac ITEM=${OPTIONS[$IND_FILESAVE]} case "$ITEM" in save) # Dont touch BASEDIR ;; drop) BASEDIR="/tmp" ;; esac ITEM=${OPTIONS[$IND_FILENAME]} case "$ITEM" in ask) ask_filename ;; "create random") random_filename ;; "incremental pattern") set_next_pattern_filename ;; esac ITEM=${OPTIONS[$IND_DELAY]} SCREENSHOT_OPTIONS="$SCREENSHOT_OPTIONS -d $ITEM" } check_existence() { if [ -e "$FILENAME" ] then zenity --question --text "File already exists. Overwrite?" OVERWRITE=$? if [ $OVERWRITE -ne 0 ] then exit 1 fi fi } do_screenshot() { rm -f "$FILENAME" mkdir -p `dirname "$FILENAME"` $SCREENSHOT_CMD $SCREENSHOT_OPTIONS "$FILENAME" CMD="$IMAGEPASTE_CMD $FILENAME" URL="`$CMD`" echo "$FILENAME -> $URL" >> $LOG } copy_to_clipboard() { if [ ${OPTIONS[$IND_COPY_CLIPBOARD]} == "yes" ] then echo "$URL" | xclip -selection primary echo "$URL" | xclip -selection clipboard fi } show_links() { if [ ${OPTIONS[$IND_SHOW_LINKS]} == "yes" ] then zenity --info --no-wrap --text "$FILENAME\n$URL" fi } init if [ -e "$CACHE" -a "$1" == "cached" ] then load_cache else ask_options fi save_cache parse_options check_existence do_screenshot copy_to_clipboard show_links exit 0 


UPD 01 : Added delay selection: 0, 5, 10, 30, 60 seconds.
UPD 02 : Added: 1) caching user responses 2) the ability to specify an incremental pattern for the file name.

Source: https://habr.com/ru/post/132064/


All Articles