Photoshop脚本:创建选区,然后在其中放入文本框

时间:2019-03-11 23:19:52

标签: javascript adobe photoshop extendscript photoshop-script

在Photoshop中使用Javascript(ExtendScript),我正在尝试制作一个动态文本框。

我的最终目标是一个脚本,用户将自己的文本写入到弹出的新窗口中,然后将该文本放置在图像的底部10%中,并动态缩放以适合图像的底部10%。< / p>

因此,无论较低10%的尺寸是多少(由resizeCanvas参数创建),文本始终都适合在其中。因此,当用户添加一些单词时,它看起来不会太小;当用户添加较长的句子时,它也不会溢出。

此处最初提出的问题(带有图片)https://forums.adobe.com/message/10952562#10952562

示例起始图片:

start image

弹出窗口:

custom PopUp

小文本结果:

outcome1

长文本结果:

outcome 2

这是我目前所在的位置:

#target Photoshop  
  
app.preferences.rulerUnits = Units.PIXELS;  
app.preferences.typeUnits = TypeUnits.PIXELS;  
var doc = app.activeDocument;  
var hgt10 = doc.height / 10;  
var FillColor = new SolidColor;  
FillColor.rgb.hexValue = 'ff0000';  
var newHgt = doc.height + hgt10  
doc.resizeCanvas(doc.width, newHgt, AnchorPosition.TOPCENTER);  
  
  var win = new Window('dialog', "Custom Text");  
  var grp1 = win.add('group');  
  grp1.alignChildren = "top";  
  var txt = grp1.add('edittext', [0, 0, 300, 150], "Write your message", {multiline: true, scrolling: true});  
  txt.active = true;  
  var grp2 = win.add('group');  
  var goBtn = grp2.add('button', undefined, "Run");  
  grp2.add('button', undefined, "Cancel");  
    
  var newLyr = doc.artLayers.add();  
  newLyr.kind = LayerKind.TEXT;  
  // CONTENTS  
  var txtLyr = newLyr.textItem  
  txtLyr.font = "ArialMT"  
  txtLyr.contents = txt.text  
  var uV = new UnitValue(20,'mm')  
  txtLyr.size = UnitValue(25,'mm')  
  txtLyr.color = FillColor;  
  txtLyr.position = [25, 2200] // x, y - Need to fix position to fit in the lower 10%  
  
  goBtn.onClick = function() {  
    win.close();   
  }  
  
win.show();     

所以我需要帮助的领域是:

1)将文本放置在新区域中(由resizeCanvas参数创建,扩展了10%)

2)添加换行符并自动调整大小以适合各种长度的文本文件。

问候,

1 个答案:

答案 0 :(得分:0)

要添加换行符,请用\n替换所有\r(至少对于Windows,我感觉此Mac版本可能还需要其他内容,但不确定):

txtLyr.contents = txt.text.replace(/\n/gm, '\r')

要定位文本,您需要知道大小(取决于行号)和前导。

var lines = txt.text.split('\n'); //getting the number of lines
var maxSize = hgt10 / lines.length; //calculating max size for a line (including the leading) = 10% of doc height devided by lines number
newLyr.textItem.useAutoLeading = true // making sure that text uses autoleading
var leading = newLyr.textItem.autoLeadingAmount; //autoleading value is in % of text size
maxSize = maxSize / leading * 100; // recalculating text size based on leading in pixels
txtLyr.size = maxSize;
txtLyr.position = [25, originalDocHeight + maxSize]

一行一行的结果:

enter image description here