Created
January 7, 2014 12:37
-
-
Save senki/8298709 to your computer and use it in GitHub Desktop.
creates psd-copies of layersets with dialog to select layers and suffix;
be advised: existing files of identical names will be overwritten without warnings;
This file contains 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
// creates psd-copies of layersets with dialog to select layers and suffix; | |
// be advised: existing files of identical names will be overwritten without warnings; | |
// 2009, pfaffenbichler, use it at your own risk; | |
// mod by Csaba Maulis aka @_senki on 2014-01-07 | |
// Changes: | |
// - Create PSDs instead of PDFs | |
// - Maintan all layers inside the LayerSet, | |
// - but removes all others | |
// based on the work of Ralf Berger (https://blogs.adobe.com/jnack/2007/01/useful_photoshop_layer_handling_scripts.html) | |
// - Added more meiningfull menu name | |
// - Small changes on dialog | |
/* | |
// BEGIN__HARVEST_EXCEPTION_ZSTRING | |
<javascriptresource> | |
<name>$$$/JavaScripts/layerSet2PSDs/Menu=Layer Groups to Photoshop files...</name> | |
<category>layers</category> | |
<enableinfo>true</enableinfo> | |
</javascriptresource> | |
// END__HARVEST_EXCEPTION_ZSTRING | |
*/ | |
#target photoshop | |
if (app.documents.length > 0) { | |
//////////////////////////////////// | |
// get document-path and -title; | |
var myDocument = app.activeDocument; | |
var myDocName = myDocument.name.match(/(.*)\.[^\.]+$/)[1]; | |
var myPath = myDocument.path; | |
var theLayerSets = collectLayerSets(myDocument); | |
////// filter for checking if entry is numeric, thanks to xbytor ////// | |
numberKeystrokeFilter = function() { | |
this.text = this.text.replace(",", ""); | |
this.text = this.text.replace(".", ""); | |
if (this.text.match(/[^\-\.\d]/)) { | |
this.text = this.text.replace(/[^\-\.\d]/g, ""); | |
}; | |
if (this.text == "") { | |
this.text = "0" | |
} | |
}; | |
//////////////////////////////////// | |
// create the dialog; | |
var dlg = new Window('dialog', "Photoshop files from layersets", [500,300,930,840]); | |
//create list for layer-selection; | |
dlg.layerRange = dlg.add('panel', [21,20,279,445], "Select layersets to create PSDs of"); | |
dlg.layerRange.layersList = dlg.layerRange.add('listbox', [11,20,240,405], '', {multiselect: true}); | |
for (var q = 0; q < theLayerSets.length; q++) { | |
dlg.layerRange.layersList.add ("item", theLayerSets[q].name); | |
dlg.layerRange.layersList.items[q].selected = true | |
}; | |
// entry for suffix; | |
dlg.suffix = dlg.add('panel', [290,20,410,85], "Enter suffix"); | |
dlg.suffix.suffixText = dlg.suffix.add('edittext', [11,20,99,40], "", {multiline:false}); | |
// entry for number; | |
dlg.number = dlg.add('panel', [290,100,410,165], "Start with #"); | |
dlg.number.startNumber = dlg.number.add('edittext', [11,20,45,40], "1", {multiline:false}); | |
dlg.number.addNumber = dlg.number.add('checkbox', [55,20,105,40], "add", {multiline:false}); | |
dlg.number.startNumber.onChange = numberKeystrokeFilter; | |
// field to add layer-name; | |
dlg.layerName = dlg.add('panel', [290,180,410,270], "Layer Name"); | |
dlg.layerName.doAddName = dlg.layerName.add('radiobutton', [11,20,120,40], "add it"); | |
dlg.layerName.dontAddName = dlg.layerName.add('radiobutton', [11,45,120,65], "don’t add it"); | |
dlg.layerName.doAddName.value = true; | |
// field to select target-folder; | |
dlg.target = dlg.add('panel', [290,285,410,445], "Target Folder"); | |
dlg.target.targetSel = dlg.target.add('button', [11,20,100,42], "Select"); | |
dlg.target.targetField = dlg.target.add('statictext', [11,50,100,155], String(myPath), {multiline:true}); | |
dlg.target.targetSel.onClick = function () { | |
var target = Folder.selectDialog("select a target folder"); | |
dlg.target.targetField.text = target.fsName | |
}; | |
// ok- and cancel-buttons; | |
dlg.buildBtn = dlg.add('button', [220,460,410,482], 'OK', {name:'ok'}); | |
dlg.cancelBtn = dlg.add('button', [21,460,210,482], 'Cancel', {name:'cancel'}); | |
dlg.warning = dlg.add('statictext', [21,490,410,530], "Be advised: Existing files of the same name will be replaced without prompting", {multiline: true}); | |
dlg.center(); | |
//////////////////////////////////// | |
var myReturn = dlg.show (); | |
// in case of OK; | |
if (myReturn == true && dlg.layerRange.layersList.selection.length > 0) { | |
// get the number instead of the name; | |
var theLayerSelection = new Array; | |
var theColl = dlg.layerRange.layersList.items; | |
for (var p = 0; p < dlg.layerRange.layersList.items.length; p++) { | |
if (dlg.layerRange.layersList.items[p].selected == true) { | |
theLayerSelection = theLayerSelection.concat(p); | |
} | |
}; | |
// collect the rest of the variables, | |
var theSuffix = dlg.suffix.suffixText.text; | |
var theNumber = Number (dlg.number.startNumber.text) - 1; | |
var theLayerNameAdd = dlg.layerName.doAddName.value; | |
var theDestination = dlg.target.targetField.text; | |
var theNumbering = dlg.number.addNumber.value; | |
// PSD options | |
PSDOpts = new PhotoshopSaveOptions(); | |
PSDOpts.alphaChannels = true; | |
PSDOpts.annotations = true; | |
PSDOpts.embedColorProfile = true; | |
PSDOpts.layers = true; | |
PSDOpts.spotColors = true; | |
var theVisibilities = new Array; | |
// create the file-name; | |
if (theSuffix.length > 0) { | |
var aSuffix = "_" + theSuffix | |
} | |
else { | |
var aSuffix = "" | |
}; | |
// create a copy; | |
var theCopy = myDocument.duplicate("thecopy", true); | |
// do the operation; | |
for (var m = theLayerSelection.length - 1; m >= 0; m--) { | |
app.activeDocument = myDocument; | |
var theLayer = theLayerSets[theLayerSelection[m]]; | |
if (theNumbering == true) { | |
theNumber = bufferNumberWithZeros((Number (theNumber) + 1), 2); | |
theNumberString = "_" + theNumber | |
}; | |
else { | |
theNumberString = "" | |
}; | |
// get the layername for the file-name; | |
if (theLayerNameAdd == true) { | |
var aLayerName = "_" + theLayer.name.replace("/", "_") | |
} | |
else { | |
var aLayerName = "" | |
} | |
// transfer layerset over to the copy; | |
theLayer.duplicate (theCopy, ElementPlacement.PLACEATBEGINNING); | |
app.activeDocument = theCopy; | |
// hide the last added layer; | |
theCopy.layers[1].visible = false; | |
DeleteEmptyOrHiddenLayers(); | |
theCopy.saveAs((new File(theDestination+"/"+myDocName+aSuffix+aLayerName+theNumberString+".psd")),PSDOpts,true) | |
}; | |
theCopy.close(SaveOptions.DONOTSAVECHANGES); | |
} | |
}; | |
else {alert ("no document open")}; | |
//////////////////////////////////// | |
////// buffer number with zeros ////// | |
function bufferNumberWithZeros (number, places) { | |
var theNumberString = String(number); | |
for (var o = 0; o < (places - String(number).length); o++) { | |
theNumberString = String("0" + theNumberString) | |
}; | |
return theNumberString | |
}; | |
////// function collect all layersets ////// | |
function collectLayerSets (theParent) { | |
if (!allLayerSets) { | |
var allLayerSets = new Array} | |
else {}; | |
for (var m = theParent.layers.length - 1; m >= 0;m--) { | |
var theLayer = theParent.layers[m]; | |
// apply the function to layersets; | |
if (theLayer.typename == "ArtLayer") { | |
// allLayerSets = allLayerSets.concat(theLayer) | |
} | |
else { | |
// this line includes the layer groups; | |
allLayerSets = allLayerSets.concat(theLayer); | |
allLayerSets = allLayerSets.concat(collectLayerSets(theLayer)) | |
} | |
} | |
return allLayerSets | |
}; | |
function DeleteEmptyOrHiddenLayers() { | |
if ( app.documents.length > 0 ) | |
{ | |
var startRulerUnits = app.preferences.rulerUnits; | |
app.preferences.rulerUnits = Units.PIXELS; | |
removeAllEmptyOrHidden(app.activeDocument); | |
app.preferences.rulerunits = startRulerUnits; | |
} | |
} | |
function removeAllHiddenArtLayers(obj) { | |
for( var i = obj.artLayers.length-1; 0 <= i; i--) { | |
try { | |
if (obj.artLayers[i].visible == false) | |
{ | |
obj.artLayers[i].remove(); | |
} | |
} | |
catch (e) { | |
} | |
} | |
for( var i = obj.layerSets.length-1; 0 <= i; i--) { | |
removeAllHiddenArtLayers(obj.layerSets[i]); | |
} | |
} | |
function removeAllEmptyArtLayers(obj) { | |
for( var i = obj.artLayers.length-1; 0 <= i; i--) { | |
try { | |
if (obj.artLayers[i].kind == LayerKind.NORMAL && obj.artLayers[i].bounds[2] == 0 && obj.artLayers[i].bounds[3] == 0) | |
{ | |
obj.artLayers[i].remove(); | |
} | |
} | |
catch (e) { | |
} | |
} | |
for( var i = obj.layerSets.length-1; 0 <= i; i--) { | |
removeAllEmptyArtLayers(obj.layerSets[i]); | |
} | |
} | |
function removeAllEmptyLayerSets(obj) { | |
var foundEmpty = true; | |
for( var i = obj.layerSets.length-1; 0 <= i; i--) { | |
if( removeAllEmptyLayerSets(obj.layerSets[i])) { | |
obj.layerSets[i].remove(); | |
} else { | |
foundEmpty = false; | |
} | |
} | |
if (obj.artLayers.length > 0) { | |
foundEmpty = false; | |
} | |
return foundEmpty; | |
} | |
function removeAllEmptyOrHidden(docRef) { | |
removeAllHiddenArtLayers(docRef) | |
removeAllEmptyArtLayers(docRef); | |
removeAllEmptyLayerSets(docRef); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment