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

Editing Content of a Text Layer in Photoshop Using Javascript

本文关键字:文本 编辑 Javascript Photoshop 使用      更新时间:2023-09-26

我正在尝试编写一个脚本来编辑Photoshop CS6中文本层的内容。这可能吗?

我有大约2000个图像,我需要处理一个工作项目。首先,我使用我已经拥有的javascript在Photoshop中添加每个图像的文件名作为文本层(见下文)。示例文件名为"UCMC_0018015 D FSH E."。我的脚本成功地将此文件名作为Photoshop中的文本层添加到图像中。

但是,我想编辑文本层,用空格替换下划线,并从文本字符串的末尾删除"FSH E"(所有文件名都有这些元素,但文件名中的数字因文件而异)。有人能帮我写剧本吗?我刚开始写和运行脚本,但我正在尽我最大的努力在工作中学习。你能给我任何建议都将不胜感激。

这是我当前用于将文件名添加到图像的脚本。我不确定我是否可以编辑它,或者是否需要编写一个新的脚本来编辑文本层。谢谢你的帮助!

//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!" );
}

您可以使用正则表达式删除所有空白并用下划线替换它们。据我所知,你可以先把"FSH E"替换成一个空字符串。如果这些字母不同,那么你将不得不使用不同的策略。但这将在目前起作用。这是您需要的代码的基本部分。

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);

你的最终代码应该像这个

//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!" );
}
  • 作为JavasScript的新手,我想指出的是,您有一个字符串形式的showextension变量。它可能更容易成为布尔值。所以它只能是真或假。一根绳子可以,嗯。。。任何东西
  • 另一点是,您使用indexOf来查找扩展。这会很好用的。除非你有一个文件名,比如"my.可爱.照片.jpg";而您的扩展名是"可爱的.photo.jpg"。您可能会猜测,使用lastIndexOf可以在字符串末尾附近找到项目的索引