Photoshop Script Listener可访问文件夹中的所有文件

时间:2014-04-03 06:47:13

标签: javascript actionscript photoshop

我在Photoshop cc中使用ScriptListener插件创建了一个动作脚本。我不懂javascript。你能帮我写一些代码,这将帮助我访问源文件夹中的所有文件,然后应用photoshop过滤器将它们保存在文件夹中,并附上其名称。 附:抱歉我的英文。

// =======================================================
var idOpn = charIDToTypeID( "Opn " );
var desc1 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
desc1.putPath( idnull, new File( "C:\\Users\\Данила\\Documents\\PhotoBook\\DSCF1698.JPG"    ) );
executeAction( idOpn, desc1, DialogModes.NO );

// =======================================================
var idCnvM = charIDToTypeID( "CnvM" );
var desc2 = new ActionDescriptor();
var idT = charIDToTypeID( "T   " );
var idGrys = charIDToTypeID( "Grys" );
desc2.putClass( idT, idGrys );
executeAction( idCnvM, desc2, DialogModes.NO );

// =======================================================
var idHghP = charIDToTypeID( "HghP" );
var desc3 = new ActionDescriptor();
var idRds = charIDToTypeID( "Rds " );
var idPxl = charIDToTypeID( "#Pxl" );
desc3.putUnitDouble( idRds, idPxl, 9.200000 );
executeAction( idHghP, desc3, DialogModes.NO );

// =======================================================
var idLvls = charIDToTypeID( "Lvls" );
var desc4 = new ActionDescriptor();
var idpresetKind = stringIDToTypeID( "presetKind" );
var idpresetKindType = stringIDToTypeID( "presetKindType" );
var idpresetKindCustom = stringIDToTypeID( "presetKindCustom" );
desc4.putEnumerated( idpresetKind, idpresetKindType, idpresetKindCustom );
var idAdjs = charIDToTypeID( "Adjs" );
    var list1 = new ActionList();
        var desc5 = new ActionDescriptor();
        var idChnl = charIDToTypeID( "Chnl" );
            var ref1 = new ActionReference();
            var idChnl = charIDToTypeID( "Chnl" );
            var idOrdn = charIDToTypeID( "Ordn" );
            var idTrgt = charIDToTypeID( "Trgt" );
            ref1.putEnumerated( idChnl, idOrdn, idTrgt );
        desc5.putReference( idChnl, ref1 );
        var idInpt = charIDToTypeID( "Inpt" );
            var list2 = new ActionList();
            list2.putInteger( 100 );
            list2.putInteger( 125 );
        desc5.putList( idInpt, list2 );
        var idGmm = charIDToTypeID( "Gmm " );
        desc5.putDouble( idGmm, 0.800000 );
    var idLvlA = charIDToTypeID( "LvlA" );
    list1.putObject( idLvlA, desc5 );
desc4.putList( idAdjs, list1 );
executeAction( idLvls, desc4, DialogModes.NO );

// =======================================================
var idslct = charIDToTypeID( "slct" );
var desc6 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
    var ref2 = new ActionReference();
    var idmoveTool = stringIDToTypeID( "moveTool" );
    ref2.putClass( idmoveTool );
desc6.putReference( idnull, ref2 );
var iddontRecord = stringIDToTypeID( "dontRecord" );
desc6.putBoolean( iddontRecord, true );
var idforceNotify = stringIDToTypeID( "forceNotify" );
desc6.putBoolean( idforceNotify, true );
executeAction( idslct, desc6, DialogModes.NO );

// =======================================================
var idsave = charIDToTypeID( "save" );
var desc7 = new ActionDescriptor();
var idAs = charIDToTypeID( "As  " );
    var desc8 = new ActionDescriptor();
    var idEQlt = charIDToTypeID( "EQlt" );
    desc8.putInteger( idEQlt, 12 );
    var idMttC = charIDToTypeID( "MttC" );
    var idMttC = charIDToTypeID( "MttC" );
    var idNone = charIDToTypeID( "None" );
    desc8.putEnumerated( idMttC, idMttC, idNone );
var idJPEG = charIDToTypeID( "JPEG" );
desc7.putObject( idAs, idJPEG, desc8 );
var idIn = charIDToTypeID( "In  " );
desc7.putPath( idIn, new File( "C:\\Users\\Данила\\Desktop\\Photobook\\DSCF1698.jpg" ) );
var idDocI = charIDToTypeID( "DocI" );
desc7.putInteger( idDocI, 35 );
var idsaveStage = stringIDToTypeID( "saveStage" );
var idsaveStageType = stringIDToTypeID( "saveStageType" );
var idsaveBegin = stringIDToTypeID( "saveBegin" );
desc7.putEnumerated( idsaveStage, idsaveStageType, idsaveBegin );
executeAction( idsave, desc7, DialogModes.NO );

// =======================================================
var idsave = charIDToTypeID( "save" );
var desc9 = new ActionDescriptor();
var idAs = charIDToTypeID( "As  " );
    var desc10 = new ActionDescriptor();
    var idEQlt = charIDToTypeID( "EQlt" );
    desc10.putInteger( idEQlt, 12 );
    var idMttC = charIDToTypeID( "MttC" );
    var idMttC = charIDToTypeID( "MttC" );
    var idNone = charIDToTypeID( "None" );
    desc10.putEnumerated( idMttC, idMttC, idNone );
var idJPEG = charIDToTypeID( "JPEG" );
desc9.putObject( idAs, idJPEG, desc10 );
var idIn = charIDToTypeID( "In  " );
desc9.putPath( idIn, new File( "C:\\Users\\Данила\\Desktop\\Photobook\\DSCF1698.jpg" ) );
var idDocI = charIDToTypeID( "DocI" );
desc9.putInteger( idDocI, 35 );
var idsaveStage = stringIDToTypeID( "saveStage" );
var idsaveStageType = stringIDToTypeID( "saveStageType" );
var idsaveSucceeded = stringIDToTypeID( "saveSucceeded" );
desc9.putEnumerated( idsaveStage, idsaveStageType, idsaveSucceeded );
executeAction( idsave, desc9, DialogModes.NO );

// =======================================================
var idCls = charIDToTypeID( "Cls " );
executeAction( idCls, undefined, DialogModes.NO );

1 个答案:

答案 0 :(得分:0)

你走了。我在一个名为doS​​tuff的函数中包装了主进程,然后它保存了&关闭文件。注意!它将覆盖现有文件,因此请首先备份源文件!

//pref pixels
app.preferences.rulerUnits = Units.PIXELS;

var myJpgQuality = 12; // jpeg quality

// get all the files to process
var inFolder = Folder.selectDialog("Please select folder to process");
if (inFolder != null)
{
    var fileList = inFolder.getFiles();
}

// look over the first image
var indexFile = fileList[0].toString();
var myDirectoryName = indexFile.substring(0, indexFile.lastIndexOf("/"));

for (var i = 0; i < fileList.length; i++)
{

    try 
    {
      var doc = open(fileList[i]);
    }
    catch(e)
    {
      alert("Failed to open!");
    }

    var myFile = fileList[i].toString();
    var myFileName = myFile.substring(myFile.lastIndexOf("/") + 1, myFile.lastIndexOf("."));
    var myFilePath = myDirectoryName + "/" + myFileName + ".jpg";

    // run the main function here
    doStuff();

    // save
    saveAsJpeg(myFilePath);

    //close without saving
    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}

function saveAsJpeg(apath)
{
    // jpg file options
    var jpgFile = new File(apath);
    jpgSaveOptions = new JPEGSaveOptions();
    jpgSaveOptions.formatOptions = FormatOptions.OPTIMIZEDBASELINE;
    jpgSaveOptions.embedColorProfile = true;
    jpgSaveOptions.matte = MatteType.NONE;
    jpgSaveOptions.quality = myJpgQuality;

    activeDocument.saveAs(jpgFile, jpgSaveOptions, true, Extension.LOWERCASE);
}


function doStuff()
{
    // =======================================================
    var idCnvM = charIDToTypeID( "CnvM" );
    var desc2 = new ActionDescriptor();
    var idT = charIDToTypeID( "T   " );
    var idGrys = charIDToTypeID( "Grys" );
    desc2.putClass( idT, idGrys );
    executeAction( idCnvM, desc2, DialogModes.NO );

    // =======================================================
    var idHghP = charIDToTypeID( "HghP" );
    var desc3 = new ActionDescriptor();
    var idRds = charIDToTypeID( "Rds " );
    var idPxl = charIDToTypeID( "#Pxl" );
    desc3.putUnitDouble( idRds, idPxl, 9.200000 );
    executeAction( idHghP, desc3, DialogModes.NO );

    // =======================================================
    var idLvls = charIDToTypeID( "Lvls" );
    var desc4 = new ActionDescriptor();
    var idpresetKind = stringIDToTypeID( "presetKind" );
    var idpresetKindType = stringIDToTypeID( "presetKindType" );
    var idpresetKindCustom = stringIDToTypeID( "presetKindCustom" );
    desc4.putEnumerated( idpresetKind, idpresetKindType, idpresetKindCustom );
    var idAdjs = charIDToTypeID( "Adjs" );
        var list1 = new ActionList();
            var desc5 = new ActionDescriptor();
            var idChnl = charIDToTypeID( "Chnl" );
                var ref1 = new ActionReference();
                var idChnl = charIDToTypeID( "Chnl" );
                var idOrdn = charIDToTypeID( "Ordn" );
                var idTrgt = charIDToTypeID( "Trgt" );
                ref1.putEnumerated( idChnl, idOrdn, idTrgt );
            desc5.putReference( idChnl, ref1 );
            var idInpt = charIDToTypeID( "Inpt" );
                var list2 = new ActionList();
                list2.putInteger( 100 );
                list2.putInteger( 125 );
            desc5.putList( idInpt, list2 );
            var idGmm = charIDToTypeID( "Gmm " );
            desc5.putDouble( idGmm, 0.800000 );
        var idLvlA = charIDToTypeID( "LvlA" );
        list1.putObject( idLvlA, desc5 );
    desc4.putList( idAdjs, list1 );
    executeAction( idLvls, desc4, DialogModes.NO );

    // =======================================================
    var idslct = charIDToTypeID( "slct" );
    var desc6 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref2 = new ActionReference();
        var idmoveTool = stringIDToTypeID( "moveTool" );
        ref2.putClass( idmoveTool );
    desc6.putReference( idnull, ref2 );
    var iddontRecord = stringIDToTypeID( "dontRecord" );
    desc6.putBoolean( iddontRecord, true );
    var idforceNotify = stringIDToTypeID( "forceNotify" );
    desc6.putBoolean( idforceNotify, true );
    executeAction( idslct, desc6, DialogModes.NO );
}