Skip to main content
Iuigidesu
Inspiring
August 19, 2024
Answered

Script to round up text size?

  • August 19, 2024
  • 4 replies
  • 2621 views

Hi, I'm sorry if this is silly, but I was wondering if there could be a way to automate rounding up/down text sizes? I had to rezise some of my psds and ended up with really weird text sizes, like 71.867pt instead of 72pt, I tried to fix it by changing the dpi on the image, and it solved that one document, but If i try to do it in another document it'll give me weird sizes again, and while I can try to manually fix every document dpi, Im working in about 750 documents, so it'll take forever.
I can also change the individual text layer size, but it's the same problem because I got to change every single layer size and I was wondering if someone has some kind of script that could round up every text layer size to its nearest whole number.
Thank you all

This topic has been closed for replies.
Correct answer Lumigraphics

Modification to set ruler units to points, which is useful when working with type. This adds logic to set type size to an enumerated list of type sizes instead of simply rounding. You can comment out that block and use the simple round method if that works better. You can also change the list of sizes to suit.

 

 

/*
Utility Pack Scripts created by David M. Converse ©2018-24

This Photoshop script changes text layer sizes to the nearest integer

Last modifed 8/22/2024

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#target photoshop

roundupTextSizes();

function roundupTextSizes(){
    if(!app.documents.length > 0){ //must have an open document
        return;
        }
    //initialize variables
    var docRef = null;
    var vis = true;
    var LayerRef = null;
    var TextRef = null;
    var TextSize = 0;
    var enumSizes = [8, 12, 16, 20, 24, 32, 36, 48, 60, 72];
    try{
        var originalDialogMode = app.displayDialogs;
        app.displayDialogs = DialogModes.ERROR;
        var originalRulerUnits = preferences.rulerUnits;
        docRef = app.activeDocument;
        preferences.rulerUnits = Units.POINTS;
        docRef.activeLayer = docRef.artLayers[0];
        for(var i = 0; i < docRef.artLayers.length; i++){ //loop through art layers
            enumSizes = [8, 12, 16, 20, 24, 32, 36, 48, 60, 72] //reset list
            LayerRef = docRef.artLayers[i];
            vis = true;
            if(LayerRef.kind == LayerKind.TEXT){
                if(!LayerRef.visible){ //handle invisible layers
                    vis = false;
                    }
                TextRef = LayerRef.textItem;
                TextSize = TextRef.size; //get text size
                //can use different logic here
                //TextSize = Math.round(TextSize); //simple round down
                //set size to enum value, comment block out and uncomment line above for simple rounding
                enumSizes.push(TextSize); //add to enum array
                enumSizes.sort(function(a,b){return a-b;}) //sort by value
                for(var j = 0; j < enumSizes.length; j++){
                    if(j == enumSizes.length - 1){
                        TextRef.size = Math.round(TextSize); //larger than enum list, simple round down
                        break;
                        }
                    if(enumSizes[j] == TextSize){ //find current
                        TextRef.size = enumSizes[j + 1]; //change to next enum value
                        break;
                        }
                    }
                //end set to enum value
                if(vis == false){
                    LayerRef.visible = false; //reset to hidden as needed
                    }
                }
            }
        preferences.rulerUnits = originalRulerUnits;
        app.displayDialogs = originalDialogMode;
        }
    catch(e){
        Window.alert(e + e.line);
        }
    }

 

4 replies

c.pfaffenbichler
Community Expert
Community Expert
August 22, 2024

Please provie a sample file. 

Iuigidesu
IuigidesuAuthor
Inspiring
August 22, 2024

I'm using pt as units 😕😕

LumigraphicsCorrect answer
Legend
August 22, 2024

Modification to set ruler units to points, which is useful when working with type. This adds logic to set type size to an enumerated list of type sizes instead of simply rounding. You can comment out that block and use the simple round method if that works better. You can also change the list of sizes to suit.

 

 

/*
Utility Pack Scripts created by David M. Converse ©2018-24

This Photoshop script changes text layer sizes to the nearest integer

Last modifed 8/22/2024

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#target photoshop

roundupTextSizes();

function roundupTextSizes(){
    if(!app.documents.length > 0){ //must have an open document
        return;
        }
    //initialize variables
    var docRef = null;
    var vis = true;
    var LayerRef = null;
    var TextRef = null;
    var TextSize = 0;
    var enumSizes = [8, 12, 16, 20, 24, 32, 36, 48, 60, 72];
    try{
        var originalDialogMode = app.displayDialogs;
        app.displayDialogs = DialogModes.ERROR;
        var originalRulerUnits = preferences.rulerUnits;
        docRef = app.activeDocument;
        preferences.rulerUnits = Units.POINTS;
        docRef.activeLayer = docRef.artLayers[0];
        for(var i = 0; i < docRef.artLayers.length; i++){ //loop through art layers
            enumSizes = [8, 12, 16, 20, 24, 32, 36, 48, 60, 72] //reset list
            LayerRef = docRef.artLayers[i];
            vis = true;
            if(LayerRef.kind == LayerKind.TEXT){
                if(!LayerRef.visible){ //handle invisible layers
                    vis = false;
                    }
                TextRef = LayerRef.textItem;
                TextSize = TextRef.size; //get text size
                //can use different logic here
                //TextSize = Math.round(TextSize); //simple round down
                //set size to enum value, comment block out and uncomment line above for simple rounding
                enumSizes.push(TextSize); //add to enum array
                enumSizes.sort(function(a,b){return a-b;}) //sort by value
                for(var j = 0; j < enumSizes.length; j++){
                    if(j == enumSizes.length - 1){
                        TextRef.size = Math.round(TextSize); //larger than enum list, simple round down
                        break;
                        }
                    if(enumSizes[j] == TextSize){ //find current
                        TextRef.size = enumSizes[j + 1]; //change to next enum value
                        break;
                        }
                    }
                //end set to enum value
                if(vis == false){
                    LayerRef.visible = false; //reset to hidden as needed
                    }
                }
            }
        preferences.rulerUnits = originalRulerUnits;
        app.displayDialogs = originalDialogMode;
        }
    catch(e){
        Window.alert(e + e.line);
        }
    }

 

Legend
August 21, 2024

This script will iterate through your text layers and round the sizes to the nearest integer. Save this as a PLAIN TEXT file with .jsx extension and save to your Photoshop scripts folder.

https://helpx.adobe.com/photoshop/using/scripting.html

 

/*
Utility Pack Scripts created by David M. Converse ©2018-24

This Photoshop script changes text layer sizes to the nearest integer

Last modifed 8/21/2024

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#target photoshop

roundupTextSizes();

function roundupTextSizes(){
    if(!app.documents.length > 0){ //must have an open document
        return;
        }
    //initialize variables
    var docRef = null;
    var vis = true;
    var LayerRef = null;
    var TextRef = null;
    var TextSize = 0;
    try{
        docRef = app.activeDocument;
        docRef.activeLayer = docRef.artLayers[0];
        for(var i = 0; i < docRef.artLayers.length; i++){ //loop through art layers
            LayerRef = docRef.artLayers[i];
            vis = true;
            if(LayerRef.kind == LayerKind.TEXT){
                if(!LayerRef.visible){ //handle invisible layers
                    vis = false;
                    }
                TextRef = LayerRef.textItem;
                TextSize = TextRef.size; //get text size
                //can use different logic here
                TextSize = Math.round(TextSize);
                TextRef.size = TextSize; //change to rounded size
                if(vis == false){
                    LayerRef.visible = false; //reset to hidden as needed
                    }
                }
            }
        }
    catch(e){
        Window.alert(e + e.line);
        }
    }

 

Iuigidesu
IuigidesuAuthor
Inspiring
August 21, 2024

It definitely works, since it's giving me whole numbers and its essentially what I was looking for, however, the numbers are way too far away from the original, some examples are 4.09pt to 8pt and 5.87pt to 3pt, I tried looking for the Math.round function online and honestly i couldnt figure out how to fix it. I dunno what might cause it, I tried changing my rule units from pt to px and it made it even worse... 😭

Legend
August 22, 2024

This is a sample, I didn't know if that even was what you needed. We can set the units, and a full production script would have to be fleshed out a bit more. But is this the direction you want to go?

Legend
August 20, 2024

Its easy if each type layer has the same size text. Its doable but MUCH more complex if you have multiple text sizes in one layer.

Iuigidesu
IuigidesuAuthor
Inspiring
August 20, 2024

Yeah if that were the case, I'd just select them all and set a size, however I'm working in documents with a bunch of different sizes, fonts and text layers, so that's not an option 😰

I'll upload a sample of my documents later so you guys can get an idea 

c.pfaffenbichler
Community Expert
Community Expert
August 21, 2024

But does one Type Layer contain text of different font sizes? 

c.pfaffenbichler
Community Expert
Community Expert
August 20, 2024

Please provie a sample file. 

Do Type Layers only ever contain text of one font size or can they combine different font sizes?