使用swatch名称在Photoshop中删除颜色样本

时间:2014-01-22 09:23:29

标签: javascript photoshop photoshop-script

我知道可以使用脚本中的索引删除photoshop中的色样,它看起来像这样:

function DeleteSwatch( index )
{
    var idDlt = charIDToTypeID( "Dlt " );
    var desc11 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
    var ref5 = new ActionReference();
    var idClrs = charIDToTypeID( "Clrs" );
    ref5.putIndex( idClrs, index );
    desc11.putReference( idnull, ref5 );
    executeAction( idDlt, desc11, DialogModes.NO );
}

然而,我想知道是否有人知道使用颜色样本名称而不是索引的方法吗?

1 个答案:

答案 0 :(得分:0)

我很幸运并想通了,所以分享它。

function DeleteSwatch( name )
{
    var ref = new ActionReference();
    ref.putEnumerated( charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
    var desc = executeActionGet(ref);// get the app descriptor
    var presetsList = desc.getList(stringIDToTypeID('presetManager'));// the presets list
    var swatchDesc = presetsList.getObjectValue(1);// swatches is the second key
    var nameList = swatchDesc.getList(charIDToTypeID("Nm  "));// there is only one key in the swatch descriptor so get the list
    var nameOfFirstSwatch = nameList.getString(0);// all the name list keys are strings data types so get the first swatch name

    var indexToDelete = -1;

    for (var i = 0; i <  nameList.count; i++) 
    {
        if(nameList.getString(i) === name)
        {
          indexToDelete = i + 1;
          break;
        }
    }

    if(indexToDelete != -1)
    {
        var idDlt = charIDToTypeID( "Dlt " );
        var ad1 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
        var ref5 = new ActionReference();
        var idClrs = charIDToTypeID( "Clrs" );
        ref5.putIndex( idClrs, indexToDelete );
        ad1.putReference( idnull, ref5 );
        executeAction( idDlt, ad1, DialogModes.NO );
    }
}