Photoshop Extendscript:如何var检查当前打开的文件并执行某些文件名的命令

时间:2018-08-22 02:28:27

标签: javascript photoshop extendscript

我在photoshop中一次打开了几张图像,但是需要使用文件名对其进行相应检查并相应地保存下来。谢谢!

示例:

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

var defaultRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;

if ((documents.length > 0) && (activeDocument.saved)){

var AD = activeDocument;
var initialFolder = activeDocument.path;

var docRef = activeDocument;

    var fileNameNoExtension = docRef.name;
    fileNameNoExtension = fileNameNoExtension.split( "." );
    if ( fileNameNoExtension.length > 1 ) {
        fileNameNoExtension.length--;
    }
    fileNameNoExtension = fileNameNoExtension.join(".");

var Filename = fileNameNoExtension; 

  AD.flatten();
  var docWidth = AD.width; 
  var docHeight = AD.height;
  var docRes = AD.resolution;


  var jpgOptns = new JPEGSaveOptions();
  jpgOptns.formatOptions = FormatOptions.OPTIMIZEDBASELINE;
  jpgOptns.embedColorProfile = true;
  jpgOptns.matte = MatteType.NONE;
  jpgOptns = new JPEGSaveOptions;

  for(a=0+1;a<=5;a++){

    jpgSaveFile =  File(initialFolder+"/"+Filename+a+".jpg");
    jpgOptns.quality = 5;
AD.saveAs (jpgSaveFile ,jpgOptns , true, Extension.LOWERCASE);

  }

 while (app.documents.length) {
 app.activeDocument.close()
 }

  if(docWidth>docHeight){
  var multipW=1;
  var multipH=13;
  }else{
  var multipW=13;
  var multipH=1;
  }


 }else{

  alert("Either you have no document open or you haven't saved your 
  work anywhere prior to the script \nSave your document first !");

}

1 个答案:

答案 0 :(得分:0)

要处理打开的文档,请遍历documents对象,以从名称中获取大小,我想是这样的:

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
}

您如何添加1px边框?我想您可以执行以下操作:全选,合同选择,填入白色。

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

并且无法为网络保存指定最大尺寸,但是您可以使用类似的方法:它将以所需的质量(默认为75)保存jpg,并查看尺寸是否小于所需的尺寸。如果更大,它将以较小的递归质量再次保存:

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