skip to Main Content

I’m designing hundreds of posters that all have different text – but should all be at the same X,Y position – reference point being the top left: X= 213 px and Y= 41 px

Some are a little off and I’d love to get them corrected, quickly and through automation.

It’s easy enough to create an action to transform text, but since the text content is different from file to file, I can’t automate that portion.

So looking for a script that essentially selects the text layer. There’s only one text layer in all of these documents so something like “function: gettextlayer” and then select that layer in the layer panel.

I can do the transform bit via action automation from there.

Been scratching my head at this one and have dug everywhere.

2

Answers


  1. If the text layer is never in a layerset you can use the following snippet to find and move it…

    var doc = app.activeDocument;
    for (var i = 0; i < doc.layers.length; i++) {
        var lyr = doc.layers[i];
    
        if (lyr.kind == LayerKind.TEXT) {
            //bounds order is top, left, bottom right
            var dx = 213 - lyr.bounds[0].as("px") ;
            var dy = 41 - lyr.bounds[1].as("px");
    
            lyr.translate(dx, dy);
        }
    
    }
    

    Be aware that the bounds relate to the text box, not the text itself. So particularly in the case of a paragraph text box this may not be what you’re expecting. If you need the bounds of the text itself, you’ll want to rasterize a copy of the text layer and do the maths off that layer instead.

    If it can be in a layerset it gets a bit harder because you have to loop through each layerset recursively to find it. Unless you go get yourself a copy of the stdlib.js file out of xtools. A fab library to have hanging around if you’re going to do any scripting. Once you have that file you can just use…

    #include "stdlib.js"
    var doc = app.activeDocument;
    var lyr = Stdlib.findLayerByProperty(doc, "kind", LayerKind.TEXT, false);
    var dx = 213 - lyr.bounds[0].as("px") ;
    var dy = 41 - lyr.bounds[1].as("px");
    lyr.translate(dx, dy);
    

    The rest of the text properties can be accessed off the ‘textItem’ proprty of the layer once you’re found it. E.g:

    lyr.textItem.contents = "some new text";
    lyr.textItem.font = "fontname";
    

    See the Javascript Reference pdf in your Photoshop install directory for more info.

    Login or Signup to reply.
  2. You can get position from textLayers and regular ones like this

    function getPosition(layer) {
        if (layer.textItem) {
            var X = parseFloat(layer.textItem.position[0]);
            var Y = parseFloat(layer.textItem.position[0]);
        } else {
            var X = parseFloat(layer.bounds[0]);
            var Y = parseFloat(layer.bounds[1]);
        }
        return {x:X,y:Y}
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search