在Photoshop中打开的多个图像会根据文件名保存为Web不同的文件大小

时间:2018-08-28 20:02:08

标签: javascript batch-processing photoshop photoshop-script

我在photoshop中一次打开了几张图像,但是需要使用文件名对其进行相应检查并相应地保存下来。该脚本似乎与一个图像一起运行,但是打开的其他图像不起作用。谢谢!

问题1:添加的边框必须为黑色而非白色。

问题2:需要处理所有打开的图像。当前仅在打开第一个图像而不是其余图像的情况下工作。

这是脚本的作用

Name.SizexSize.png->添加1px边框-> JPG 59kb->保存为网络+无提示。 Name.SizexSize.png->添加1px边框-> JPG 39kb->保存为网络+无提示。覆盖原稿。

for (var i = 0; i < documents.length; i++) {
activeDocument = documents[i]; //will change active document to one of 
the opened

var docNameSize = activeDocument.name.split(".")[1].split("x"); //will 
get you [Size, Size] from Name.SizexSize.png;
for (var k = 0; k < docNameSize.length; k++) docNameSize[k] = 
parseInt(docNameSize[k]); //will convert [Size, Size] to numbers
}


 for (var i = 0; i < documents.length; i++) {
 activeDocument = documents[i]; //will change active document to one 
 of the opened

 var docNameSize = activeDocument.name.split(".")[1].split("x"); 
 //will get you [Size, Size] from Name.SizexSize.png;
 for (var k = 0; k < docNameSize.length; k++) docNameSize[k] = 
 parseInt(docNameSize[k]); //will convert [Size, Size] to numbers
 }

 selectAll()
 contractSelection(1)
 invertSelection()
 fillWithWhiteColor()
 deselect()

 function cTID(s)
 {
  return app.charIDToTypeID(s);
 };

 function sTID(s)
 {
 return app.stringIDToTypeID(s);
 };

 function selectAll()
 {
 var desc26 = new ActionDescriptor();
 var ref2 = new ActionReference();
 ref2.putProperty(cTID('Chnl'), cTID('fsel')); 
 desc26.putReference(cTID('null'), ref2);
 desc26.putEnumerated(cTID('T   '), cTID('Ordn'), cTID('Al  '));
 executeAction(cTID('setd'), desc26, DialogModes.NO);
 }

 function contractSelection(_value)
 {
var desc30 = new ActionDescriptor();
desc30.putUnitDouble(cTID('By  '), cTID('#Pxl'), _value);
desc30.putBoolean(sTID('selectionModifyEffectAtCanvasBounds'), true);
executeAction(cTID('Cntc'), desc30, DialogModes.NO);
};

function invertSelection()
{
executeAction(cTID('Invs'), undefined, DialogModes.NO);
};

function fillWithWhiteColor()
{
var desc35 = new ActionDescriptor();
desc35.putEnumerated(cTID('Usng'), cTID('FlCn'), cTID('Clr '));
var desc36 = new ActionDescriptor();
desc36.putUnitDouble(cTID('H   '), cTID('#Ang'), 0.000000);
desc36.putDouble(cTID('Strt'), 0.000000);
desc36.putDouble(cTID('Brgh'), 100.000000);
desc35.putObject(cTID('Clr '), cTID('HSBC'), desc36);
desc35.putUnitDouble(cTID('Opct'), cTID('#Prc'), 100.000000);
desc35.putEnumerated(cTID('Md  '), cTID('BlnM'), cTID('Nrml'));
executeAction(cTID('Fl  '), desc35, DialogModes.NO);
};

function deselect()
{
var desc38 = new ActionDescriptor();
var ref3 = new ActionReference();
ref3.putProperty(cTID('Chnl'), cTID('fsel'));
desc38.putReference(cTID('null'), ref3);
desc38.putEnumerated(cTID('T   '), cTID('Ordn'), cTID('None'));
executeAction(cTID('setd'), desc38, DialogModes.NO);
};
saveJPG(
{
  path: activeDocument.path,
  maxSize: 50 //size in kbs
 })

 function saveJPG(_data)
 {
 if (_data.path == undefined) return false;
_data.name = _data.name == undefined ? activeDocument.name : _data.name;
_data.quality == undefined && _data.quality = 75;

if (!new Folder(_data.path).exists)
{
    alert("Output path doesn't exist!"); //you can add a function to create a path if needed
    return false
}

var options = new ExportOptionsSaveForWeb(),
    jpgFile = new File(_data.path + '/' + getName(_data.name) + '.jpg');

options.format = SaveDocumentType.JPEG;
options.quality = _data.quality;
activeDocument.exportDocument(jpgFile, ExportType.SAVEFORWEB, options);

if (_data.maxSize != undefined)
{
    var ms = _data.maxSize * 1000;
    if (jpgFile.length > ms)
    {
        if (!jpgFile.remove())
        {
            alert('Save file is locked, please make sure it\'s not opened anywhere');
            return
        };
        saveJPG(
        {
            path: _data.path,
            name: _data.name,
            maxSize: _data.maxSize,
            quality: _data.quality - 2
        });
    }
};

function getName(fullName)
{
    var temp = String(fullName).split("/"),
        fullName = temp.pop();
    return fullName.replace(/\.[^.]+$/g, "")
};
};

1 个答案:

答案 0 :(得分:0)

您正在遍历文档以获取其名称,但是随后您执行了所有仅一次创建边框的例程。所有这些selectAll()contractSelection()和其他都应该在循环中。如果您不熟悉Javascript,建议您参加一门关于codeacademy的课程:我免费,不会花很多时间。

要将颜色更改为黑色,请将此行desc36.putDouble(cTID('Brgh'), 100.000000);替换为desc36.putDouble(cTID('Brgh'), 0);