Image Events creating zero-size files

My image resizing script pulls images from optical media and outputs the resized versions into directories on my hard drive. Unfortunately, it will occasionally produce a bad file (zero-size). I can run the same DVD through the process twice, and have a different bad file each time. The bad file is sometimes a thumbnail, and sometimes the larger image.

I’m thinking this must be some kind of bug with Image Events. Maybe it has an error when reading the file from optical media, and just produces the zero-size file. My plan to resolve it is to basically add a new section at the end of my script. This section would:

  • search for zero-size files in my big and thumbs directories
  • for any files it finds, re-process them from the source

Any suggestions on code to do this? Also, am I going down the right path; or is there something I could change in my original code that might fix this problem?

-- clear out and re-create our temporary directory structure
do shell script "rm -R -f /Users/Shared/Image_Import"
do shell script "mkdir /Users/Shared/Image_Import"
do shell script "mkdir /Users/Shared/Image_Import/big"
do shell script "mkdir /Users/Shared/Image_Import/thumbs"

set inputFolder to quoted form of POSIX path of (choose folder with prompt "Select folder which contains the art file(s)" without invisibles)

-- this section allows me to store the cd/dvd/folder path in a FileMaker database for reference
-- just remove this section if not using FileMaker
tell application "FileMaker Pro"
	set contents of cell "g_import_path" of ¬
		current record of layout "Tools_Image Import_detail" to inputFolder
end tell

-- process the images and thumbnails
set theFiles to paragraphs of (do shell script "find " & inputFolder & " | grep .tif")
runConversion(theFiles)

on runConversion(theItems)
	set bigFolder to POSIX path of ("/Users/Shared/Image_Import/big")
	set thumbFolder to POSIX path of ("/Users/Shared/Image_Import/thumbs")
	tell application "Image Events"
		launch
		set maxSize to 1200
		set thumbSize to 300
		if (count items of theItems) is greater than 0 then
			repeat with anItem in theItems
				set imageFile to (open POSIX file (contents of anItem) as alias)
				set {width, height} to dimensions of imageFile
				if width > maxSize or height > maxSize then
					scale imageFile to size maxSize
				end if
				save imageFile as JPEG in bigFolder
				if width > thumbSize or height > thumbSize then
					scale imageFile to size thumbSize
				end if
				save imageFile as JPEG in thumbFolder
			end repeat
		else
			display dialog "Nothing to convert."
		end if
		quit
	end tell
	-- remove profiles from images (they sometimes add 5MB!)
	do shell script "sips --deleteProperty profile " & POSIX path of bigFolder & "/*.jpg"
	-- remove profiles from thumbnails (they sometimes add 5MB!)
	do shell script "sips --deleteProperty profile " & POSIX path of thumbFolder & "/*.jpg"
end runConversion

Model: MacBook Pro
AppleScript: 1.10.7
Browser: Firefox 2.0.0.3
Operating System: Mac OS X (10.4)

Apparently Quicktime 7.2 has some exporting issues, and I imagine Image Events is using Quicktime to write the files. There is an article at MacFixIt that explains it.

EDIT: Downgrading does not solve the problem. I can reproduce the zero byte file problem on:
Quad G5 10.4.10 with Quicktime 7.2
Quad G5 10.4.10 downgraded to 7.1.6 using macfixit instructions
Mac Pro 10.4.8 with Quicktime 7.1.3

EDIT: Message prior to testing follows:
Hmmmm…this is an interesting possibility. I know Image Events uses “sips” for processing. And I see some other references on the net regarding sips using quicktime.

http://lists.apple.com/archives/QuickTime-API/2005/Apr/msg00157.html

Thanks for making this connection, which I never would have made. I’ll try downgrading.

I would still love any suggestions on the best way to search for zero-byte files, as I think it’s important I trap for this possibility in the future (and I am guessing anyone else using “Image Events” should do so as well).

This code snippet is supposed to find all the zero byte .jpg files in my two directories and then re-process them from the original .tif files stored in the inputFolder. Unfortunately, I am very new to AppleScript, and it is badly broken. Any help would be greatly appreciated.

Just to clarify: All the files in bigFolder and thumbFolder are jpg. The original files have the same name, but are tif.

set inputFolder to quoted form of POSIX path of (choose folder with prompt "Select DVD which contains original TIFFs" without invisibles)

set bigFolder to POSIX path of ("/Users/Shared/Image_Import/big")
set thumbFolder to POSIX path of ("/Users/Shared/Image_Import/thumbs")

tell application "Finder"
	set zeroByteBigFileList to (every file of folder (POSIX file bigFolder as Unicode text) whose size = 0)
	set zeroByteThumbFileList to (every file of folder (POSIX file thumbFolder as Unicode text) whose size = 0)
end tell

tell application "Image Events"
	launch
	set maxSize to 1200
	set thumbSize to 300
	if (count items of zeroByteBigFileList) is greater than 0 then
		repeat with anItem in zeroByteBigFileList
			set badFile to anItem
			set imageFile to (do shell script "find " & inputFolder & " | grep " & (POSIX path of (file name of badFile))) --I only want badFile to be the file name without extension
			set {width, height} to dimensions of imageFile
			if width > maxSize or height > maxSize then
				scale imageFile to size maxSize
			end if
			save imageFile as JPEG in bigFolder
			if width > thumbSize or height > thumbSize then
				scale imageFile to size thumbSize
			end if
			save imageFile as JPEG in thumbFolder
		end repeat
	end if
	if (count items of zeroByteThumbFileList) is greater than 0 then
		repeat with anItem in zeroByteThumbFileList
			set badFile to anItem
			set imageFile to (do shell script "find " & inputFolder & " | grep " & (POSIX path of (file name of badFile))) --I only want badFile to be the file name without extension
			set {width, height} to dimensions of imageFile
			if width > maxSize or height > maxSize then
				scale imageFile to size maxSize
			end if
			save imageFile as JPEG in bigFolder
			if width > thumbSize or height > thumbSize then
				scale imageFile to size thumbSize
			end if
			save imageFile as JPEG in thumbFolder
		end repeat
	end if
	quit
end tell

Model: MacBook Pro
AppleScript: 1.10.7
Browser: Firefox 2.0.0.3
Operating System: Mac OS X (10.4)

OK, here is a script that I’ve been working on much of the day. I think it is really close to working, as the individual components test out ok. Unfortunately, I’m getting this error in my event log:

do shell script "find '/Volumes/MM0407/' | grep 816835_MM0407_A.tif"
	"/Volumes/MM0407//26-27/816835_MM0407_A.tif"
runConversion({"/Volumes/MM0407//26-27/816835_MM0407_A.tif"})
	"Finder got an error: Can't continue runConversion."

Here is the new script:

set inputFolder to quoted form of POSIX path of (choose folder with prompt "Select DVD which contains original TIFFs" without invisibles)

set bigFolder to POSIX path of ("/Users/Shared/Image_Import/big")
set thumbFolder to POSIX path of ("/Users/Shared/Image_Import/thumbs")

tell application "Finder"
	set zeroByteThumbFileList to (every file of folder (POSIX file thumbFolder as Unicode text) whose size = 0)
	if (count items of zeroByteThumbFileList) is greater than 0 then
		repeat with anItem in zeroByteBigFileList
			set badFile to name of anItem
			set cleanedFile to ((text 1 thru -5 of badFile) & ".tif")
			set theFiles to paragraphs of (do shell script "find " & inputFolder & " | grep " & cleanedFile)
			runConversion(theFiles)
		end repeat
	end if
	set zeroByteBigFileList to (every file of folder (POSIX file bigFolder as Unicode text) whose size = 0)
	if (count items of zeroByteBigFileList) is greater than 0 then
		repeat with anItem in zeroByteBigFileList
			set badFile to name of anItem
			set cleanedFile to ((text 1 thru -5 of badFile) & ".tif")
			set theFiles to paragraphs of (do shell script "find " & inputFolder & " | grep " & cleanedFile)
			runConversion(theFiles)
		end repeat
	end if
end tell

on runConversion(theItems)
	set bigFolder to POSIX path of ("/Users/Shared/Image_Import/big")
	set thumbFolder to POSIX path of ("/Users/Shared/Image_Import/thumbs")
	tell application "Image Events"
		launch
		set maxSize to 1200
		set thumbSize to 300
		if (count items of theItems) is greater than 0 then
			repeat with anItem in theItems
				set imageFile to (open POSIX file (contents of anItem) as alias)
				set {width, height} to dimensions of imageFile
				if width > maxSize or height > maxSize then
					scale imageFile to size maxSize
				end if
				save imageFile as JPEG in bigFolder
				if width > thumbSize or height > thumbSize then
					scale imageFile to size thumbSize
				end if
				save imageFile as JPEG in thumbFolder
			end repeat
		else
			display dialog "Nothing to convert."
		end if
		quit
	end tell
	-- remove profiles from images (they sometimes add 5MB!)
	do shell script "sips --deleteProperty profile " & POSIX path of bigFolder & "/*.jpg"
	-- remove profiles from thumbnails (they sometimes add 5MB!)
	do shell script "sips --deleteProperty profile " & POSIX path of thumbFolder & "/*.jpg"
end runConversion

EDIT: Argh! After several tests, I ended up with some zero-byte files again. I’ll update when I determine the problem.

Here is my final working script. This script is used to import FPO images into a FileMaker database. I am creating a big image (1200 by x) and thumbnail (300 by x). Sometimes Image Edit creates zero-bye images, so I find these and re-process them. I also remove all color profiles at the end, as these were causing image bloat.

-- clear out and re-create our temporary directory structure
do shell script "rm -R -f /Users/Shared/Image_Import"
do shell script "mkdir /Users/Shared/Image_Import"
do shell script "mkdir /Users/Shared/Image_Import/big"
do shell script "mkdir /Users/Shared/Image_Import/thumbs"

--sets up the directories for processing
set bigFolder to POSIX path of ("/Users/Shared/Image_Import/big")
set thumbFolder to POSIX path of ("/Users/Shared/Image_Import/thumbs")

-- request the source media
set inputFolder to quoted form of POSIX path of (choose folder with prompt "Select folder which contains the art file(s)" without invisibles)

-- this section allows me to store the cd/dvd/folder path in a FileMaker database for reference
-- just remove this section if not using FileMaker
tell application "FileMaker Pro Advanced"
	set contents of cell "g_import_path" of ¬
		current record of layout "Tools_Image Import_detail" to inputFolder
end tell

-- process the images and thumbnails
set theFiles to paragraphs of (do shell script "find " & inputFolder & " | grep .tif")
runConversion(theFiles)

-- sometimes Image Edit is producing zero byte images
-- this code looks for thes images, and re-processes them
tell application "Finder"
	set bigFolder to POSIX path of ("/Users/Shared/Image_Import/big")
	set thumbFolder to POSIX path of ("/Users/Shared/Image_Import/thumbs")
	set filesToProcess to ""
	set zeroByteThumbFileList to (every file of folder (POSIX file thumbFolder as Unicode text) whose size = 0)
	if (count items of zeroByteThumbFileList) is greater than 0 then
		repeat with anItem in zeroByteBigFileList
			set badFile to name of anItem
			set originalFile to ((text 1 thru -5 of badFile) & ".tif")
			if filesToProcess = "" then
				set filesToProcess to originalFile
			else
				set filesToProcess to filesToProcess & "|" & originalFile
			end if
		end repeat
	end if
	set zeroByteBigFileList to (every file of folder (POSIX file bigFolder as Unicode text) whose size = 0)
	if (count items of zeroByteBigFileList) is greater than 0 then
		repeat with anItem in zeroByteBigFileList
			set badFile to name of anItem
			set originalFile to ((text 1 thru -5 of badFile) & ".tif")
			if filesToProcess = "" then
				set filesToProcess to originalFile
			else
				set filesToProcess to filesToProcess & "|" & originalFile
			end if
		end repeat
	end if
end tell

-- if zero byte images were found, re-process them
if filesToProcess is not equal to "" then
	set theFiles to paragraphs of (do shell script "find " & inputFolder & " | grep -E " & quoted form of filesToProcess)
	runConversion(theFiles)
end if

on runConversion(theItems)
	set bigFolder to POSIX path of ("/Users/Shared/Image_Import/big")
	set thumbFolder to POSIX path of ("/Users/Shared/Image_Import/thumbs")
	tell application "Image Events"
		launch
		set maxSize to 1200
		set thumbSize to 300
		if (count items of theItems) is greater than 0 then
			repeat with anItem in theItems
				set imageFile to (open POSIX file (contents of anItem) as alias)
				set {width, height} to dimensions of imageFile
				if width > maxSize or height > maxSize then
					scale imageFile to size maxSize
				end if
				save imageFile as JPEG in bigFolder
				if width > thumbSize or height > thumbSize then
					scale imageFile to size thumbSize
				end if
				save imageFile as JPEG in thumbFolder
			end repeat
		else
			display dialog "Nothing to convert."
		end if
		quit
	end tell
end runConversion

-- set root directories for profile removal
set bigFolder to POSIX path of ("/Users/Shared/Image_Import/big")
set thumbFolder to POSIX path of ("/Users/Shared/Image_Import/thumbs")
-- remove profiles from images (they sometimes add 5MB!)
do shell script "sips --deleteProperty profile " & POSIX path of bigFolder & "/*.jpg"
-- remove profiles from thumbnails (they sometimes add 5MB!)
do shell script "sips --deleteProperty profile " & POSIX path of thumbFolder & "/*.jpg"