0

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.

dontdad
  • 71
  • 1
  • 7

2 Answers2

0

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.

Anna Forrest
  • 1,711
  • 14
  • 21
  • Thanks Anna, This certainly selects the text layer and moves it - it isn't in layer set so that first script works without jumping in to xtools. Awesome! Only issue is that the transform point is at the center, and if the text is a different size it puts it into different places. Ideally it's looking at the top left of each text box, which should always land at 213, 41. Also curious about using this script to change the font and font size of the text? Sorry for the delay and huge thanks! – dontdad Mar 09 '15 at 14:46
  • In other words - changing the Reference Point Location to be top left rather than center. – dontdad Mar 09 '15 at 16:29
  • added a bit more to the answer - you aren't setting the reference point location at all - you're just doing the maths to figure out how far you need to move the layer, because translate takes a delta, not an absolute position. If it is not working as you expect, I suggest you but a break point in and double check the bounds coords your getting make sense for the text layer you're looking at. – Anna Forrest Mar 10 '15 at 12:36
  • ah-ha. Yeah I'm needing to place it absolutely because the text is a different size in every document - IE different character count. So the text "Josh" and "Josh Rogen" will place differently using the same dx / dy. All the documents are the same size though. Thoughts? Huge thanks! – dontdad Mar 10 '15 at 18:17
  • You should be recalculating your dx,dy for every document. If you post the script you're actually using I can have a look and see if I can spot any problems. – Anna Forrest Mar 11 '15 at 12:54
  • I'm using your script :) - it's putting the text in a different place in every document... rather than uniformly placing the text. – dontdad Mar 16 '15 at 18:29
  • put a break point in and see if dx and dy are proper unitvalue objects. If not use this to make them so: new UnitValue(dx, 'px') and see if that fixes it. – Anna Forrest Mar 17 '15 at 22:12
  • Can you put that in context? Modify the answer? Just don't know exactly where that should go in the string. – dontdad Mar 23 '15 at 18:48
0

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}
}
microbians
  • 455
  • 5
  • 11