Need help with Image Events/Stuffit Expander/Mail.app

I want a script to automatically download new files from an ftp server, unzip any archives, and email small jpegs of the downloaded files. I’m not posting the ftp handling component here because that part works fine.

I had this all working when dealing with plain files, but it broke when I tried to make it handle subfolders and zipped archives.

Any suggestions are welcome! Thanks in advance. Luke


set theFileList to {}
set thePreviews to {}
set the image_list to {}



tell application "Finder"
	
	set serverVolume to (folder "Home Directory:Desktop:Shared:Uploads" of desktop)
	
	repeat with test_a_folder in {"Previews", "In", "Out", "Skipped"}
		if not (exists folder test_a_folder of serverVolume) then
			set name of (make new folder of serverVolume) to test_a_folder
		end if
	end repeat
	set previewFolder to (folder "Previews" of serverVolume)
	set the in_folder to (folder "In" of serverVolume)
	set the Out_Folder to (folder "Out" of serverVolume)
	set the Skipped_Folder to (folder "Skipped" of serverVolume)
end tell

--get list of downloaded files and their types
tell application "Finder"
	set the_zipped_files to (every file of (entire contents of the in_folder) whose file type = "ZIP ")
end tell
if the_zipped_files ≠ {} then
	tell application "StuffIt Expander"
		expand every file of (the_zipped_files)
	end tell
	tell application "Finder" to move the_zipped_files to the Out_Folder
end if

tell application "Finder"
	set the in_files to (every item of (entire contents of in_folder) whose kind ≠ "folder")
	
	
	repeat with previewMe in the in_files
		set the generic_name to my strip_name(previewMe) as string
		set theImagePath to (POSIX path of (previewFolder as alias) & genericName & "jpg")

		--convert to jpeg max size 300 x 400
		
		
		tell application "Image Events"
			try
				set this_image to (open file (POSIX path of previewMe))
				set target_width to 400
				set target_height to 300
				copy dimensions of this_image to {current_width, current_height}
				if current_width > current_height then
					if current_width > target_width then
						scale this_image to size target_width
					end if
				else if current_width ≤ current_height then
					if current_height > target_height then
						scale this_image to size target_height
					end if
				end if
				save this_image in (POSIX path of the Image_Path) as JPEG with icon
				tell application "System Events" to set thePreviews to thePreviews & the Image_Path
				tell application "Finder"
					set theRawSize to size of (info for previewMe)
					set mySize to (convertByteSize of me on theRawSize)
				end tell
				
				set theFileList to theFileList & return & (this_file) & tab & tab & (mySize)
			on error
				tell application "Finder" to move previewMe to the Skipped_Folder with replacing
			end try
		end tell
		
		
	end repeat
	
	
	tell application "Mail"
		set myMessage to (make new outgoing message with properties {subject:"Files Received", content:"Files received " & (current date) & "." & return & return & "Name" & tab & tab & "Size" & return & theFileList & return & return & "NOTE: The attached images are for preview only!" & return})
		tell myMessage
			make new to recipient at end of to recipients with properties {address:"somebody@somewhere.com"}	
repeat with attachMe from 1 to (count thePreviews)
				tell content
					make new attachment with properties {file name:(item attachMe of thePreviews)} at after the last paragraph
				end tell
			end repeat
			send
		end tell
	end tell
	

end tell
to convertByteSize on theRawSize
-- convertByteSize -- by Nigel Garvey & Richard Morton, 2002 --
-- Convert bytes to a readable string in bytes-K-MB-GB as required.
-- Pass the number in bytes. Returns string.
	set oneK to 2 ^ 10
	set oneMB to 2 ^ 20
	set oneGB to 2 ^ 30
	tell theRawSize to ¬
		if it ≥ oneGB then
			return ((it div oneGB) & "." & text 2 thru 3 of ((100 + ((it mod oneGB) div (oneK * 1.0E+4))) as string) & " GB") as string
		else if it ≥ oneMB then
			return ((it div oneMB) & "." & text 2 thru 3 of ((100 + ((it mod oneMB) div (oneK * 10))) as string) & " MB") as string
		else if it ≥ oneK then
			return (it div oneK & " KB") as string
		else
			return (it & " bytes") as string
		end if
end convertByteSize

on strip_name(previewMe)
--thanks to StefanK for this sub
	tell application "Finder"
		set {name:Nm, name extension:Ex} to info for (previewMe as alias)
		if Ex is missing value then return Nm
		return text 1 thru ((count Nm) - (count Ex) - 1) of Nm
	end tell
end strip_name


Hi,

some suggestions:

There are some slips of the pen, check these variable names
generic_name <–> genericName
theImagePath <–> the ImagePath

for this reason I never use the sugar cosmetic “article” the

I’ prefer to check for name extension “zip”, not for file type “ZIP”
Here is a optimized version of the first part of your script


.
set serverVolume to ((path to desktop as Unicode text) & "Shared:Uploads:") as alias

set previewFolder to make_new_folder(serverVolume, "Previews")
set in_folder to make_new_folder(serverVolume, "In")
set Out_Folder to make_new_folder(serverVolume, "Out")
set Skipped_Folder to make_new_folder(serverVolume, "Skipped")

--get list of downloaded files and their types
tell application "Finder"
	-- if you use Tiger, uncomment the next 4 lines which are commented out (the "bug" has been fixed in Leopard)
	-- 	try
	set the_zipped_files to (every file of (entire contents of in_folder) whose name extension is "zip") as alias list
	-- 	on error
	--		set the_zipped_files to (every file of (entire contents of in_folder) whose name extension is "zip") as alias as list
	--	end try
end tell
if the_zipped_files ≠ {} then
	tell application "StuffIt Expander" to expand the_zipped_files
	tell application "Finder" to move the_zipped_files to Out_Folder
end if

.


.

on make_new_folder(theFolder, fName)
	try
		return ((theFolder as Unicode text) & fName) as alias
	on error
		tell application "Finder" to return (make new folder at theFolder with properties {name:fName}) as alias
	end try
end make_new_folder