[英]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!" );
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.