简体   繁体   中英

Photoshop script to split text layer into words

Using Photoshop 2017, I am splitting a text layer into words:

在此处输入图片说明

Using the following script:

 psd = app.activeDocument; layer = psd.activeLayer; var text = layer.textItem.contents; var textArray = text.split(" "); var lngth = textArray.length+1; for (var w=1; w<lngth;w++){ wordlayer = layer.duplicate(); wordlayer.name = textArray[w-1]; wordlayer.textItem.contents = textArray[w-1]; psd.activeLayer = wordlayer; }

Which produces this result:

在此处输入图片说明

What I'm having a problem with, as you can tell, is the positioning. Is it possible to get the original coordinates of each word so that I can position each text layer appropriately?

Another possible approach: I thought about duplicating each layer as it is and changing the colour transparency of the other words to 0%. The closest question I found to it is this one [ExtendScript]Change font color, size of certain word in text layer , but even if that did work, It doesn't look like setting alpha/transparency for individual words is possible anyway.

You need to take into account the leading (vertical space in lines of text) and the original font size. Here's your amended script.

// Switch off any dialog boxes
displayDialogs = DialogModes.NO; // OFF

psd = app.activeDocument;
layer = psd.activeLayer;
var text = layer.textItem.contents;
var textArray = text.split(" ");
var lngth = textArray.length+1;


for (var w=1; w<lngth;w++)
{
    wordlayer = layer.duplicate();
    wordlayer.name = textArray[w-1];
    wordlayer.textItem.contents = textArray[w-1];
    psd.activeLayer = wordlayer;
    var fontsize = wordlayer.textItem.size;
    var leading = wordlayer.textItem.leading;
    //alert("fontsize: " + fontsize + "\nleading: " + leading);
    translate(0,leading*(w-1));
}


// Switch off any dialog boxes
displayDialogs = DialogModes.ALL; // OFF

function translate(x,y)
{
  // =======================================================
  var idTrnf = charIDToTypeID( "Trnf" );
  var desc179 = new ActionDescriptor();
  var idnull = charIDToTypeID( "null" );
  var ref38 = new ActionReference();
  var idLyr = charIDToTypeID( "Lyr " );
  var idOrdn = charIDToTypeID( "Ordn" );
  var idTrgt = charIDToTypeID( "Trgt" );
  ref38.putEnumerated( idLyr, idOrdn, idTrgt );
  desc179.putReference( idnull, ref38 );
  var idFTcs = charIDToTypeID( "FTcs" );
  var idQCSt = charIDToTypeID( "QCSt" );
  var idQcsa = charIDToTypeID( "Qcsa" );
  desc179.putEnumerated( idFTcs, idQCSt, idQcsa );
  var idOfst = charIDToTypeID( "Ofst" );
  var desc180 = new ActionDescriptor();
  var idHrzn = charIDToTypeID( "Hrzn" );
  var idPxl = charIDToTypeID( "#Pxl" );
  desc180.putUnitDouble( idHrzn, idPxl, x ); // x 
  var idVrtc = charIDToTypeID( "Vrtc" );
  var idPxl = charIDToTypeID( "#Pxl" );
  desc180.putUnitDouble( idVrtc, idPxl, y ); // y
  var idOfst = charIDToTypeID( "Ofst" );
  desc179.putObject( idOfst, idOfst, desc180 );
  var idIntr = charIDToTypeID( "Intr" );
  var idIntp = charIDToTypeID( "Intp" );
  var idbicubicAutomatic = stringIDToTypeID( "bicubicAutomatic" );
  desc179.putEnumerated( idIntr, idIntp, idbicubicAutomatic );
  executeAction( idTrnf, desc179, DialogModes.NO );
}

You may have to take in to account spaces between words as well as line breaks in your creation of textArray .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM