Skip to main content
Known Participant
December 7, 2022
Answered

Export each layer as a separate file but without hidden layers

  • December 7, 2022
  • 1 reply
  • 1167 views

Hello everyone.

i have this script that export each layer of my document as a separate file (originally in PDF format but i changed to EPS for my necessity)

 

/*
* Description: An Adobe Illustrator script that export each layer as a separate PDF file
* Usage: Layer name is the PDF file name. Rename layers if necessary.
* This is an early version that has not been sufficiently tested. Use at your own risks.
* License: GNU General Public License Version 3. (http://www.gnu.org/licenses/gpl-3.0-standalone.html)
*
* Copyright (c) 2009. William Ngan.
* http://www.metaphorical.net

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/


var doc = app.activeDocument;

// prepare layers
for(var i=0; i<doc.layers.length; i++) {
		doc.layers[i].visible = false;
}


// go through each layers
for(var i=0; i<doc.layers.length; i++) {
	doc.layers[i].visible = true;
	if (i>0) doc.layers[i-1].visible = false;

	var fpath = doc.path; // save to document's folder
	fpath.changePath( doc.layers[i].name+'.eps');
	saveEPS( fpath );
}



/**
	* Save EPS file
	* @9397041 file File object
*/
function saveEPS( file ) {
	
	var saveOpts = new EPSSaveOptions();				
	doc.saveAs( file, saveOpts );
}

 

The outcome is: one EPS file for each layers of the original document with one layer visible and all the other invisible.

What i need is that each EPS file has only the visible layer.

 

For now to get the result i have to subsequently use another script that i use with batch option:

#target illustrator
var myDoc=app.activeDocument;
var layerCount=myDoc.layers.length;
for (var ii = layerCount - 1; ii >= 0; ii--) {
    var currentLayer = myDoc.layers[ii];
    currentLayer.locked = false;
    if (currentLayer.visible == false){
        currentLayer.visible = true;
        currentLayer.remove();
        }
    }

 

 Is there a way to make this happen in just one script?

Thanks!

This topic has been closed for replies.
Correct answer Sergey Osokin
main();

function main() {
  var doc = app.activeDocument;
  var layers = doc.layers;
  var epsArr = [];
  var i = 0;

  // Prepare layers
  for (i = 0; i < layers.length; i++) {
    layers[i].visible = false;
  }

  // Go through each layers
  for (i = 0; i < layers.length; i++) {
    layers[i].visible = true;
    if (i > 0) layers[i - 1].visible = false;
    var fPath = doc.path; // save to document's folder
    fPath.changePath(layers[i].name + '.eps');
    saveEPS(fPath);
    epsArr.push(fPath);
  }

  doc.close(SaveOptions.DONOTSAVECHANGES);

  app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
  for (i = 0; i < epsArr.length; i++) {
    rmvHiddenLayers(epsArr[i]);
  }
  app.userInteractionLevel = UserInteractionLevel.DISPLAYALERTS;
}

// Save EPS file
function saveEPS(f) {
  var saveOpts = new EPSSaveOptions();
  activeDocument.saveAs(f, saveOpts);
}

// Remove hidden layers from specified file and save
function rmvHiddenLayers(fPath) {
  var doc = File(fPath);
  try {
    doc = app.open(doc);
    for (var i = doc.layers.length - 1; i >= 0; i--) {
      if (!doc.layers[i].visible) {
        doc.layers[i].visible = true;
        doc.layers[i].remove();
      }
    }
    doc.close(SaveOptions.SAVECHANGES);
  } catch (e) {}
}

1 reply

Sergey Osokin
Sergey OsokinCorrect answer
Inspiring
December 7, 2022
main();

function main() {
  var doc = app.activeDocument;
  var layers = doc.layers;
  var epsArr = [];
  var i = 0;

  // Prepare layers
  for (i = 0; i < layers.length; i++) {
    layers[i].visible = false;
  }

  // Go through each layers
  for (i = 0; i < layers.length; i++) {
    layers[i].visible = true;
    if (i > 0) layers[i - 1].visible = false;
    var fPath = doc.path; // save to document's folder
    fPath.changePath(layers[i].name + '.eps');
    saveEPS(fPath);
    epsArr.push(fPath);
  }

  doc.close(SaveOptions.DONOTSAVECHANGES);

  app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
  for (i = 0; i < epsArr.length; i++) {
    rmvHiddenLayers(epsArr[i]);
  }
  app.userInteractionLevel = UserInteractionLevel.DISPLAYALERTS;
}

// Save EPS file
function saveEPS(f) {
  var saveOpts = new EPSSaveOptions();
  activeDocument.saveAs(f, saveOpts);
}

// Remove hidden layers from specified file and save
function rmvHiddenLayers(fPath) {
  var doc = File(fPath);
  try {
    doc = app.open(doc);
    for (var i = doc.layers.length - 1; i >= 0; i--) {
      if (!doc.layers[i].visible) {
        doc.layers[i].visible = true;
        doc.layers[i].remove();
      }
    }
    doc.close(SaveOptions.SAVECHANGES);
  } catch (e) {}
}
DelrestoAuthor
Known Participant
December 8, 2022

Hi @Sergey Osokin , i tested it and it works! Brilliant!

Since i want the final eps with all items visible i added this line

app.executeMenuCommand('showAll'); 

just before

 doc.close(SaveOptions.SAVECHANGES);

 

Even if it seems to work fine, i'd like your confirmation that it's in the right place.

Anyway, thanks a lot!

Wish you the best!

Sergey Osokin
Inspiring
December 8, 2022

It's a working method.