OM Wiki

Technical Artist Wiki

ユーザ用ツール

サイト用ツール


サイドバー

adobe:adobe_photoshop:export_photoshop_layers

レイヤーを出力するスクリプト

作成中。

スクリプト

#target Photoshop;
app.bringToFront();
 
try {
    // Config Settings
    var scriptName = "DEV_OM_PSD_to_PNG_Exporter";
    var appVer = app.version;
    var docRef = app.activeDocument;
    var docRefName = docRef.name;
    var exportPath = docRef.path + '\\' + 'images';
    var docName = decodeURI(docRef.name);
    var parentObj = {};
    var e = '';
 
    var win = "window {  \
        orientation: 'column', \
        alignChildren: ['fill', 'top'], \
        text: 'Script UI', \
        margins:15, \
        \
        group1: Group{ \
            cd: StaticText { text:'Exporting Layers...', preferredSize: [250, 15]}, \
            }, \
        }\
    }"
 
    win = new Window(win);
    win.show();
 
    var dupDoc = docRef.duplicate();
 
    selectAllLayers();
    hideLayers();
    traverseLayersAMFlat(dupDoc);
 
    for (key in parentObj) {
        var savedState2 = dupDoc.activeHistoryState;
        if (parentObj[key].layerID) {
            _selectLayerById(parentObj[key].layerID[0]);
            var activeLayer = dupDoc.activeLayer;
            activeLayer.visible = true;
        }
 
        try {
            _selectLayerById(parentObj[key].cropLayerID[0]);
            var activeLayer = dupDoc.activeLayer;
            var bounds = getLayerBounds();
            crop(bounds);
        } catch (e) {;
        }
 
        for (key2 in parentObj[key].layerIDs) {
            var folderPath = parentObj[key].parentPath[0];
            folderPath = exportPath + '\\' + folderPath;
            var folderObj = new Folder(folderPath);
 
            if (!folderObj.exists) {
                folderObj.create();
            }
 
            if (folderObj.exists) {
                _selectLayerById(parentObj[key].layerIDs[key2])
                var activeLayer = dupDoc.activeLayer;
                activeLayer.visible = true;
 
                var file = parentObj[key].layerNames[key2];
                file = new File(folderPath + '\\' + file + ".png");
 
                if (parseInt(appVer) >= 12) {
                    exportPng24AM(file); //CS6
                } else {
                    exportPng24CS5(file); //CS5 is 12.0.1.
                }
 
                activeLayer.visible = false;
            } else {
                alert('出力先がありません');
                break;
            }
        }
        dupDoc.activeHistoryState = savedState2;
    }
 
} catch (e) {    
    var e = 'err';
    alert("ERR_EXPORT");
 
} finally {
    if (dupDoc) {
        var desc = new ActionDescriptor();
        desc.putEnumerated( cTID( "Svng" ), cTID( "YsN " ), cTID( "N   " ) );
        executeAction( cTID( "Cls " ), desc, DialogModes.NO );
    }
 
    if (win) {
        win.close();
    }
 
    if(e !== 'err') {
        alert("Done", 'Script UI');
    }
}
 
//////////////////////////////////////////////////////////////////////////////////////////////////////
 
function _selectLayerById(idx) {    //select just this layer
      var ref = new ActionReference();
      ref.putIdentifier(cTID('Lyr '), idx);
      var desc = new ActionDescriptor();
      desc.putReference(cTID('null'), ref);
      desc.putBoolean(cTID('MkVs'), false);
      executeAction(cTID("slct"), desc, DialogModes.NO);
};
 
function getActiveLayerId() {
    var ref = new ActionReference();
    ref.putProperty(cTID("Prpr"), cTID("LyrI"));
    ref.putEnumerated(cTID("Lyr "), cTID("Ordn"), cTID("Trgt"));
    return executeActionGet(ref).getInteger(sTID("layerID"));
};
 
function getLayerNameByIndex( idx ) {   
    var ref = new ActionReference();   
    ref.putProperty( cTID("Prpr") , cTID( "Nm  " ));   
    ref.putIndex( cTID( "Lyr " ), idx );  
    return executeActionGet(ref).getString(cTID( "Nm  " ));
};
 
//non-recursive action manager traversal function
function traverseLayersAMFlat(doc, ftn) {
    //how many layers are there in this document?
    var ref = new ActionReference();
    ref.putEnumerated(cTID('Dcmn'), cTID('Ordn'), cTID('Trgt'));
    var count = executeActionGet(ref).getInteger(cTID('NmbL'));
 
    //traverse the list backwards (does parents first)
    for (var i = count; i >= 1; i--) {
        ref = new ActionReference();
        ref.putIndex(cTID('Lyr '), i);
        var desc = executeActionGet(ref); //access layer index #i
        var layerID = desc.getInteger(sTID('layerID')); //ID for selecting by ID #
        var layerSection = typeIDToStringID(desc.getEnumerationValue(sTID('layerSection')));
 
        if (layerSection != 'layerSectionEnd') { //do this layer
            _selectLayerById(layerID);
            var layer = dupDoc.activeLayer;
            var layerName = getLayerNameByIndex(i).replace(/[\\\*\/\?:"\|<> ]/g, '_');
            var parentPath = "";            
 
            if (layerSection == 'layerSectionStart') {
                parentObj[layerID] = {};
                parentObj[layerID].layerID = [];
                parentObj[layerID].layerNames = [];
                parentObj[layerID].layerIDs = [];                
                parentObj[layerID].parentPath = [];
                parentObj[layerID].cropLayerID = [];
                parentPath = layerName;
 
                function getParent(layer) {
                    var layerParentName = layer.parent.name; ////////////////////
                    if (layerParentName !== dupDoc.name) {
                        parentPath = layerParentName + '\\' + parentPath;
                        getParent(layer.parent);
                    }
                };
 
                getParent(layer);
                parentObj[layerID].layerID.push(layerID);
                parentObj[layerID].parentPath.push(parentPath);
 
            } else {             
                try {
                    dupDoc.activeLayer = layer.parent; ////////////////////////
                    var layerParentID = getActiveLayerId();
 
                    if (parentObj[layerParentID]) {
                        if (layerName.indexOf('#Crop_') != -1) {
                            parentObj[layerParentID].cropLayerID.push(layerID);
                        } else {
                            parentObj[layerParentID].layerNames.push(layerName);
                            parentObj[layerParentID].layerIDs.push(layerID);
                        }
                    } else {
                        alert("レイヤーグループオブジェクトが取得できませんでした。");
                    }
                } catch(e) {
                    if (!parentObj['root']){
                        parentObj['root'] = {};
                        parentObj['root'].layerNames = [];
                        parentObj['root'].layerIDs = [];
                        parentObj['root'].parentPath = [];
                        parentObj['root'].cropLayerID = [];
                    }
 
                    if (layerName.indexOf('#Crop_') != -1) {
                        parentObj['root'].cropLayerID.push(layerID);
                    } else {
                        parentObj['root'].layerIDs.push(layerID);
                        parentObj['root'].layerNames.push(layerName);
                    }                                        
                    parentObj['root'].parentPath.push('');
                }
            }
        }
    } //for i-- countdown
 
    try { //if there is a magic background layer, process it, too
        app.activeDocument.activeLayer = app.activeDocument.backgroundLayer;
        ftn(doc, app.activeDocument.backgroundLayer);
    } catch (e) {;
    }
};
 
function exportPsdAM(fileName) {
    var desc = new ActionDescriptor();
    var desc2 = new ActionDescriptor();
    desc2.putBoolean( sTID( "maximizeCompatibility" ), true );
    desc.putObject( cTID( "As  " ), cTID( "Pht3" ), desc2 );
    desc.putPath( cTID( "In  " ), new File(fileName) );
    desc.putInteger( cTID( "DocI" ), 39 );
    desc.putBoolean( cTID( "Cpy " ), false );
    desc.putBoolean( cTID( "Lyrs" ), false );
    desc.putEnumerated( sTID( "saveStage" ), sTID( "saveStageType" ), sTID( "saveSucceeded" ) );
    executeAction( cTID( "save" ), desc, DialogModes.NO );
};
 
function exportPng24CS5(fileName) {
    var saveOption = new PNGSaveOptions();
    saveOption.interlaced = false;
    docRef.saveAs(fileName, saveOption, true, Extension.LOWERCASE);
}
 
function exportPng24AM(fileName) {
    var desc = new ActionDescriptor();
    var desc2 = new ActionDescriptor();
	desc2.putEnumerated(cTID("Op  "), cTID("SWOp"), cTID("OpSa"));
	desc2.putEnumerated(cTID("Fmt "), cTID("IRFm"), cTID("PN24"));
	desc2.putBoolean(cTID("Intr"), false);
	desc2.putBoolean(cTID("Trns"), true);
	desc2.putBoolean(cTID("Mtt "), true);
	desc2.putInteger(cTID("MttR"), 0);
	desc2.putInteger(cTID("MttG"), 0);
	desc2.putInteger(cTID("MttB"), 0);
	desc2.putBoolean(cTID("SHTM"), false);
	desc2.putBoolean(cTID("SImg"), true);
	desc2.putBoolean(cTID("SSSO"), false);
	desc2.putList(cTID("SSLt"), new ActionList());
	desc2.putBoolean(cTID("DIDr"), false);
	desc2.putPath(cTID("In  "), new File(fileName));
	desc.putObject(cTID("Usng"), sTID("SaveForWeb"), desc2);
	app.executeAction(cTID("Expr"), desc, DialogModes.NO);
}; 
 
function crop(bounds) {
    var desc = new ActionDescriptor();
    var desc2 = new ActionDescriptor();
    desc2.putUnitDouble(cTID("Top "), cTID("#Pxl"), bounds.y1);
    desc2.putUnitDouble(cTID("Left"), cTID("#Pxl"), bounds.x1);
    desc2.putUnitDouble(cTID("Btom"), cTID("#Pxl"), bounds.y2);
    desc2.putUnitDouble(cTID("Rght"), cTID("#Pxl"), bounds.x2);
    desc.putObject(cTID("T   "), cTID("Rctn"), desc2);
    desc.putUnitDouble(cTID("Angl"), cTID("#Ang"), 0.000000);
    desc.putBoolean(cTID("Dlt "), false);
    desc.putEnumerated(sTID("cropAspectRatioModeKey"), sTID("cropAspectRatioModeClass"), sTID("targetSize"));
    desc.putUnitDouble(cTID("Wdth"), cTID("#Pxl"), 0);
    desc.putUnitDouble(cTID("Hght"), cTID("#Pxl"), 0);
    desc.putUnitDouble(cTID("Rslt"), cTID("#Rsl"), 0);
    executeAction(cTID("Crop"), desc, DialogModes.NO);
};
 
function getLayerBounds() {
    var ref = new ActionReference();
    ref.putEnumerated(cTID("Lyr "), cTID("Ordn"), cTID("Trgt"));
    var desc = executeActionGet(ref).getObjectValue(sTID("bounds"));
    var bounds = {};
    bounds.x1 = desc.getUnitDoubleValue(sTID("left"));
    bounds.y1 = desc.getUnitDoubleValue(sTID("top"));
    bounds.x2 = desc.getUnitDoubleValue(sTID("right"));
    bounds.y2 = desc.getUnitDoubleValue(sTID("bottom"));
    bounds.w = bounds.x2 - bounds.x1;
    bounds.h = bounds.y2 - bounds.y1;
    return bounds;
};
 
function selectAllLayers() {
	var ref = new ActionReference();
	ref.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt'));
	var desc = new ActionDescriptor();
	desc.putReference(cTID('null'), ref);
	executeAction(sTID('selectAllLayers'), desc, DialogModes.NO);
};
 
function hideLayers() {
	var ref = new ActionReference();
	ref.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt'));
	var list = new ActionList();
	list.putReference(ref);
	var desc = new ActionDescriptor();
	desc.putList(cTID('null'), list);
	executeAction(cTID('Hd  '), desc, DialogModes.NO);
};
 
function cTID(s) {return app.charIDToTypeID(s);}
function sTID(s) {return app.stringIDToTypeID(s);}
adobe/adobe_photoshop/export_photoshop_layers.txt · 最終更新: 2021/05/03 19:17 by ochiaimitsuo