使用Javascript在Photoshop中编辑文本层的内容
我正在尝试编写一个脚本来编辑PhotoshopCS6中文本层的内容。可能吗 我有大约2000张图片需要为一个工作项目处理。首先,我使用javascript在Photoshop中添加每个图像的文件名作为文本层,我已经在下面看到了。示例文件名是UCMC_0018015 D FSH E。我的脚本成功地将此文件名作为Photoshop中的文本层添加到图像中 但是,我想编辑文本层,以便用空格替换下划线,并从文本字符串的末尾删除FSH E所有文件名都有这些元素,但文件名中的数字因文件而异。有人能帮我写剧本吗?我不熟悉编写和运行脚本,但我正在尽我最大的努力在工作中学习。如果您能给我任何建议,我将不胜感激 这是我当前用于将文件名添加到图像的脚本。我不确定是否可以编辑它,或者是否需要编写新脚本来编辑文本层。谢谢你的帮助使用Javascript在Photoshop中编辑文本层的内容,javascript,photoshop,photoshop-script,Javascript,Photoshop,Photoshop Script,我正在尝试编写一个脚本来编辑PhotoshopCS6中文本层的内容。可能吗 我有大约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.lovely.photo.jpg;而您的扩展名应该是可爱的.photo.jpg,正如您可能猜到的那样,使用lastIndexOf可以在字符串末尾附近找到该项的索引。
试着实现全部功能,告诉我们会发生什么。非常感谢你,食尸鬼傻瓜。我尝试将此代码放到脚本中,但不管文件名如何,它都会给我脚本警报UCMC_0018015_D。我不确定我是否添加了错误的代码,我对javascript非常陌生!很抱歉或者如果访问Photoshop文本层需要额外的编码。有什么想法吗?下面是我的完整脚本,其中添加了您的代码。谢谢你的帮助。哎呀,我没意识到评论中的字符数量有限。我不会重新发布我的脚本,我只会告诉您,我在myLayerRef.opacity=100之后立即将代码按原样插入到我的脚本中;在//之前,下面的代码剥离扩展并写入文本层。fname=仅文件名我已经修改了您的代码,因此它现在可以与文件名一起使用。