Use javascript in Photoshop to modify the contents of a text item

。_饼干妹妹 提交于 2021-02-17 05:10:19

问题


I know how to set the contents of a textitem in Photoshop using code like this

var al = doc.activeLayer;
if(al.kind == LayerKind.TEXT) {
    //get the textItem
    var ti = al.textItem;
    //change contents
   ti.contents = "stackoverflow";
}

However is it possible to modify just part of its contents and apply formatting. For example in the work "stackOverflow" I just want to select the first letter and make the font 30px and leave the rest unchanged?


回答1:


There's no easy way to do that to my knowledge. First of all, it's not possible with DOM, so forget using textItem.contents. With AM the tricky part is that AM code for text layers is really precise. Every time you change something, it defines all possible parameters for all "blocks" of text, for example in this case I had a 35px height text layer written in Gudea font and I changed one letter to 70px: https://pastebin.com/XLP64azz You may see that there's a lot of garbage in there and the text layer is considered as 3 separate blocks now: letters from 0 to 5 (lines 64-65: stack), from 5 to 6 (lines 217-218: O) and from 6 to 14 (lines 281-282: verflow). A little bit lower to each block there are lines that set the size, for example

desc62.putUnitDouble( cTID('Sz  '), cTID('#Pxl'), 35.000000 );
desc62.putUnitDouble( sTID('impliedFontSize'), cTID('#Pxl'), 35.000000 );

I know it's possible to remove most of the excessive descriptors (like if you remove descriptors that define font name, the layer will have the font name it had originally), so I guess it's possible to shrink this huge function to several lines that only change a size of a particular letter, but I didn't try that. I guess the algorithm will be like this:

  1. get font size and text with DOM;
  2. split text to blocks (like from 0 to 1 + from 1 to last character to change a font size of a first letter);
  3. use this function to set parameters to specific blocks;

And there'll be problems if there're several styles already exist in the text layer: you'll have to parse layer contents with AM, get all the styles of all the blocks to find the one you're referring to...



来源:https://stackoverflow.com/questions/54684487/use-javascript-in-photoshop-to-modify-the-contents-of-a-text-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!