Last active
February 25, 2018 15:40
-
-
Save TMPxyz/7a8298f0f22e3d8a4c9b8b8020db681f to your computer and use it in GitHub Desktop.
Automatically export selected Photoshop layers based on the layer names;
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#target photoshop | |
// debug level: 0-2 (0:disable, 1:break on error, 2:break at beginning) | |
// $.level = 0; | |
// debugger; // launch debugger on next line | |
if (app.documents.length <= 0) { | |
if (DialogModes.NO != app.playbackDisplayDialogs) { | |
alert('no document is opened!'); | |
} | |
} | |
else { | |
app.activeDocument.suspendHistory("AutoExportSelectedLayers", "main()"); | |
} | |
/////////////////////////////////////////////////////////////////////////////// | |
// Functions | |
/////////////////////////////////////////////////////////////////////////////// | |
/////////////////////////////////////////////////////////////////////////////// | |
// Function: main | |
// Usage: the core routine for this script | |
// Input: <none> | |
// Return: <none> | |
/////////////////////////////////////////////////////////////////////////////// | |
function main() { | |
try { | |
var docRef = app.activeDocument; | |
var selectedLayers = getSelectedLayers(); | |
if (selectedLayers.length <= 0) { | |
if (DialogModes.NO != app.playbackDisplayDialogs) { | |
alert("no selected layers"); | |
return 'cancel'; // quit, returning 'cancel' (dont localize) makes the actions palette not record our script | |
} | |
} else { | |
var result = ""; | |
setVisibleAll(docRef, false); | |
for (var i in selectedLayers) { | |
var layerOrSet = selectedLayers[i]; | |
result = exportLayer(layerOrSet, result); | |
} | |
setVisibleAll(docRef, true); | |
InfoBox("Result: \n" + result); | |
} | |
} catch (e) { | |
alert(e); | |
} | |
} | |
function InfoBox(str) { | |
dlgMain = new Window("dialog", "Finished"); | |
// match our dialog background color to the host application | |
var brush = dlgMain.graphics.newBrush(dlgMain.graphics.BrushType.THEME_COLOR, "appDialogBackground"); | |
dlgMain.graphics.backgroundColor = brush; | |
dlgMain.graphics.disabledBackgroundColor = dlgMain.graphics.backgroundColor; | |
dlgMain.orientation = 'column'; | |
dlgMain.alignChildren = 'left'; | |
var lines = str.split('\n'); | |
for (var i in lines) { | |
var s = lines[i]; | |
dlgMain.add("statictext", undefined, s); | |
} | |
dlgMain.btnOK = dlgMain.add("button", undefined, "OK"); | |
dlgMain.btnOK.onClick = function () { | |
dlgMain.close(0); | |
} | |
// give the hosting app the focus before showing the dialog | |
app.bringToFront(); | |
dlgMain.center(); | |
dlgMain.show(); | |
} | |
function getSelectedLayers() { | |
var selectedLayers = new Array; | |
var layerIndices = getSelectedLayersIdx(); | |
for (var i in layerIndices) { | |
var layer = selectByIndex(layerIndices[i]); | |
selectedLayers.push(layer); | |
} | |
return selectedLayers; | |
} | |
function getSelectedLayersIdx() { | |
var selectedLayers = new Array; | |
var ref = new ActionReference(); | |
ref.putEnumerated(charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt")); | |
var desc = executeActionGet(ref); | |
if (desc.hasKey(stringIDToTypeID('targetLayers'))) { | |
desc = desc.getList(stringIDToTypeID('targetLayers')); | |
var c = desc.count | |
var selectedLayers = new Array(); | |
for (var i = 0; i < c; i++) { | |
try { | |
activeDocument.backgroundLayer; | |
selectedLayers.push(desc.getReference(i).getIndex()); | |
} catch (e) { | |
selectedLayers.push(desc.getReference(i).getIndex() + 1); | |
} | |
} | |
} else { | |
var ref = new ActionReference(); | |
ref.putProperty(charIDToTypeID("Prpr"), charIDToTypeID("ItmI")); | |
ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt")); | |
try { | |
activeDocument.backgroundLayer; | |
selectedLayers.push(executeActionGet(ref).getInteger(charIDToTypeID("ItmI")) - 1); | |
} catch (e) { | |
selectedLayers.push(executeActionGet(ref).getInteger(charIDToTypeID("ItmI"))); | |
} | |
var vis = app.activeDocument.activeLayer.visible; | |
if (vis == true) app.activeDocument.activeLayer.visible = false; | |
var desc9 = new ActionDescriptor(); | |
var list9 = new ActionList(); | |
var ref9 = new ActionReference(); | |
ref9.putEnumerated(charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt')); | |
list9.putReference(ref9); | |
desc9.putList(charIDToTypeID('null'), list9); | |
executeAction(charIDToTypeID('Shw '), desc9, DialogModes.NO); | |
if (app.activeDocument.activeLayer.visible == false) selectedLayers.shift(); | |
app.activeDocument.activeLayer.visible = vis; | |
} | |
return selectedLayers; | |
} | |
/////////////////////////////////////////////////////////////////////////////// | |
// Function: selectByIndex | |
// Description: Sets the activeLayer by AM itemIndex and returns DOM active layer | |
// Usage: var idx = makeActiveByIndex( 7 ); | |
// Input: Number (single idx) | |
// Return: activeLayer | |
/////////////////////////////////////////////////////////////////////////////// | |
function selectByIndex(idx) { | |
// avoid error when background layer idx is passed | |
if (idx == 0) return; | |
// if an array is passed use only the first element | |
if (idx.length != undefined) { | |
idx = idx[0]; | |
} | |
// execute selection | |
var desc = new ActionDescriptor(); | |
var ref = new ActionReference(); | |
ref.putIndex(charIDToTypeID('Lyr '), idx); | |
desc.putReference(charIDToTypeID('null'), ref); | |
executeAction(charIDToTypeID('slct'), desc, DialogModes.NO); | |
return app.activeDocument.activeLayer; | |
} | |
/////////////////////////////////////////////////////////////////////////////// | |
// Function: exportLayer | |
// Usage: export specified layer | |
// Input: layer reference | |
// Return: none | |
/////////////////////////////////////////////////////////////////////////////// | |
function exportLayer(layerOrSet, result) { | |
try { | |
var docRef = app.activeDocument; | |
layerOrSet.visible = true; | |
//Options to export to PNG files | |
var options = new ExportOptionsSaveForWeb(); | |
options.format = SaveDocumentType.PNG; | |
options.PNG8 = false; //PNG-24 | |
options.transparency = true; | |
options.optimized = true; | |
var basename = docRef.name.slice(0, docRef.name.lastIndexOf('.')); | |
var path = docRef.path + '/export_' + basename; | |
var folder1 = Folder(path); | |
if (!folder1.exists) | |
folder1.create(); | |
var outputPath = path + "/" + layerOrSet.name + '.png'; | |
docRef.exportDocument(File(outputPath), ExportType.SAVEFORWEB, options); | |
layerOrSet.visible = false; | |
result += "OK: " + outputPath + "\n"; | |
return result; | |
} catch (e) { | |
result += "FAIL: " + layerOrSet.name + "\n"; | |
return result; | |
} | |
} | |
function setVisibleAll(obj, flag) { | |
for (var i = 0; i < obj.artLayers.length; i++) { | |
obj.artLayers[i].allLocked = false; | |
obj.artLayers[i].visible = flag; | |
} | |
for (var i = 0; i < obj.layerSets.length; i++) { | |
setVisibleAll(obj.layerSets[i], flag); | |
} | |
} | |
/////////////////////////////////////////////////////////////////////////////// | |
// Function: setInvisibleAllArtLayers | |
// Usage: unlock and make invisible all art layers, recursively | |
// Input: document or layerset | |
// Return: all art layers are unlocked and invisible | |
/////////////////////////////////////////////////////////////////////////////// | |
function setInvisibleAllArtLayers(obj) { | |
for (var i = 0; i < obj.artLayers.length; i++) { | |
obj.artLayers[i].allLocked = false; | |
obj.artLayers[i].visible = false; | |
} | |
for (var i = 0; i < obj.layerSets.length; i++) { | |
setInvisibleAllArtLayers(obj.layerSets[i]); | |
} | |
} | |
function isLayer(obj) { | |
return obj.typename == "ArtLayer"; | |
} | |
function isLayerSet(obj) { | |
return obj.typename == "LayerSet"; | |
} | |
function getSelectedLayersNames() { | |
var s = ""; | |
var sLayers = getSelectedLayersIdx(); | |
var Names = new Array(); | |
for (var a in sLayers) { | |
//Names.push(app.activeDocument.layers[Number(sLayers[a])].name); | |
var idx = Number(sLayers[a]); | |
var name = getLayerNameByIndex(idx); | |
Names.push(name); | |
s += name + " : "; | |
s += idx + "\n"; | |
} | |
alert(Names.join('\n')); | |
alert(s); | |
return Names; | |
} | |
function getLayerNameByIndex(idx) { | |
var ref = new ActionReference(); | |
ref.putIndex(charIDToTypeID("Lyr "), idx); | |
return executeActionGet(ref).getString(charIDToTypeID("Nm ")); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment