The only accurate way to make the script work was to use the free, third-party exiftool and combine that with checking the first byte of the image file, because exiftool lies when it encounters a tiff image with a .CR2 extension.
If you have Xcode (Mac App Store) or preferably the far smaller Apple's Command Line Tools for Xcode 11 (requires free developer account) installed, then the homebrew package manager (also required) will automatically compile and install exiftool in /usr/local/bin. Normally, I do not post code that requires other installation conditions, but this is the sole exception.
To get homebrew package manager installed, you can copy/paste the following into an open Terminal window with a return:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Once that is done, you can do the following, also in the Terminal:
brew update
brew upgrade
brew info exiftool
brew install exiftool
brew cleanup # after you have updated exiftool once, this will purge the prevous version
You will need /usr/local/bin in your shell PATH, so either in your ~/.bash_profile, or ~/.zshrc, you would do the following:
export PATH=".:/usr/local/bin:$PATH"
# gag Google analytics tracking
export HOMEBREW_NO_ANALYTICS=1
and for the current Terminal session to put this into effect:
source ~/.bash_profile or source ~/.zshrc
You would run the script from the Terminal with the only argument as the full or tilde (~) path to the parent directory of your images:
# make the script executable
chmod +x ./imgmv.zsh
./imgmv.zsh ~/Desktop/Test3
There are instructions in the script for also generating a report of images to be renamed. The fact that there are 35 lines of comments in the script likely means that the hosting software will not allow me to post the full 112 lines, so I will try to do that in a follow up post as it also counts the lines in this post as well.