Skip to main content
New Participant
September 7, 2009
Answered

Export Groups to Files?

  • September 7, 2009
  • 13 replies
  • 123401 views

Is there a way to export multiple GROUPS into individual files, such as jpg?  If I run the File > Scripts > Export LAYERS To Files, each LAYER becomes a jpeg, but i'd like each GROUP to be flattened and exported as a jpg. Is there a script or funtion that performs this task?

This topic has been closed for replies.
Correct answer c.pfaffenbichler

You could replace the pdfOptions with

var jpgopts = new JPEGSaveOptions();

jpgopts.embedColorProfile = true;

jpgopts.formatOptions = FormatOptions.STANDARDBASELINE;

jpgopts.matte = MatteType.NONE;

jpgopts.quality = 10;

and edit that according to Your needs.

Then replace »pdfOpts« and ».pdf« in the saveAs-line with »jpgopts« and ».jpg«.

13 replies

New Participant
September 1, 2011

Its not working on mac cs5

c.pfaffenbichler
Community Expert
September 8, 2009

You could try attached Script, You’d have to adapt it to jpg, currently it saves pdfs.

I have not tested it very extensively and only in CS4 though.

// creates pdf-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;
#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', "pdfs from layersets", [500,300,930,840]);	
//create list for layer-selection;
	dlg.layerRange = dlg.add('panel', [21,20,279,445], "select layersets to create pdfs 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,40], "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,475], 'OK', {name:'ok'});
	dlg.cancelBtn = dlg.add('button', [21,460,210,475], '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;
// pdf options	
		pdfOpts = new PDFSaveOptions() ;
		pdfOpts.embedColorProfile = true;
		pdfOpts.PDFCompatibility =  PDFCompatibility.PDF13;
		pdfOpts.downSample =  PDFResample.NONE;
		pdfOpts.vectorData = true;
		pdfOpts.alphaChannels = false;
		pdfOpts.byteOrder = ByteOrder.MACOS; 
		pdfOpts.layers = false ;
		pdfOpts.preserveEditing = false ;
		pdfOpts.convertToEightBit = true;
		pdfOpts.annotations = false;
		pdfOpts.colorConversion = false;
		pdfOpts.embedFonts = true;
		pdfOpts.embedThumbnail = true;
		pdfOpts.transparency = false;
		pdfOpts.encoding = PDFEncoding.PDFZIP ;	
		var theVisibilities = new Array;
// create the pdf-name;
		if (theSuffix.length > 0) {
			var aSuffix = "_" + theSuffix
			}
		else {
			var aSuffix = ""
			};
// create a flattened 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 pdf-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 llast added layer;
			theCopy.layers[1].visible = false;
			theCopy.saveAs((new File(theDestination+"/"+myDocName+aSuffix+aLayerName+theNumberString+".pdf")),pdfOpts,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
};
808eatzAuthor
New Participant
September 9, 2009

Yes, the Groups are all at the top level, and YES! your script worked wonderfully!  I'll need to figure out the jpg instead of pdf portion, but you definitely solved the difficult part. Thank you!!

Community Expert
June 8, 2010

Great skript!

Is there a way to export Groups to png?


Groups to .png is what I*m searching for, too.

Any ideas? Anyone?

c.pfaffenbichler
Community Expert
September 7, 2009

Are all the groups on the topmost hierarchical level or are there groups within groups?