-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan
More file actions
executable file
·143 lines (127 loc) · 3.29 KB
/
scan
File metadata and controls
executable file
·143 lines (127 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env bash
set -e
set -o pipefail
# Usage: see below
#
# This is an interactive/noninteractive script for wrapping SANE and
# ImageMagick to interface with my ScanSnap scanner. You can pass a
# variety of different tokens defined in the argument parser below to
# choose a set of options (monochrome/color, letter-size/long-page,
# simplex/duplex), or alternatively you can specify interactive mode
# to let you enter any of those tokens, scan a document with them, and
# let you enter new options or press return to repeat the same. Don't
# forget about the option to open each document after scanning, like I
# forgot about it for several years after implementing.
die() {
echo "$@" 1>&2
exit 1
}
color=""
duplex=""
open=""
interactive=""
long=""
for arg; do
case "$arg" in
-) ;;
gray | g | m | monochrome | bw | b | w | -b | -g | -m) color= ;;
color | c | rgb | -c) color=yes ;;
1 | s | simplex | 1sided | ss | -s | -1) duplex= ;;
duplex | double | 2 | 2sided | ds | -d | -2) duplex=yes ;;
1page | page | p) long= ;;
l | long) long=yes ;;
1b)
;;
1c)
color=yes
;;
2b)
duplex=yes
;;
2c)
color=yes
duplex=yes
;;
1bl | 1l)
long=yes
;;
1cl)
color=yes
long=yes
;;
2bl | 2l)
duplex=yes
long=yes
;;
2cl)
color=yes
duplex=yes
long=yes
;;
open | o | -o) open=yes ;;
interactive | i | int | -i | -int) interactive=yes ;;
*) die "unknown argument: $arg" ;;
esac
done
if [[ -n "$interactive" ]]; then
last_cmd=
while read -r cmd; do
if [[ "$cmd" == q* ]]; then
exit 0
fi
if [[ -n "$cmd" ]]; then
last_cmd="$cmd"
fi
scan $last_cmd || true
done
exit 0
fi
scandir="$HOME/files/scans"
timestamp="$(date +%Y%m%d%H%M%S)"
target="${scandir}/${timestamp}.pdf"
if [[ ! -d "$scandir" ]]; then
die "not a directory: $scandir"
fi
tmpdir="$(mktemp -d)"
trap 'rm -rf $tmpdir' EXIT
cd "$tmpdir"
args=(
--device="fujitsu:ScanSnap iX500:1377846"
# Note that if we don't override the batch filename format,
# documents of 10 or more pages will get out of order due to
# lexicographic sorting in the shell.
--batch="out%05d.pnm" --format=pnm --resolution=300
)
if [[ -n "$color" ]]; then
args=("${args[@]}" --mode=Color)
fi
if [[ -n "$duplex" ]]; then
args=("${args[@]}" --source="ADF Duplex")
fi
if [[ -n "$long" ]]; then
# What a mess. It's really unclear why these parameters are all
# needed but they are. Output to PNG or JPEG also fails, but only
# in ALD mode. For ALD you need to do PNM. See
# <https://alioth-lists.debian.net/pipermail/sane-devel/2015-January/032999.html>.
args=(
"${args[@]}"
--ald=yes --buffermode=On
--page-height=1000mm -y 1000mm
)
fi
scanimage "${args[@]}"
magick ./*.pnm scan.pdf
# If stdout is redirected to a file, write to it. Otherwise just save
# a file to the standard directory.
if [[ -t 1 ]]; then
mv scan.pdf "$target"
if [[ -n "$open" ]]; then
if command -v open &>/dev/null; then
open "$target"
else
xdg-open "$target" &>/dev/null
fi
fi
else
cat scan.pdf
fi