I found this code for resizing images. It works as a droplet (save as app.) I set the code to resize image to “200” and pre-pend “200_” to the new file name. It works great. However, rather than prepend “200_” I’d rather have the script add the size to the end of the name. Assuming original name of file is “image.jpg” I want the new resized image to be saved with the name of “image_200.jpg.” Can someone please help?
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 200
-- 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) & "_200" & (name of this_item)
save this_image in new_item as typ
end tell
end rescale_and_save
You may try :
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 200
-- 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"
set theFolder to (container of this_item as string)
set oldName to (name of this_item)
end tell
# Split the name upon the period(s)
set asList to my decoupe(theName, ".")
# Drop the name extension
set bareName to my recolle(items 1 thru -2 of asList, ".")
# Build the new pathname
set new_item to theFolder & ":" & bareName & "_200" & "." & asList's item -1
save this_image in new_item as typ
end tell
end rescale_and_save
#=====
on decoupe(t, d)
local oTIDs, l
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set l to text items of t
set AppleScript's text item delimiters to oTIDs
return l
end decoupe
#=====
on recolle(l, d)
local oTIDs, t
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set t to l as text
set AppleScript's text item delimiters to oTIDs
return t
end recolle
#=====
Yvan KOENIG (VALLAURIS, France) jeudi 25 juin 2015 15:38:47