John,
hope I have this correct
in the Run Shell Script, I have this.
!/bin/zsh
# Note: use zsh -vx for debugging
# Zsh home: http://zsh.sourceforge.net/
# Recursively look for image files with wrong extension for mimetype
# and correct those extensions.
# usage: ./imgmv.zsh /path/to/parent/folder
zmodload zsh/regex
# global variables are denoted with -g
typeset -g mimetype
# arrays
typeset -ga mstatus
typeset -ga result=()
typeset -gA assoc=(dng image/x-adobe-dng cr2 image/x-canon-cr2 cr3 image/x-canon-cr3 raf image/x-fujifilm-raf tif image/tiff jpg image/jpeg)
function verify_file_type () {
local img_file=$1
local code=""
# sniff the first byte of the image file, without newline
code=$(od -t x1 -N 1 $img_file | awk '{$1=$1;print $2}')
if [[ $code = ff ]];
then
# get lower-case extension (:l:e) of this img_file
echo "image/jpeg ${img_file:l:e}"
elif [[ $code = 4d ]];
then
echo "image/tiff ${img_file:l:e}"
elif [[ $code =~ '00|46|49' ]]
then
echo "RAW ${img_file:l:e}"
else
echo "$code ${img_file:l:e}"
fi
return
}
function contains () {
# Lookup image extension as a key in associative array
# to see if it matches (true) or not (false) the mimetype
local mtype=$1
local extype=$2
# get the value associated with the extension key and compare to mimetype
[[ ${(v)assoc[$extype]} = $mtype ]] && echo true || echo false
return
}
function valid_image_ext () {
# return true when mimetype and extension are correct
local img_file=$1
local truth=""
# assign function output as regular array elements
# result[1] will be mimetype, and result[2] is the image extension
result=($(verify_file_type $img_file))
# see if extension is either of jpg or tiff
[[ $result[2] =~ 'jpg|tif' ]] && truth=true || truth=false
# does it match image/jpeg or image/tiff
if [[ $result[1] =~ 'i*(jpeg|tiff)' && $truth = true ]];
then
mimetype=$result[1]
echo "true $mimetype"
# extension does not match this mimetype
elif [[ $result[1] =~ 'i*(jpeg|tiff)' && $truth = false ]];
then
mimetype=$result[1]
echo "false $mimetype"
# otherwise it is a camera raw file with some extension
elif [[ $result[1] = RAW ]];
then
# get the Camera Raw mimetype string
mimetype=$(/usr/local/bin/exiftool -s3 -MIMEType $img_file)
# does the current image extension match the mimetype
# send back "true|false mimetype" to array mstatus
echo "$(contains $mimetype $result[2]) $mimetype"
fi
return
}
function rename_ext () {
# use mimetype (value) to lookup proper extension (key) in
# associative array and use that extension for the rename.
local mtype=$1
local img_file=$2
local extkeyname=${(k)assoc[(r)$mtype]}
# force raw extensions to upper-case
# force RAW file extensions to upper-case (:u) and others to lower (:l)
[[ $extkeyname =~ 'dng|cr2|raf' ]] && ext=${extkeyname:u} || ext=${extkeyname:l}
# tack extension onto full path and basename (a:r)
mv ${img_file} ${img_file:a:r}.$ext
return
}
startdir=$1
setopt GLOB_STAR_SHORT
# case-insensitive extension match
setopt no_CASE_GLOB
# if you add additional extensions here, add them in the associative array too
# tiff and jpeg are illegal extension formats, so not included here. The
# zsh (.) means to get just the paths to files, not folders.
for f in ${startdir}/**.(jpg|tif|cr2|dng|raf)(.); do
setopt CASE_GLOB
# mstatus[1] = true|false, mstatus[2]=mimetype
mstatus=($(valid_image_ext $f))
# true means the mimetype and extension match so fetch next file
[[ $mstatus[1] = true ]] && continue
# remove comment in print statement to generate a report prior to rename
# e.g. imgmv.zsh path >& ~/Desktop/rpt.txt
# print $mstatus[2] $f
$(rename_ext $mstatus[2] $f)
done
exit
and in the Run AppleScript I have this
use scripting additions
on run {input, parameters}
display alert "Change File Extension" message "Completed" as informational
return input
end run
hope this helps...
Paul