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

时间:2014-10-14 18:37:48

标签: javascript photoshop photoshop-script

我正在尝试编写一个脚本来编辑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!" );
}

1 个答案:

答案 0 :(得分:0)

您可以使用正则表达式删除所有空格并用下划线替换它们。据我所知,你可以先将“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变量作为字符串。它可能更容易成为布尔值。所以它只能是TRUE或FALSE。字符串可以是......好吧......。
  • 另一点是你有indexOf来查找扩展名。这将工作正常。除非你有一个文件名,如“my.lovely.photo.jpg”;而你的扩展名是“lovely.photo.jpg”正如你猜测的那样使用lastIndexOf,找到字符串末尾附近项目的索引。
相关问题