Skip to content

Instantly share code, notes, and snippets.

@Luzifer
Last active December 17, 2015 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Luzifer/5559874 to your computer and use it in GitHub Desktop.
Save Luzifer/5559874 to your computer and use it in GitHub Desktop.
Imports an image from a twain sane scanner, converts to jpg and saves it to the desktop of the current user, then opens it with Preview.app
#!/bin/bash
targetfile="scan_$( date +"%Y-%m-%d_%H-%M-%S" ).jpg"
# Get image from scanner
scanimage --format=tiff --resolution 300 > /tmp/scan.tiff
# TODO: Search for correct error message (need it to be thrown by scanimage before implementing)
if [ $(grep -q 'cups' /tmp/scan.tiff ) ]; then
# Calculate size of file without error message
newsize=$(( $(wc -c /tmp/scan.tiff | sed "s/^[^0-9]*\([0-9]*\)[^0-9].*$/\1/") - 22 ))
# Remove error message
tail -c $newsize /tmp/scan.tiff > /tmp/scannew.tiff
mv /tmp/scannew.tiff /tmp/scan.tiff
fi
# Use convert to create a jpg
convert -quality 90 /tmp/scan.tiff "$HOME/Desktop/$targetfile"
# Remove tiff
rm /tmp/scan.tiff
# Open jpg in default JPG viewer (mostly preview) to be edited
open "$HOME/Desktop/$targetfile"
@mathiasbynens
Copy link

Instead of creating $homedir manually, why not just use $HOME?

Also, you might want to wrap stuff like $homedir/Desktop/$targetfile in quotes, in case the expanded string contains spaces.

@Luzifer
Copy link
Author

Luzifer commented May 11, 2013

Thanks for your hints. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment