📜 ⬆️ ⬇️

Easy copying of paper documents in Linux

In this article I want to demonstrate how you can screw the simplest GUI to the bash script.

Prehistory


Everyone at least once in their life faced with photocopying documents. There is a wide selection of copying devices on offer, ranging from photocopying devices to an MFP, where a scanner, a printer and a copier are combined in one device. If you have a scanner and printer, you can use the copy program. These programs come bundled with many scanner models. For the house, I preferred to have the last option, because the photocopying device copies without semitones, and the MFP, although it saves space, when one of the components fails (usually a printer), you have to throw it out completely, and the quality of the scanner is not very good for the MFP.

Just as with many scanner models, the Epson Perfection was included in my Epson Perfection photo 2480 scanner, the main drawback of which was the version available only under Windows. The search for similar programs under Linux has come to naught.
At one of the forums I was recommended to write a script and even helped with writing. Here is the script:
')
#! /bin/bash SCANNER=`sane-find-scanner -q | sed -e 's/.*at /snapscan:/'` #      TMPFILE="/tmp/scan.tiff" #     LOCKFILE="/tmp/copy.lock" #  #     #   ,       if ! lockfile-create --retry 2 -l $LOCKFILE; then exit fi scanimage --device-name $SCANNER --format tiff --mode Gray -x 210 -y 297 --resolution 300 --brightness -3 -p > $TMPFILE #.     man scanimage tiff2ps -z -w 8.27 -h 11.69 $TMPFILE | lp lockfile-remove -l $LOCKFILE #   


We fasten the interface


The above script had a huge flaw - it was impossible to specify the number of copies. After a small rework, it became possible to specify the number of copies on the command line, but this did not suit my household, who did not want to go into the console. I did not want to write a native application with an interface and did not know how. Then I learned that bash scripts can be interfaced via the dialog, zenity, kdialog programs. Well, I tried to rewrite using zenity + I added handling various errors. It turned out the addition of the interface is not easy, but very simple! This is what came of it.

First we define variables and block.

 SCANNER=`sane-find-scanner -q | sed -e 's/.*at /snapscan:/'` #      DIALOG=zenity TMPFILE="/tmp/scan.tiff" #     LOCKFILE="/tmp/copy.lock" #  DLG_COUNT_TITLE=" " DLG_COUNT_TEXT="   " DLG_PROGRESS_TITLE=" " DLG_PROGRESS_TEXT="..." MSG_START_PROCESS="    $SCANNER" MSG_SCAN="" MSG_PRINT="   " MSG_COMPLETE="   ." MSG_ERROR_LOCK=":    ,        ." MSG_ERROR_SCAN=":   ,          ." MSG_ERROR_PRINT=":   ." 


After initialization of all variables, the script asks for the number of copies. The program returns the result to the console, but we use the inverse single quotes (where the Russian is located, but in the English layout) assign the result to the COPY_COUNT variable.

 COPY_COUNT=`$DIALOG --scale\ --title="$DLG_COUNT_TITLE"\ --text="$DLG_COUNT_TEXT"\ --min-value=1\ --max-value=50\ --value=1\ --step=1` 


Zenity return the return code, which is stored in the variable $? .. We will have to check it. Return codes: 0 - the user specified / entered something and clicked OK; 1 - the user clicked "Cancel"; -1 - an error occurred. It is advisable to continue the work of the program only with the return code 0, since the user could erroneously run a script and he should have a choice of cancellation.

 if [[ "$?" == "0" ]]; then #  fi 


Since the scanning and printing process is rather “long” from several seconds to several minutes, it is necessary to somehow show at what stage the process is going. At the same time, the user should be able to stop the copying process at the scanning stage. For bar progress, zenity has a parameter --progress . As a result, the previous code takes the form:

 if [[ "$?" == "0" ]]; then ( #  ) | $DIALOG --progress \ --title="$DLG_PROGRESS_TITLE" \ --text="$DLG_PROGRESS_TEXT" \ --percentage=0 --auto-close fi 


In order for the progress bar to move, it is necessary that in the block with the comment marked “useful code”, the percent values ​​are output via echo, for example echo 10 sets the progress bar to 10%, echo 100 to 100%. To indicate at what stage of the process we are (scanning, printing or completing the program) in the form of text, you also need to use echo, but we must also add a hash sign at the beginning of the message, for example echo "# The scanning process is in progress" will cause the text to appear on the progress bar.

And lastly, we are already adding scanning and printing commands.

Conclusion


The result is a program that has a very simple and intuitive interface and functionality that is not inferior to Epson's native copy program.

 #! /bin/bash SCANNER=`sane-find-scanner -q | sed -e 's/.*at /snapscan:/'` #      DIALOG=zenity TMPFILE="/tmp/scan.tiff" #     LOCKFILE="/tmp/copy.lock" #  DLG_COUNT_TITLE=" " DLG_COUNT_TEXT="   " DLG_PROGRESS_TITLE=" " DLG_PROGRESS_TEXT="..." MSG_START_PROCESS="    $SCANNER" MSG_SCAN="" MSG_PRINT="   " MSG_COMPLETE="   ." MSG_ERROR_LOCK=":    ,        ." MSG_ERROR_SCAN=":   ,          ." MSG_ERROR_PRINT=":   ." #     if ! lockfile-create --retry 2 -l $LOCKFILE; then $DIALOG --error --text "$MSG_ERROR_LOCK" exit fi COPY_COUNT=`$DIALOG --scale\ --title="$DLG_COUNT_TITLE"\ --text="$DLG_COUNT_TEXT"\ --min-value=1\ --max-value=50\ --value=1\ --step=1` if [[ "$?" == "0" ]]; then ( echo "# $MSG_START_PROCESS" echo "10" sleep 2 echo "# $MSG_SCAN" echo "25" #.     man scanimage scanimage --device-name $SCANNER\ --format tiff\ --mode Gray\ -x 210 -y 297 --resolution 300\ --brightness -3 > $TMPFILE if [ $? != 0 ]; then $DIALOG --error --text "$MSG_ERROR_SCAN" lockfile-remove -l $LOCKFILE exit fi echo "# $MSG_PRINT" echo "75" #     if [[ "$COPY_COUNT" == "1" ]]; then tiff2ps -z -w 8.27 -h 11.69 $TMPFILE | lp else tiff2ps -z -w 8.27 -h 11.69 $TMPFILE | lp -n $COPY_COUNT fi if [ $? != 0 ]; then $DIALOG --error --text "$MSG_ERROR_PRINT" lockfile-remove -l $LOCKFILE exit fi rm -f $TMPFILE echo "# $MSG_COMPLETE" echo "100" sleep 2 ) | $DIALOG --progress\ --title="$DLG_PROGRESS_TITLE"\ --text="$DLG_PROGRESS_TEXT"\ --percentage=0\ --auto-close || lockfile-remove -l $LOCKFILE fi lockfile-remove -l $LOCKFILE 


I hope my script will be useful for you.

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


All Articles