Skip to main content
roberts77626518
Participating Frequently
November 21, 2025
Answered

Script to rename files to a global file name and append a ratio number based on the width and height

  • November 21, 2025
  • 2 replies
  • 140 views

Here is my proposed workflow:

 

  1. Five open psd files all of the same subject but created from different templates for printing on different papers with different ratios, e.g. 3x2, 4x3, 5x4, 14x11 and A1.
  2.  Open script that allows me to input a global name for each active doc.
  3.  Script then looks at each file's width and height in pixels and appends 3x2 or 4x3 or 5x4 or 14x11 or A1 to the global file name so that each file has a unique name.
  4.  Opens a Save As box to allow me to point to a save location.
  5.  Proceeds to Save a copy of each file as a jpeg with a quality option of 12.
  6.  end.

 

I've written script in the past but I am somewhat rusty. I'll need this routine to do the same thing for portrait orientation too but I can modify it on my own. I tried using AI to generate simpler scripts that I could put together but they don't work. Debugging in Photoshop uses UXP and I'm not familiar with that tool.

 

MacOS Ventura 13

 

Thanks

Correct answer ExUSA

If you search on here, you'll find numerous file saving scripts. You can also use Image Processor (built-in) and run a script afterward to rename files based on dimensions. You could also use Adobe Bridge to do this and bypass Photoshop.

 

Below is a basic sample, you'd need to write the logic to process dimensions and map them to the enumerated list of prefixes you have.

 

try{
if(documents.length > 0){
    var originalDialogMode = app.displayDialogs;
    app.displayDialogs = DialogModes.ERROR;
    var originalRulerUnits = preferences.rulerUnits;
    preferences.rulerUnits = Units.PIXELS;
    var docH = 0;
    var docW = 0;
    var docRatio = 1;
    var baseName = Window.prompt('Enter the base name');
    var jpegOpt = new JPEGSaveOptions();
    jpegOpt.embedColorProfile = true;
    jpegOpt.quality = 12;
    jpegOpt.matte = MatteType.NONE;
    jpegOpt.formatOptions = FormatOptions.STANDARDBASELINE;
    if(baseName != null){
        var expFolder = Folder.selectDialog('Select Export Folder');
        for(var a = 0; a < documents.length; a++){
            docH = documents[a].height;
            docW = documents[a].width;
            docRatio = docH / docW;

            //insert logic to map ratio to enumerated ratio name
            
            var newName = baseName + docRatio.toString();
            var F = expFolder.fsName + '/' + newName + '.jpg';
            documents[a].saveAs(File(F), jpegOpt, true);
            }
        }
    preferences.rulerUnits = originalRulerUnits;
    app.displayDialogs = originalDialogMode;
    }
    }
catch(e){
    Window.alert(e + e.line);
    }

 

2 replies

Stephen Marsh
Community Expert
Community Expert
November 21, 2025

If you always have a fixed canvas pixel width and height which equates to a given ratio, then you can simply map this to a preset name, no need to calculate.

 

If the pixels are variable, then you may need to calculate the ratio. Getting the aspect ratio isn't always an easy task. When I first started scripting I found 3 different methods that don't always produce the same result or match the results of say Adobe Bridge.

 

https://raw.githubusercontent.com/MarshySwamp/Photoshop-Document-Dimensions---MP-Value---Aspect-Ratio/refs/heads/main/Photoshop%20Document%20Dimensions%20-%20MP%20Value%20-%20Aspect%20Ratio.jsx

 

You will probably get the most consistent results using the "GCD" or "Farey" methods.

 

You could just program the A1 fallback if the ratio is not equal to 3x2, 4x3 etc to simply equal A1. Is it always going to be A1? Or will other standard A series paper sizes need to be considered? As print size is determined by PPI, the resolution may also play a role with rounding which may result in slight variations from the basic math from the ideal 1:1.41 ratio. 

 

P.S. If you are using artboards, then this adds further complications as Photoshop only has one canvas, so you would need to base calculations off each artboard bounds.

ExUSACorrect answer
Legend
November 21, 2025

If you search on here, you'll find numerous file saving scripts. You can also use Image Processor (built-in) and run a script afterward to rename files based on dimensions. You could also use Adobe Bridge to do this and bypass Photoshop.

 

Below is a basic sample, you'd need to write the logic to process dimensions and map them to the enumerated list of prefixes you have.

 

try{
if(documents.length > 0){
    var originalDialogMode = app.displayDialogs;
    app.displayDialogs = DialogModes.ERROR;
    var originalRulerUnits = preferences.rulerUnits;
    preferences.rulerUnits = Units.PIXELS;
    var docH = 0;
    var docW = 0;
    var docRatio = 1;
    var baseName = Window.prompt('Enter the base name');
    var jpegOpt = new JPEGSaveOptions();
    jpegOpt.embedColorProfile = true;
    jpegOpt.quality = 12;
    jpegOpt.matte = MatteType.NONE;
    jpegOpt.formatOptions = FormatOptions.STANDARDBASELINE;
    if(baseName != null){
        var expFolder = Folder.selectDialog('Select Export Folder');
        for(var a = 0; a < documents.length; a++){
            docH = documents[a].height;
            docW = documents[a].width;
            docRatio = docH / docW;

            //insert logic to map ratio to enumerated ratio name
            
            var newName = baseName + docRatio.toString();
            var F = expFolder.fsName + '/' + newName + '.jpg';
            documents[a].saveAs(File(F), jpegOpt, true);
            }
        }
    preferences.rulerUnits = originalRulerUnits;
    app.displayDialogs = originalDialogMode;
    }
    }
catch(e){
    Window.alert(e + e.line);
    }