02.12.2012 19:11:00 • Categories: Applescript • Tags: Applescript
Image Events mit AppleScript
-- save in Script Editor as an Application, then -- drag image files onto the Application's icon on open some_items repeat with this_item in some_items try rescale_and_save(this_item) end try end repeat end opento rescale_and_save(this_item) tell application "Image Events" launch set the target_width to 120 -- open the image file set this_image to open this_item set typ to file type of this_image 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
property openTypes : {"PDF", "com.adobe.pdf", "BMP", "com.microsoft.bmp", "JPEG", "JPEG2", "jpg", "public.jpeg", ¬
"PICT", "com.apple.pict", "PNG", "public.png", "PSD", "com.adobe.photoshop-image", "TIFF", "public.tiff"}
--Get the artwork file
set theFiles to choose file with prompt "Choose art file(s)" of type openTypes with multiple selections allowed without invisibles
runConversion(theFiles)
on open someFiles
runConversion(someFiles)
end open
on runConversion(theItems)
set saveFolder to choose folder with prompt "Save resized pictures where?" without multiple selections allowed and invisibles
tell application "Image Events"
launch
set newHeight to 600
set newWidth to 0
if (count items of theItems) is greater than 0 then
repeat with anItem in theItems
set imageFile to (open anItem)
set theSize to dimensions of imageFile
set width to item 1 of theSize
set height to item 2 of theSize
set ratio to (width / height)
set newWidth to (ratio * newHeight) as integer
if newHeight > newWidth then
scale imageFile to size newHeight
else
scale imageFile to size newWidth
end if
save imageFile as JPEG in saveFolder
close imageFile
end repeat
else
display dialog "Nothing to convert."
end if
end tell
end runConversion
global theImage-- Prompt the user to select a file set theImage to choose file with prompt "Please select an image file:" without invisibles -- Prompt the user to enter the width of the picture set theResult to display dialog "Saisissez la largeur de l'image." default answer "640" set theAnswer to text returned of theResult
-- Retrieve the resize percentage amount set theNewWidth to theAnswer as integer
-- Determine a path in which to save the resized image tell application "Finder" set theImageName to name of theImage set theImageFolder to (folder of theImage) as string end tell set theResizedImagePath to theImageFolder & "Resized-" & theImageName as string
-- Open the image, resize it, and save it as a new file tell application "Image Events" launch set theImage to open theImage
-- This is the tricky part: you need to keep the ratio of the original picture copy dimensions of theImage to {currentWidth, currentHeight} set ratio to theNewWidth / currentWidth
tell theImage scale by factor ratio to size theNewWidth save in theResizedImagePath close end tell end tell
set this_file to choose filetry tell application "Image Events" launch set this_image to open this_file scale this_image to size 318 rotate this_image to angle 45 pad this_image to dimensions {800, 600} save this_image with icon close this_image end tell on error error_message display dialog error_message end try