• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers

Export each layer as a separate file but without hidden layers

Explorer ,
Dec 07, 2022 Dec 07, 2022

Copy link to clipboard

Copied

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
	* @Param 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!

TOPICS
Scripting

Views

345

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Contributor , Dec 07, 2022 Dec 07, 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(fPa
...

Likes

Translate

Translate
Contributor ,
Dec 07, 2022 Dec 07, 2022

Copy link to clipboard

Copied

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) {}
}

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 08, 2022 Dec 08, 2022

Copy link to clipboard

Copied

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!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 08, 2022 Dec 08, 2022

Copy link to clipboard

Copied

It's a working method.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 13, 2022 Dec 13, 2022

Copy link to clipboard

Copied

Hi @Sergey Osokin  sorry to bother you again but i tested your script again adn again and the result is Ai crashing once every couple of times. I tried it with two different files, same result.

Is there a way to prevent this to happen? Maybe it's not the script's fault, i know, but maybe there's a way to improve it so it works everytime?

Thanks in advance!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 13, 2022 Dec 13, 2022

Copy link to clipboard

Copied

LATEST

Have you tried another version of Illustrator? Or on a different computer? There's not enough data yet to guess why it crashes. Maybe you can show the file, your version of Illustrator?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines