简体   繁体   English

使用Javascript在Photoshop中编辑文本图层的内容

[英]Editing Content of a Text Layer in Photoshop Using Javascript

I am trying to write a script to edit the content of a Text Layer in Photoshop CS6. 我正在尝试编写脚本来编辑Photoshop CS6中文本层的内容。 Is that possible? 那可能吗?

I have about 2000 images that I need to process for a work project. 我有大约2000张图片,需要为一个工作项目处理。 First I am adding the filename of each image as a text layer in Photoshop using a javascript I already have (see below). 首先,我使用已有的JavaScript在Photoshop中将每个图像的文件名添加为文本层(请参见下文)。 A sample filename is "UCMC_0018015 D FSH E." 示例文件名是“ UCMC_0018015 D FSH E”。 My script successfully adds this filename to the image as a text layer in Photoshop. 我的脚本成功将此文件名添加到图像中,作为Photoshop中的文本层。

However, I would then like to edit the text layer in order to replace the underscore with a space, and removing " FSH E" from the end of the text string (all file names have these elements, but the numbers in the file name varies from file to file). 但是,我然后想编辑文本层,以便用空格替换下划线,并从文本字符串的末尾删除“ FSH E”(所有文件名都有这些元素,但是文件名中的数字会有所不同从文件到文件)。 Can anyone help me with the script I need to do this? 谁能帮我完成我需要做的脚本? I am new to writing and running scripts, but I am doing my best to learn on the job. 我不熟悉编写和运行脚本,但是我正在竭尽全力地学习这份工作。 Any advice you can give me would be appreciated. 您能给我的任何建议将不胜感激。

Here is my current script for adding the filename to the image. 这是我当前用于将文件名添加到图像的脚本。 I am not sure if I can edit it or if I will need to write a new script to edit the text layer. 我不确定是否可以编辑它,或者是否需要编写一个新脚本来编辑文本层。 Thank you for your help! 谢谢您的帮助!

//Check if a document is open
if ( documents.length > 0 )
{
var originalRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PERCENT;

try
{
    var docRef = activeDocument;

    // Create a text layer at the front

    var myLayerRef = docRef.artLayers.add();
    myLayerRef.kind = LayerKind.TEXT;
    myLayerRef.name = "Filename";
    var myTextRef = myLayerRef.textItem;

    //Set your parameters below this line

    //If you wish to show the file extension, change the n to y in the line below, if not use n.
    var ShowExtension = "n";
    // Insert any text to appear before the filename, such as your name and copyright info     between the quotes. 
    //If you do not want extra text, delete between the quotes (but leave the quotes in).
    var TextBefore = "";

    // Insert any text to appear after the filename between the quotes. 
    //If you do not want extra text, delete between the quotes (but leave the quotes in).
    var TextAfter = "";

    // Set font size in Points
    myTextRef.size = 30;

    //Set font - use GetFontName.js to get exact name
    myTextRef.font = "Times New Roman";

    //Set text colour in RGB values
    var newColor = new SolidColor();
newColor.rgb.red = 0;
newColor.rgb.green = 0;
newColor.rgb.blue = 0;
myTextRef.color = newColor;

    // Set the position of the text - percentages from left first, then from top.
    myTextRef.position = new Array( 75, 98);

    // Set the Blend Mode of the Text Layer. The name must be in CAPITALS - ie change NORMAL to DIFFERENCE.
    myLayerRef.blendMode = BlendMode.NORMAL;

    // select opacity in percentage
    myLayerRef.opacity = 100;

// The following code strips the extension and writes tha text layer. fname = file name only            

di=(docRef.name).indexOf(".");      
fname = (docRef.name).substr(0, di);
//use extension if set
if ( ShowExtension == "y" )
{
fname = docRef.name
}  


    myTextRef.contents = TextBefore + "  " + fname +  "  " + TextAfter;


}
catch( e )
{
    // An error occurred. Restore ruler units, then propagate the error back
    // to the user
    preferences.rulerUnits = originalRulerUnits;
    throw e;
}

// Everything went Ok. Restore ruler units
preferences.rulerUnits = originalRulerUnits;
}
else
{
alert( "You must have a document open to add the filename!" );
}

You can use a regular expression to remove all the whitespace and replace them with underscores. 您可以使用正则表达式删除所有空格并将其替换为下划线。 As far as I understand you can do a literal replace for " FSH E" to an empty string first. 据我了解,您可以先用文字替换“ FSH E”为空字符串。 If those letters are different then you'll have to use a different tactic. 如果这些字母不同,那么您将不得不使用不同的策略。 But this will work for now. 但这现在将起作用。 This is the basic part of the code that you need. 这是您需要的代码的基本部分。

var myFileName = "UCMC_0018015 D FSH E";

// remove " FSH E" 
myFileName = myFileName.replace(" FSH E", "");

// replace whitespce with underscores
myFileName = myFileName.replace(/\s/gi, "_");

alert(myFileName);

Your final code should look like this 您的最终代码应如下所示

//Check if a document is open
if ( documents.length > 0 )
{
var originalRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PERCENT;

try
{
    var docRef = activeDocument;

    // Create a text layer at the front

    var myLayerRef = docRef.artLayers.add();
    myLayerRef.kind = LayerKind.TEXT;
    myLayerRef.name = "Filename";
    var myTextRef = myLayerRef.textItem;

    //Set your parameters below this line

    //If you wish to show the file extension, change the n to y in the line below, if not use n.
    var ShowExtension = false;
    // Insert any text to appear before the filename, such as your name and copyright info     between the quotes. 
    //If you do not want extra text, delete between the quotes (but leave the quotes in).
    var TextBefore = "";

    // Insert any text to appear after the filename between the quotes. 
    //If you do not want extra text, delete between the quotes (but leave the quotes in).
    var TextAfter = "";

    // Set font size in Points
    myTextRef.size = 30;

    //Set font - use GetFontName.js to get exact name
    myTextRef.font = "Times New Roman";

    //Set text colour in RGB values
    var newColor = new SolidColor();
newColor.rgb.red = 0;
newColor.rgb.green = 0;
newColor.rgb.blue = 0;
myTextRef.color = newColor;

    // Set the position of the text - percentages from left first, then from top.
    myTextRef.position = new Array( 75, 98);

    // Set the Blend Mode of the Text Layer. The name must be in CAPITALS - ie change NORMAL to DIFFERENCE.
    myLayerRef.blendMode = BlendMode.NORMAL;

    // select opacity in percentage
    myLayerRef.opacity = 100;

// The following code strips the extension and writes tha text layer. fname = file name only            

var fname = docRef.name;

// code changes here.
// remove " FSH E" 
fname = fname.replace(" FSH E", "");

// replace whitespaces with underscores
fname = fname.replace(/\s/gi, "_");

//use extension if set
if ( ShowExtension == true )
{
    di =(fname).lastIndexOf(".");      
    fname = (fname).substr(0, di);
}  

    myTextRef.contents = TextBefore + "  " + fname +  "  " + TextAfter;


}
catch( e )
{
    // An error occurred. Restore ruler units, then propagate the error back
    // to the user
    preferences.rulerUnits = originalRulerUnits;
    throw e;
}

// Everything went Ok. Restore ruler units
preferences.rulerUnits = originalRulerUnits;
}
else
{
alert( "You must have a document open to add the filename!" );
}
  • As your new to JavasScript, I'd like to point out that you have the showextension variable as a string. 作为您JavasScript的新手,我想指出一下,您具有showextension变量作为字符串。 It's probably easier for it to be a Boolean. 使其成为布尔值可能会更容易。 So it can only ever be TRUE or FALSE. 因此,它只能是TRUE或FALSE。 A string could be, well... anything. 一个字符串可能是,好吧...任何东西。
  • Another point is you had indexOf to find the extension. 另一点是您拥有indexOf来查找扩展名。 which will work fine. 这将正常工作。 Unless you have a filename such as "my.lovely.photo.jpg"; 除非您有一个文件名,例如“ my.lovely.photo.jpg”; whereas your extension would be "lovely.photo.jpg" Using lastIndexOf, as you might guess, finds the index of the item near the end of the string. 而您的扩展名将是“ lovely.photo.jpg”,您可能会猜到,使用lastIndexOf可以找到字符串结尾附近的项目的索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 CEP/JavaScript 在 Photoshop 中使用按钮放大图层 - Zoom on a layer in Photoshop with a button using CEP/JavaScript Photoshop JavaScript图层创建 - Photoshop JavaScript Layer creation 使用Adobe Photoshop中的JavaScript脚本,如何更改所选文本层的内容 - Using JavaScript scripting in Adobe Photoshop, how to change the contents of the selected text layer Javascript Photoshop:用于更改现有文本图层的文本颜色的语法 - Javascript Photoshop: Syntax for changing text color of an already existing text layer Photoshop Javascript-尝试创建新的文本层时出现语法错误 - Photoshop Javascript - syntax error when trying to create new text layer 如何使用 CEP/ExtendScript/JavaScript 在 Photoshop 上关闭图层文件夹? - How to close a layer folder on Photoshop using CEP/ExtendScript/JavaScript? 如何在Photoshop中使用相对坐标使用JavaScript移动图层 - How to use JavaScript move layer in Photoshop using the relative coordinates 使用 cep/Javascript 在 Photoshop 中的新图层上绘制圆圈 - Draw circle on a new layer in Photoshop using cep/Javascript Photoshop JavaScript频道内容 - photoshop javascript channel content Photoshop Javascript 到 go 到下一层(上一层) - Photoshop Javascript to go to the next layer (layer above)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM