I previously wrote on a related topic Resizing Images Into One Folder regarding the use of creating a Folder Action. Now, however, I’ve chose the “droplet” route for image resizing rather than the folder action route for image resizing.
My dilemma is that I can’t get the droplet to place my resized image into the appropriate folder. I initially have the droplet create a folder (a folder called “Done”) and then I attempt to have the droplet place the resized (and newly named file) image into the folder “Done”.
My code is as follows:
-- save in Script Editor as Application
-- drag files to its icon in Finder
on open some_items
repeat with this_item in some_items
tell application "Finder"
if not (exists folder "Done" of container of this_item) then
make new folder at container of this_item with properties {name:"Done"}
end if
set the destination_folder to folder "Done" of container of this_item as alias
set the destination_directory to POSIX path of the destination_folder
end tell
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 ¬
(destination_directory as string) & "internet_" & (name of this_item)
save this_image in new_item as typ
end tell
end rescale_and_save
The droplet creates the folder “Done”, but then the droplet application quits after that and there is no resized image in the “Done” folder.
Does anyone know what I’m doing wrong? Am I missing some semantics here? Thanks, again.
You need to designate destination_directory as a global variable or pass it to the handler. This seems to work for jpeg’s
global destination_directory
on open some_items
repeat with this_item in some_items
tell application "Finder"
if not (exists folder "Done" of container of this_item) then
make new folder at container of this_item with properties {name:"Done"}
end if
set the destination_folder to folder "Done" of container of this_item as alias
set the destination_directory to POSIX path of the destination_folder
end tell
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 ¬
(destination_directory as string) & "internet_" & (name of this_item)
save this_image in new_item as typ
end tell
end rescale_and_save
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 ¬
(destination_directory as string) & "internet_" & (name of this_item)
save this_image in new_item as typ
end tell
end rescale_and_save
…as its own separate function.
Thank you! I owe you. This droplet also works with other image types such as TIFF and PNG (test and confirmed!)