Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
justinyaodu committed Aug 8, 2021
0 parents commit 5ab6223
Show file tree
Hide file tree
Showing 4 changed files with 315 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# MIT License

Copyright (c) 2021 Justin Yao Du

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# iscanimage

`iscanimage` is an user-friendly command line program for operating scanner devices in Linux. It uses `scanimage` to do the actual scanning, as the name implies, but it adds many quality-of-life improvements for interactive use.

## Features

* Select a scanner device interactively
* Scan multiple files in one session
* Save all scanned files to a specified directory
* Name each file as you go (defaults to the current date and time)
* Evaluate custom commands before and after scanning each file

## Installation

Download the [iscanimage](iscanimage) script, make it executable, and drop it somewhere on your `$PATH`.

## Usage

Here's an example user session:

![iscanimage demo in the terminal](demo.png)

Try `iscanimage --help` for more details.

## To Do

* Create a proper manpage

## License

`iscanimage` is licensed under the [MIT License](LICENSE.md).
Binary file added demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
263 changes: 263 additions & 0 deletions iscanimage
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
#!/bin/bash

name='iscanimage'
version='1.0.0'

version_msg="${name} ${version}
Copyright (C) 2021 Justin Yao Du.
Licensed under the MIT License.
https://github.com/justinyaodu/${name}"

selected_device=''
output_dir=''
output_format='png'
run_before=''
run_after=''
colorize=''
verbose=0
scanimage_options=()

help_msg="Usage: ${name} [OPTIONS...] [-- SCANIMAGE_OPTIONS...]
User-friendly interactive wrapper script for scanimage.
Options:
-L, --list-devices list all devices and exit
-O, --output-dir DIR put scanned files in this directory (if unspecified,
use a temporary directory)
-f, --format FORMAT use FORMAT as the output file format and file
extension (default: ${output_format})
-d, --device-name DEVICE scan from DEVICE (if unspecified, ask the user)
-B, --before CMD evaluate CMD before scanning each file
-A, --after CMD evaluate CMD after successfully scanning each file
--color always colorize output
--no-color never colorize output
-v, --verbose show more verbose output
-h, --help show this help message
--version show version information
The evaluated commands for --before and --after can use \$filename to refer to
the current filename. For example:
scanimage --after 'cp \"\$filename\" ~'
Ensure that the parameter expansion is properly quoted or escaped, to avoid
premature expansion by your shell."

devices=()

load_devices() {
local line
while read -r line; do
echo "${#devices[@]}"

devices+=("${line}")
printf "\tName:\t%s\n" "${line}"

read -r line
printf "\tVendor:\t%s\n" "${line}"

read -r line
printf "\tModel:\t%s\n" "${line}"

read -r line
printf "\tType:\t%s\n" "${line}"
done < <(scanimage --formatted-device-list $'%d\n%v\n%m\n%t\n')
}

select_device() {
echoy "Detecting scanners..."
load_devices

echoy "Select a scanner device by index (0-$(( ${#devices[@]} - 1 ))) or name."
while :; do
if ! read -r -p "Device: " selected_device; then
echor "No device selected."
exit 1
fi

if [[ "${selected_device}" =~ ^[0-9]+$ ]]; then
if (( selected_device < ${#devices[@]} )); then
selected_device="${devices[selected_device]}"
return 0
else
echor "Index out of range."
fi
else
local device
for device in "${devices[@]}"; do
[ "${selected_device}" = "${device}" ] && return 0
done
echor "Invalid device name."
fi
done
}

scan_loop() {
[ "${output_dir}" ] || output_dir="$(mktemp -d)"
echo "Using output directory: '${output_dir}'"

echoy "Enter each output filename without the extension (e.g. my_image),"
echoy "or leave the filename blank to use the current date and time."

local num_scanned=0
while :; do
local filename_stem
if ! read -r -p "Enter filename (Ctrl+D to exit): " filename_stem; then
echo
echo "Scanning canceled."
break
fi

[ "${filename_stem}" ] || filename_stem="$(date '+%Y-%m-%d-%H-%M-%S')"
filename="${output_dir}/${filename_stem}.${output_format}"

if [ "${run_before}" ]; then
(( verbose )) && echoy "Run before: '${run_before}'"
eval "${run_before}"
fi

local scan_cmd=(scanimage \
--device-name "${selected_device}" \
--format "${output_format}" \
--output-file "${filename}" \
"${scanimage_options[@]}")
(( verbose )) && echoy "Scan command:$(printf ' %q' "${scan_cmd[@]}")"

echoy "Scanning..."
if "${scan_cmd[@]}"; then
(( num_scanned++ ))
echog "Scanned file: '${filename}'"

if [ "${run_after}" ]; then
(( verbose )) && echoy "Run after: '${run_after}'"
eval "${run_after}"
fi
else
echor "Failed to scan file: '${filename}'"
fi
done

if (( num_scanned > 0 )); then
echog "Scanned ${num_scanned} file(s) to output directory: '${output_dir}'"
fi
}

color_init() {
if [ ! "${colorize}" ]; then
[ -t 1 ] && colorize=1 || colorize=0
fi

if (( colorize )); then
c0="$(tput sgr0)"
cr="$(tput setaf 1)"
cg="$(tput setaf 2)"
cy="$(tput setaf 3)"
else
c0=''
cr=''
cg=''
cy=''
fi
}

echor() {
echo "${cr}${1}${c0}"
}

echog() {
echo "${cg}${1}${c0}"
}

echoy() {
echo "${cy}${1}${c0}"
}

parse_options() {
while (( ${#} )); do
case "${1}" in
-O|--output-dir|\
-f|--format|\
-d|--device|--device-name|\
-B|--before|\
-A|--after)
if (( ${#} < 2 )); then
die_usage "Option '${1}' requires an argument."
fi
parse_option_arg "${1}" "${2}"
shift
;;
-l|-L|--list-devices)
load_devices
exit 0
;;
--color)
colorize=1
;;
--no-color)
colorize=0
;;
-v|--verbose)
verbose=1
;;
--)
shift
scanimage_options=("${@}")
return
;;
-h|--help)
echo "${help_msg}"
exit 0
;;
--version)
echo "${version_msg}"
exit 0
;;
-*)
die_usage "Unrecognized option '${1}'."
;;
*)
die_usage "Unexpected argument '${1}'."
;;
esac
shift
done
}

parse_option_arg() {
case "${1}" in
-O|--output-dir)
if [ ! -d "${2}" ]; then
echo "Output directory does not exist: '${2}'"
exit 1
fi
output_dir="${2}"
;;
-f|--format)
output_format="${2}"
;;
-d|--device|--device-name)
selected_device="${2}"
;;
-B|--before)
run_before="${2}"
;;
-A|--after)
run_after="${2}"
;;
esac
}

die_usage() {
echo "${1}"
echo "Try '${name} --help' for more information."
exit 2
}

main() {
parse_options "${@}"
color_init
[ "${selected_device}" ] || select_device
scan_loop
}

main "${@}"

0 comments on commit 5ab6223

Please sign in to comment.