Photoshop Scripting:如何知道溢出的文本部分

Photoshop Scripting: How to know the part of the text that has overflowed

我想在多张幻灯片之间展开一段很长的文字,因为一张幻灯片放不下。

我手动执行此操作,方法是查看文本在一张幻灯片中停止的位置,然后在下一张幻灯片中从那里继续(直到打印出整个文本)。

有没有办法自动执行此过程?也许有一种方法可以检索 实际上 显示的文本部分?

这可能不是您想要的,但这是一个开始。

它在图像中创建一行文本。对于我的示例,我使用了一个 640 x 480 @72dpi 的新文档。

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

// call the source document
var srcDoc = app.activeDocument;

var myString = "The quick brown fox jumps over the lazy dog";

var w = srcDoc.width.value;
var h = srcDoc.height.value;

var maxWidth = w-20;
var remanderText = "";

for (i = myString.length; i > 0; i--)
{
  var text = myString.slice(0, i);
  create_text("Arial", 36.0, text, 320, 230, "c", maxWidth);

  var sb = get_selection_bounds();

  if (sb[0] < maxWidth)
  {
    //ok
    remanderText = myString.slice(i, myString.length);
    break;
  }
  else
  {
    // delete layer
    srcDoc.activeLayer.remove();
  }
}
alert(remanderText);



// function CREATE TEXT(typeface, size, R, G, B, text content, text X pos, text Y pos)
// --------------------------------------------------------
function create_text(fface, size, content, tX, tY, just, maxw)
{
  var line = content.split("\n");

  // Thanks to visioN for the regex help
  // replaces \n with \r carriage return
  content = content.replace(/\n|<br\s*\/?>/gi, "\r");


// Add a new layer in the new document
  var artLayerRef = app.activeDocument.artLayers.add();

  // Specify that the layer is a text layer
  artLayerRef.kind = LayerKind.TEXT;

  artLayerRef.name = "text";

  //This section defines the color of the hello world text
  textColor = new SolidColor();
  textColor.rgb.red = 0;
  textColor.rgb.green = 0;
  textColor.rgb.blue = 0;

  //Get a reference to the text item so that we can add the text and format it a bit
  textItemRef = artLayerRef.textItem
  textItemRef.font = fface;
  textItemRef.contents = content;
  textItemRef.color = textColor;
  textItemRef.size = size
  textItemRef.position = new Array(tX, tY) //pixels from the left, pixels from the top


  if (just == "CENTRE" || just == "CENTER" || just = "C" || just = "c") just = Justification.CENTER
  else if (just == "RIGHT" || just == "right" || just = "R" || just = "r") just = Justification.RIGHT
  else if (just == "LEFT" || just == "left" || just = "L" || just = "l") just = Justification.LEFT
  else (just = Justification.LEFT);

  activeDocument.activeLayer.textItem.justification = just;
}

// function GET SELECTION BOUNDS ()
// ----------------------------------------------------------------
function get_selection_bounds()
{
  select_layer_as_selection();

  var x = parseFloat(app.activeDocument.selection.bounds[0]);
  var y = parseFloat(app.activeDocument.selection.bounds[1]);
  var x1 = parseFloat(app.activeDocument.selection.bounds[2]);
  var y1 = parseFloat(app.activeDocument.selection.bounds[3]);

  var selW = parseFloat(x1-x);
  var selH = parseFloat(y1-y);

  // select nothing; Deselect all
  app.activeDocument.selection.deselect();

  // return the results as an array
  return [(selW), (selW)];
}


// function SELECT LAYER AS SELECTION()
// --------------------------------------------------------
function select_layer_as_selection()
{
    var id1268 = charIDToTypeID( "setd" );
    var desc307 = new ActionDescriptor();
    var id1269 = charIDToTypeID( "null" );
    var ref257 = new ActionReference();
    var id1270 = charIDToTypeID( "Chnl" );
    var id1271 = charIDToTypeID( "fsel" );
    ref257.putProperty( id1270, id1271 );
    desc307.putReference( id1269, ref257 );
    var id1272 = charIDToTypeID( "T   " );
    var ref258 = new ActionReference();
    var id1273 = charIDToTypeID( "Chnl" );
    var id1274 = charIDToTypeID( "Chnl" );
    var id1275 = charIDToTypeID( "Trsp" );
    ref258.putEnumerated( id1273, id1274, id1275 );
    desc307.putReference( id1272, ref258 );
    executeAction( id1268, desc307, DialogModes.NO )
    return;
}