Author Topic: Free script for batch processing photoshop images  (Read 821 times)

0 Members and 1 Guest are viewing this topic.

Offline mbornbach

  • Newbie
  • *
  • Posts: 34
  • Karma: 0
Free script for batch processing photoshop images
« on: June 11, 2015, 11:46:53 AM »
Here is a photoshop script I pieced together by doing a lot of google searches and reading the applescript dictionary. Feel free to use any part or all of it for your needs.

This script:
- Changes the color mode to cmyk
- Flattens the image
- Selects a path you've drawn with the pen tool and sets it to a clipping path with a flatness of 2 and a name of "path 1"
- Crops the image to 25 pixels outside of the clipping path
- Resizes the image to set the longest side to no more than 1550 pixels
- Adds the text "_s_4c" to the end of the filename
- Adds the text "_ERROR" to the end of the filename if there is an error in photoshop, so I will know I need to take a look at the file in photoshop (usually this happens if I forgot to draw the path with the pen tool)

Code: [Select]
on open droppedfile
tell application "Adobe Photoshop CC 2014"
set display dialogs to never
activate
repeat with afile in droppedfile
tell application "Adobe Photoshop CC 2014"
open afile showing dialogs never

--TELLING PHOTOSHOP TO USE PIXEL MEASUREMENTS AND SETTING THE TARGET IMAGE SIZE SO IT CAN RESIZE THE IMAGE LATER ON
set ruler units of settings to pixel units
set target_size to 1550

--DOING ALL THE PHOTOSHOP WORK
tell current document
try

--CHANGING TO CMYK
change mode to CMYK

--FLATTENING IMAGE
flatten

--SETTING NAME OF PATH AND TURNING IT INTO A CLIPPING PATH
select first path item
if name of first path item is not "Path 1" then set name of first path item to "Path 1"
make clipping path of first path item flatness 2

--CROPPING THE IMAGE
create selection first path item feather amount 0 with antialiasing
expand selection by 25
set theCropBounds to bounds of selection
crop bounds theCropBounds

--RESIZING THE IMAGE IF IT IS TOO BIG
set long_side to height
set long_side_identity to 1
if width > long_side then set long_side_identity to 2
if width > long_side then set long_side to width
if long_side > target_size as integer and long_side_identity = 1 then
resize image height target_size resample method bicubic
end if
if long_side > target_size as integer and long_side_identity = 2 then
resize image width target_size resample method bicubic
end if
close saving yes

on error
close afile

tell application "Finder"
activate
set theFile to afile
set oldDelims to AppleScript's text item delimiters

--ADDING _ERROR TO FILENAME FOR PROBLEM FILES
set AppleScript's text item delimiters to {"."}
set fileName to name of theFile --> ex. Disneyland.tif
set nameWithoutExtension to first text item of fileName --> Disneyland
set newName to nameWithoutExtension & "_ERROR" & ".tif" --> Disneyland_s_4c.tif
set name of theFile to newName

--RESETTING APPLESCRIPT'S TEXT ITEM DELIMITERS
set AppleScript's text item delimiters to oldDelims
end tell
end try
end tell


--DONE WITH PHOTOSHOP, NOW USING THE FINDER TO RENAME THE FILE, ADDING  _s_4c to THE IMAGE NAME
tell application "Finder"
activate
set theFile to afile
set oldDelims to AppleScript's text item delimiters

try
--FOR IMAGES THAT DIDN'T HAVE _s_4c IN FILE NAME BEFORE PROCESSING
if name of theFile does not contain "_s_4c" then
set AppleScript's text item delimiters to {"."}
set fileName to name of theFile --> ex. Disneyland.tif
set nameWithoutExtension to first text item of fileName --> Disneyland
set newName to nameWithoutExtension & "_s_4c" & ".tif" --> Disneyland_s_4c.tif
set name of theFile to newName

--ELIMINATING DOUBLE _s_4c on IMAGES THAT ALREADY HAD _s_4c IN FILE NAME BEFORE PROCESSING
else if name of theFile contains "_s_4c" then
set AppleScript's text item delimiters to {"_s_4c"}
set fileName to name of theFile --> ex. Disneyland.tif
set nameWithoutExtension to first text item of fileName --> Disneyland
set newName to nameWithoutExtension & "_s_4c" & ".tif" --> Disneyland_s_4c.tif
set name of theFile to newName
end if

--RESETTING APPLESCRIPT'S TEXT ITEM DELIMITERS
set AppleScript's text item delimiters to oldDelims

--TELLING FINDER TO RESET APPLESCRIPT'S TEXT ITEM DELIMITERS IF THERE'S AN ERROR
on error
set AppleScript's text item delimiters to oldDelims

end try

end tell
end tell
end repeat
end tell
end open