I have an old Hewlett Packard DeskJet F380 all in one printer scanner. It works with Image Capture in Mac OS X. I created a one line AppleScript to activate the scanner because it doesn't have a working "scan" button on the scanner, and sometimes I have to set up the scanner away from the computer. I made an alias in shell to execute the scan so I could use Termius (a terminal app for iPhone) on an iPhone to log in to my mac via ssh and type "scan" to start scanning.

In .bash_profile, I added:

alias scan='osascript -e '"'"'tell application "System Events" to tell process "Image Capture" to click button "Scan" of group 2 of splitter group 1 of window "Image Capture"'"'"''

It took a lot of single and double quotes for the alias to work.

That was helpful. But then I wanted to be able to add an argument to rename the scanned file so it could be entered in the shell on the phone. So I commented out the alias and created this shell script and put it in /usr/local/bin

Entering "scan" with no argument will just scan and leave the file named (e.g. Scan 13.tif) but if "scan filename" is entered, it renames the file. So this command will do the scan and then rename the file (the scanner is set to generate TIFFs, so change extension and other values to jpg if appropriate).

   scan document-20200524-page-1
results in this file:
   scan document-20200524-page-1.tiff
or if that file already exists:
   scan document-20200524-page-1 1.tiff


#!/bin/sh
args=$@
interval=2
timeout=120
scanpath=~/Pictures
extension=".tiff"
scanning=true
i=0
btn=`osascript -e 'tell application "System Events" to tell process "Image Capture" to return name of button 3 of group 2 of splitter group 1 of window "Image Capture"'`
if [ "$btn" == "Scan" ]; then
	btn=`osascript -e 'tell application "System Events" to tell process "Image Capture" to click button "Scan" of group 2 of splitter group 1 of window "Image Capture"'`
else
	echo "Not ready to Scan, 'Scan' button shows '$btn'. Check scanner and try again."
	exit 1
fi

while $scanning; do
	sleep $interval
	i=`expr $i + $interval`
	btn=`osascript -e 'tell application "System Events" to tell process "Image Capture" to return name of button 3 of group 2 of splitter group 1 of window "Image Capture"'`
	if [ "$btn" == "Cancel" ]; then
		printf "Scanning... $i\r"
	else
		echo "Scanned in approximately $i seconds."
		scanning=false
	fi
	if [ $i -gt $timeout ]; then
		echo "Took over $timeout seconds. Is something wrong?"
		scanning=false
	fi
done
if [ "$args" != "" ] ; then
	filename=`osascript -e 'tell application "System Events" to tell process "Image Capture" to return name of static text 1 of UI element 1 of last row of table 1 of scroll area 1 of window "Scan Results"'`
	newfilename="$args$extension"
	filenamecheck=true
	filenamechanged=false
	fn=0
	cd $scanpath
	while $filenamecheck; do
		fn=`expr $fn + 1`
		if [ -e "$newfilename" ]; then
			newfilename="$args $fn$extension"
			filenamechanged=true
		else
			filenamecheck=false
		fi
	done
	mv "$filename" "$newfilename"
	if $filenamechanged; then
		echo "'$args$extension' already existed, renamed '$filename' to '$newfilename'"
	else
		echo "renamed '$filename' to '$newfilename'"
	fi
fi

Image Capture screen HP DeskJet scanner

-- Now I have a newer Canon Printer/Scanner so I modified the script a bit

-- linux_macosx_hints_etc page