Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'A script to batch convert images between formats' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A script to batch convert images between formats
Authored by: robspychala on Oct 09, '04 02:55:20PM
The Apple script calls got truncated from the posted file. Here is a repost.

#!/bin/bash
# A simple bash script that uses Applescript to convert between 
# image formats

if [ ${#} -ne 3 ]
then
    echo "Image conversion script using Apple Image Events."
    echo ""
    echo "    Usage: ${0} [DIR] [file ext] [jpeg2|tiff|jpeg|pict|bmp|psd|png]"
    echo ""
    exit;
fi
if [ "${1}" != "-convert" ]
then
    echo ""
    echo "starting in: $1"
    echo "getting all the $2 files to convert to $3"
    find $1 -name "*.$2" -exec ./$0 -convert {} $3 \;
    exit 1
fi

pwd=`pwd`
jpg_file="$2"
pict_file="${2%%.*}.${3}"

# deletes the previously converted file
if [ -e "${pict_file}" ]
then
    rm $pict_file
    echo "deleted old ${3}"
fi

echo "converting ${jpg_file} to ${pict_file}"
osascript<<EOF
tell application "Image Events"
	activate
	-- open the image file
	set this_image to open file "$jpg_file" as alias
	-- save in new file
	save this_image as ${3} in file "$pict_file"
	-- purge the open image data
	close this_image
end tell
EOF


[ Reply to This | # ]
A script to batch convert images between formats
Authored by: Chiwo on Oct 09, '04 07:34:18PM

Looks to me like this script will only work if it's in the current directory, and won't work on any files with spaces or other unusual characters in their names, or in directories with spaces in their names. May I suggest a structure more like this sketch:


find "$1" -type f -name "*.$2" -print | while read file
do
  dest="`dirname "$file"`/`basename "$file" ".$2"`"
  rm -f "$dest"
  osascript etc. etc.
done
Still not bombproof but more likely to work.



[ Reply to This | # ]