Add selecting and printing file dialogs
Karsten Günther reveals how to save file names and paths, select them with YAD and print with the printer dialog.
YAD
Credit: https://github.com/v1cont/yad
OUR EXPERT
Karsten Günther loves pretty things, so he loves making terminal tools a little prettier for use with YAD.
QUICK TIP
If parts of a command line are to be executed first in order to be able to use their output in the command line, then the construction $(first) is used. BASH replaces this construction with the standard output of the embedded command line.
Usually files processed by shell scripts are selected either by the user or with name patterns. The calling shell interprets the name patterns and takes over the file paths. YAD offers the ability to use a GUI way of specifying files in a dialog. However, when it comes to multiple file names as well as file names containing spaces, an extra level of complexity has to be dealt with. When selecting files manually, the shell usually masks such filenames automatically when completing the selection, but the main problem remains: saving paths with spaces so that they can be used later.
No matter how a group of files is obtained, their names – or entire paths – must be stored for processing. As a structure for such data, BASH offers simply indexed arrays (there are also others). BASH creates an array whenever a variable assignment is created in the form ARRAY=( E lemente) . The individual elements of the array are accessed in the form ${ARRAY[INDEX]} . The INDEX starts at zero and automatically ends with the number of the last element contained in the array. ${ARRAY[*]} and ${ARRAY[@]} output all elements of the array (the man page explains the difference). Using the construction ${!ARRAY[*]} the BASH generates a list of all indices available for the array. In the example from above, the result is therefore
Several filters can be defined simultaneously and are then available alternatively. The selection of a filter influences the entries displayed in the dialog. The definition of “All” (--file-filter=’All | *’) shows all files.
Whether the display must always be as indicated in the echo line is a matter of taste. The whole procedure can be combined into the extremely useful function dump_array . The name of an array variable is passed as an argument. Error handling is omitted to keep the function simple; the internal variable __ARRAY is named with two underscores so that ARRAY can also be used as an argument of the function: