Batch Data-driven variables automatically size text to fit a defined area

Discussion of Photoshop Scripting, Photoshop Actions and Photoshop Automation in General

Moderators: xbytor, DavideBarranca, Paul MR

Posts: 1
Joined: Wed Aug 09, 2017 9:11 pm

Batch Data-driven variables automatically size text to fit a defined area

Postby momo75 » Thu Aug 10, 2017 1:24 pm

Hello, I need to make a script to automatically size text to fit a defined area using Batch Data-driven variables
ex I have a list of names that have different length and I have one design that stays fixed I want the names to keep the same space no matter the length of the name
Please check the screenshot to better understand my meaning.

Capture d’écran 2017-08-10 à 00.05.55.png
Capture d’écran 2017-08-10 à 00.05.55.png (112.34 KiB) Viewed 55 times

Posts: 6
Joined: Mon Oct 17, 2016 1:41 pm
Location: USA

Re: Batch Data-driven variables automatically size text to fit a defined area

Postby jaydoubleyou80 » Fri Aug 11, 2017 2:07 pm

The following code assumes you have a layer named Flag, and a layer named Text. You can change that to determine which layer is what by layer order, or whether it's text or pixels, or whatever. I made a test file with a background, a rectangle pixel layer called Flag, and a text layer called Text. I tried with it being both too large, and too small.

Also, I've left the positioning up to you. Once you've scaled the text, you can set the position of the text layer wherever you'd like it.

I will not claim that this is elegant, or the most direct way to do it. I love scripting, but I'm no expert. For instance, I couldn't get the arrays to do the math properly when I added more than one value to them ¯\_(ツ)_/¯ so … 4 arrays it is!

Lastly, you'll have to determine how you want it to iterate through your names list. you could add them all to an array and cycle through until it's reached the end, but I don't know what source you're using so I can't replicate it.

Anyway, here it is:

Code: Select all

#target Photoshop
var startRulerUnits = app.preferences.rulerUnits
app.preferences.rulerUnits = Units.PIXELS
var docRef=app.activeDocument
var flag=docRef.artLayers.getByName ("Flag")
var text=docRef.artLayers.getByName ("Text")
docRef.activeLayer=flag//set active layer to the flag
selectionFromLayer ()
function selectionFromLayer () { //function to make a selection from a layer via scriptlistener plugin
    var idsetd = charIDToTypeID( "setd" );
    var desc3425 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref1008 = new ActionReference();
        var idChnl = charIDToTypeID( "Chnl" );
        var idfsel = charIDToTypeID( "fsel" );
        ref1008.putProperty( idChnl, idfsel );
    desc3425.putReference( idnull, ref1008 );
    var idT = charIDToTypeID( "T   " );
        var ref1009 = new ActionReference();
        var idChnl = charIDToTypeID( "Chnl" );
        var idChnl = charIDToTypeID( "Chnl" );
        var idTrsp = charIDToTypeID( "Trsp" );
        ref1009.putEnumerated( idChnl, idChnl, idTrsp );
    desc3425.putReference( idT, ref1009 );
executeAction( idsetd, desc3425, DialogModes.NO );
var bounds1=new Array
bounds1.push(docRef.selection.bounds[0]) //first pixel location (top left)
var bounds2=new Array
bounds2.push(docRef.selection.bounds[2]) //third pixel location (top right)
var flagWidth=bounds2 - bounds1 //width of flag
docRef.activeLayer=text //set active layer to the text
selectionFromLayer ()
var bounds3=new Array
bounds3.push(docRef.selection.bounds[0]) //first pixel location (top left)
var bounds4=new Array
bounds4.push(docRef.selection.bounds[2]) //third pixel location (top right)
var textWidth=bounds4-bounds3 //width of text
var percentage = flagWidth / textWidth
docRef.activeLayer.resize (percentage*100, percentage*100, AnchorPosition.MIDDLECENTER)
app.preferences.rulerUnits = startRulerUnits

Hope it helps :D

Return to “Photoshop Scripting - General Discussion”

Who is online

Users browsing this forum: No registered users and 1 guest