Resizing image files into one folder

Hello everyone,

I’m trying to create a Folder Action script so that I can designate one folder to be the recipient of any images I wish to resize. By creating a Folder Action script I can designate any folder I want as I “resize folder” and have multiple “resize folders” for various image items (including different sized items). I’ve worked with Applescript before, but I’m hardly one to make it do crazy hula dance tricks.

I’ve been able to locate two separate scripts (although they do different things) and I want to combine the two scripts to give me what I want.

  1. The first script resizes image files nicely and
  2. The second script is a script which actually creates a folder and files away the end-product into the folder.

The first script does what I want, but leaves the image in the original folder. The second script is the “zip script” from Apple’s website, but it has a nice feature that creates a “Done” folder inside of the designated folder where it files away the completed zip file. I want to use this feature of the script to make the resizing script file away the resized images into a “Done” folder.

I’ve made attempts to combine the two scripts and I’m facing frustration.

Here are the separate scripts:

Resize script:

-- save in Script Editor as Application
-- drag files to its icon in Finder

on open some_items
	repeat with this_item in some_items
		try
			rescale_and_save(this_item)
		end try
	end repeat
end open

to rescale_and_save(this_item)
	tell application "Image Events"
		launch
		set the target_width to 144
		-- open the image file
		set this_image to open this_item
		
		set typ to this_image's file type
		
		copy dimensions of this_image to {current_width, current_height}
		if current_width is greater than current_height then
			scale this_image to size target_width
		else
			-- figure out new height
			-- y2 = (y1 * x2) / x1
			set the new_height to (current_height * target_width) / current_width
			scale this_image to size new_height
		end if
		
		tell application "Finder" to set new_item to ¬
			(container of this_item as string) & "scaled." & (name of this_item)
		save this_image in new_item as typ
	end tell
end rescale_and_save

ZIP script:

on adding folder items to this_folder after receiving these_items
	tell application "Finder"
		if not (exists folder "Done" of this_folder) then
			make new folder at this_folder with properties {name:"Done"}
		end if
		set the destination_folder to folder "Done" of this_folder as alias
		set the destination_directory to POSIX path of the destination_folder
	end tell

	repeat with i from 1 to number of items in these_items
		set this_item to item i of these_items
		set the item_info to info for this_item
		if this_item is not the destination_folder and the name extension of the item_info is not in {"zip", "sit"} then
			set the item_path to the quoted form of the POSIX path of this_item
			set the destination_path to the quoted form of (destination_directory & (name of the item_info) & ".zip")
			do shell script ("/usr/bin/ditto -c -k -rsrc --keepParent " & item_path & " " & destination_path)
		end if
	end repeat
end adding folder items to

Now here’s my attempt at combining them:

-- save in Script Editor as Application
-- drag files to its icon in Finder

on adding folder items to this_folder after receiving these_items
	tell application "Finder"
		if not (exists folder "Done" of this_folder) then
			make new folder at this_folder with properties {name:"Done"}
		end if
		set the destination_folder to folder "Done" of this_folder as alias
		set the destination_directory to POSIX path of the destination_folder
	end tell

	repeat with i from 1 to number of items in these_items
		set this_item to item i of these_items
		set the item_info to info for this_item
		set the item_path to the quoted form of the POSIX path of this_item
		set the destination_path to the quoted form of (destination_directory & (name of the item_info))
		try
			tell application "Image Events"
				launch
				set the target_width to 144
				-- open the image file
				set this_image to open this_item
				
				set typ to this_image's file type
				
				copy dimensions of this_image to {current_width, current_height}
				if current_width is greater than current_height then
					scale this_image to size target_width
				else
					-- figure out new height
					-- y2 = (y1 * x2) / x1
					set the new_height to (current_height * target_width) / current_width
					scale this_image to size new_height
				end if
				
				tell application "Finder" to set new_item to ¬
					(container of this_item as string) & "scaled." & (name of this_item)
				save this_image in new_item as typ
				
			end tell
		end try
	end repeat
end adding folder items to

My script is incomplete because I get stuck on the part on where to designate that the “this_image” item be saved to the “destination_folder”.

If someone can point out my error (or point me in the direction of a better script!) I will greatly appreciate it.

Thanks a bunch.

I figured it out.

Duh! All I had to do was alter one-half of ONE silly line. Oy!

Here’s the complete code:

-- save in Script Editor as Application
-- drag files to its icon in Finder

on adding folder items to this_folder after receiving these_items
	tell application "Finder"
		if not (exists folder "Done" of this_folder) then
			make new folder at this_folder with properties {name:"Done"}
		end if
		set the destination_folder to folder "Done" of this_folder as alias
		set the destination_directory to POSIX path of the destination_folder
	end tell
	
	repeat with i from 1 to number of items in these_items
		set this_item to item i of these_items
		try
			tell application "Image Events"
				launch
				set the target_width to 144
				-- open the image file
				set this_image to open this_item
				
				set typ to this_image's file type
				
				copy dimensions of this_image to {current_width, current_height}
				if current_width is greater than current_height then
					scale this_image to size target_width
				else
					-- figure out new height
					-- y2 = (y1 * x2) / x1
					set the new_height to (current_height * target_width) / current_width
					scale this_image to size new_height
				end if
				
				tell application "Finder" to set new_item to ¬
					(destination_directory as string) & "internet_" & (name of this_item)
				save this_image in new_item as typ
				
			end tell
		end try
	end repeat
end adding folder items to

So if anyone wants to designate a folder as the receiver of items (rather than using a droplet as I took from the original image_resize code), that’s one answer to the problem.

What I did to solve my aformentioned problem was alter this line of code located near the bottom of the script:

[i]tell application "Finder" to set new_item to ¬
			(destination_directory as string) & "internet_" & (name of[/i] this_item)

Originally, the line read as such:

[i]tell application "Finder" to set new_item to ¬
			(container of this_item as string) & "internet_" & (name of this_item)[/i]

If you look carefully, I altered the destination of the item ( xxx ) from “container of this_item as string” to “destination_directory as string”. I also used the prefix “internet_”, with an underscore character, rather than “internet.”, with a period, because, for some reason, the applescript doesn’t properly execute with the term “internet”. I believe “internet” is a reserved prefix word of some sort.

If you really wanted to, you could then alter the destination folder to be any folder you wanted by changing this line (located near the top):

[i]set the destination_folder to folder "Done" of this_folder as alias[/i]

To this:

[i]set the destination_folder to folder "xxx" of folder "xxx" of disk "xxx" as alias[/i]

and then axe the “if statement” line that creates the “Done” folder.