What's new
Photoshop Gurus Forum

Welcome to Photoshop Gurus forum. Register a free account today to become a member! It's completely free. Once signed in, you'll enjoy an ad-free experience and be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Batch Automation Automated aspect ratio???


shot8un

Member
Messages
5
Likes
0
Hey all,


Just like to say hi, thins is my first post here, I've been wracking my brains over this one. I need to take 100s of jpegs of different sizes and have an action or AppleScript crop them to a specific aspect ratio, say for example 4:3. But I want it to do this regardless of pixel size. So the script would look at the width and then crop the height accordingly to make 4:3


Any ideas anyone?


Thanks in advance.


Scott
 

Paul MR

The Script Master
Messages
382
Likes
206
You can use a script to do this...
Code:
#target photoshop;
app.bringToFront();
main();
function main(){
if(!documents.length) return;
var strtRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var W = activeDocument.width;
var H = activeDocument.height;
var ratio = "4:3";
ratio = ratio.split(":");
if(W<H) ratio=ratio.reverse();
var Hpics =(W/Number(ratio[0]))*Number(ratio[1]);
if(Hpics > H){
    Wpics = (H/Number(ratio[1]))*Number(ratio[0]);
    Hpics = H;
    }else{
        Wpics = W;
        }
var Left,Top,Right,Bottom=0;
if (W==Wpics) Left = 0; Right = W; 
if (W!=Wpics) {
    var diffw = (W-Wpics)/2;
    Left = diffw; Right = Wpics+diffw;
    }
if (H==Hpics) Top = 0; Bottom = H;
if( H!=Hpics){
    var diffh = (H-Hpics)/2;
    Top = diffh; Bottom = Hpics+diffh;
    }
activeDocument.selection.select([[Left,Top],[Right,Top],[Right,Bottom],[Left,Bottom]], SelectionType.REPLACE, 0, false); 
executeAction(charIDToTypeID( "Crop" ), new ActionDescriptor(), DialogModes.NO );
activeDocument.selection.deselect();
app.preferences.rulerUnits = strtRulerUnits;
};
 

shot8un

Member
Messages
5
Likes
0
Many many thanks.....

I've got that loaded in to my scripts, how would I edit this to also do 3:2

Scott
 

Paul MR

The Script Master
Messages
382
Likes
206
Just change the line
var ratio = "4:3";
to
var ratio = "3:2";

All should then work.
 

shot8un

Member
Messages
5
Likes
0
Thats what I would of thought and tried that but got this result, the 4x3 works perfectly

Screen Shot 2017-04-20 at 16.40.39.png
 

Paul MR

The Script Master
Messages
382
Likes
206
The code is correct, it might be that you are not using a plain text editor?
Some editors will add control characters that will stop the script working.
 

shot8un

Member
Messages
5
Likes
0
Thanks Paul, spot on.

I found a code editor called sublime txt, change the code using that and it worked beautifully.
Thanks for taking the time to help me out, appreciate it.

Scott
 

Top