Skip to main content
danielj15115761
Inspiring
June 24, 2021
Answered

Script: Export as EPS, AI, PNG all in the same folder

  • June 24, 2021
  • 2 replies
  • 7371 views

Good Afternoon,

 

Here is our situation:

We have several 1000 .EPS vector files which are inside a folder and sub-folders. We need to remove the "background" layer from the vector file and save the file as an EPS, AI & PNG/JPG. These file need to saved in the original vector images folder. The original vector file can be left or saved over.

 

The attached script has been built to search for .ai and .eps files, open them, remove the bottom layer and save over the original file.

 

Our previous script (see attached) removed the layer with the name "background". We prefer this option because not all of our designs have a background layer. In these cases the vector image would be deleted and saved with a blank artboard.

 

OK, this is what we are looking for:

  1. Search a folder and sub-folders for .ai & .eps files
  2. Remove the layer name "background"
  3. If no "background" layer is in the file, then bottom layer should stay.
  4. Save as, .AI, .EPS & .PNG .JPG
  5. Images should be saved in the original vector image sub-folder.

 

Any help will be welcomed.

 

Thanks in advance,

Daniel

This topic has been closed for replies.
Correct answer m1b

Hi Daniel, @Disposition_Dev has well answered this question with a comprehensively structured script, but for your learning, here is your modified script 1, so you can see the changes. There aren't many.

function main() {

    var inputFolder = Folder.selectDialog("Select a folder to process")
    if (inputFolder != null) {
        var fileandfolderAr = scanSubFolders(inputFolder, /\.(ai|eps)$/i);
        var fileList = fileandfolderAr[0];
    } else {
        // user cancelled
        return;
    }
    for (var i = fileList.length - 1; i >= 0; i--) {
        app.open(fileList[i]);
        var doc = app.activeDocument;
        try {
            var _backgroundLayer = doc.layers.getByName('background');
            _backgroundLayer.remove();
        } catch (e) { }
        doc.close(SaveOptions.SAVECHANGES);
    }
}

main()

function scanSubFolders(tFolder, myAI) {
    var sFolders = [];
    var allFiles = [];
    sFolders[0] = tFolder;

    for (var j = 0; j < sFolders.length; j++) {
        var procFiles = sFolders[j].getFiles();

        for (var i = 0; i < procFiles.length; i++) {
            if (procFiles[i] instanceof File) {
                if (myAI == undefined) {
                    allFiles.push(procFiles);
                }

                if (procFiles[i].fullName.search(myAI) != -1) {
                    allFiles.push(procFiles[i]);
                }
            }

            else if (procFiles[i] instanceof Folder) {
                sFolders.push(procFiles[i]);
                scanSubFolders(procFiles[i], myAI);
            }
        }
    }
    return [allFiles, sFolders];
}

Regards,

Mark

2 replies

Disposition_Dev
Community Expert
June 24, 2021

 

Give this a try. Seems to work on my end.. But i'm only guessing as to the actual contents and formatting of your own folder structure and such so i can't be certain this will work on your end.

//Script Name: i have no idea what to call it.. any ideas?
//Script Author: Adobe Support Forum User danielj15115761
//Adobe Discussion Post: https://community.adobe.com/t5/illustrator/script-export-as-eps-ai-png-all-in-the-same-folder/td-p/12135881
//Script Edited by: Dilliam Wowling
	//Email: illustrator.dev.pro@gmail.com
	//github: github.com/wdjsdev
	//paypal: https://www.paypal.com/paypalme/illustratordev

#target Illustrator
function main()
{

	//logic container vvvv


	//function for saving the doc
	//into each necessary format.
	function saveDoc(doc)
	{
		var fn = doc.fullName.toString();
		var docName = fn.substring(fn.lastIndexOf("/")+1,fn.length).replace(/\.[aipdf]{2,3}/i,"");
		var savePath = fn.substring(0,fn.lastIndexOf("/")+1);

		var saveFile;
		var ext;
		var opts;

		//save the ai file:
		ext = ".ai";
		doc.saveAs(File(savePath + docName + ext));

		//save the eps
		opts = new EPSSaveOptions();
		ext = ".eps";
		doc.saveAs(File(savePath + docName + ext),opts);

		ext = ".jpg"
		doc.exportFile(File(savePath + docName + ext),ExportType.JPEG);

		ext = ".png"
		doc.exportFile(File(savePath + docName + ext),ExportType.PNG24);
		

	}

	//generic function for identifying a layer by name
	//essentially this replaces the native getByName()
	//method that causes a runtime error if the layer
	//doesn't exist. this will just return undefined
	//if the layer doesn't exist.
	function findSpecificLayer(parent,layerName,crit)
	{
		var result,layers;

		if(parent.typename === "Layer" || parent.typename === "Document")
		{
			layers = parent.layers;	
		}
		else if(parent.typename === "Layers")
		{
			layers = parent;
		}
		
		for(var x=0,len=layers.length;x<len && !result;x++)
		{
			if(crit && crit === "any" && layers[x].name.toLowerCase().indexOf(layerName.toLowerCase())>-1)
			{
				result = layers[x];
			}
			else if(layers[x].name.toLowerCase() === layerName.toLowerCase())
			{
				result = layers[x];
			}
		}
		return result;
	}


	//depth first recursive search
	//no max depth. pushes all ai or eps files
	//anywhere inside the given folder to global
	//allFiles array for processing.
	function scanSubFolders(tFolder)
	{
		var files = tFolder.getFiles();

		for(var f=0;f<files.length;f++)
		{
			if(files[f] instanceof File && aiEpsRegex.test(files[f].name))
			{
				allFiles.push(files[f]);
			}
			else if(files[f] instanceof Folder)
			{
				scanSubFolders(files[f]);
			}
		}
	}


	//logic container ^^^^




	//procedure vvvv


	var sFolders = [];
	var allFiles = [];

	//ask the user for a folder to process
	var inputFolder = Folder.selectDialog("Select a folder to process")
	if (inputFolder === null)
	{
		return undefined;
	}


	var aiEpsRegex = /\.(ai|eps)$/i;

	//get the files from the given folder and its subfolders
	scanSubFolders(inputFolder);
	if(!allFiles.length === 0)
	{
		alert("No files found to process.");
		return undefined;
	}


	var docsToClose = [];
	for (var i = allFiles.length - 1; i >= 0; i--)
	{
		app.open(allFiles[i]);
		var doc = app.activeDocument;
		var docLayers = doc.layers;
		var bgLayer = findSpecificLayer(docLayers,"background");
		if(bgLayer && docLayers.length > 1)
		{
			bgLayer.remove();
		}

		saveDoc(doc);
		docsToClose.push(doc);
	}


	for(var d=docsToClose.length-1;d>=0;d--)
	{
		docsToClose[d].close(SaveChanges.DONTSAVECHANGES);
	}

	//procedure ^^^^
}

main()


 

 

danielj15115761
Inspiring
June 25, 2021

I just ran a small test with a collection of folders. I get this error after the script processes around 10 folders and images:

 

Also, i have noticed that the all the files stay open in illustrator. Could this be changed to close the file before processing the next image?

 

Thanks in advance once again.

Daniel

danielj15115761
Inspiring
June 25, 2021

OK, forget the error above. This error message came from a broken EPS file. The Script is perfect!

Thanks again.

danielj15115761
Inspiring
June 24, 2021

Script 1:

function main() {

        var inputFolder = Folder.selectDialog("Select a folder to process")
         if (inputFolder != null){
        var fileandfolderAr = scanSubFolders(inputFolder, /\.(ai|eps)$/i);
        var fileList = fileandfolderAr[0];
        }
        for (var i = fileList.length-1; i >= 0; i--) {
        app.open(fileList[i]);
        var doc = app.activeDocument;
        try {
            var docLayers = doc.layers;
            var n = docLayers.length;
            docLayers[n-1].visible = true;
            docLayers[n-1].locked = false; 
            docLayers[n-1].remove();
            doc.save();
            doc.close(SaveOptions.NO);
        } catch (e) {

        }
    }
}

main()

function scanSubFolders(tFolder, myAI) {
var sFolders = [];
var allFiles = [];
sFolders[0] = tFolder;

for (var j = 0; j < sFolders.length; j++) {
var procFiles = sFolders[j].getFiles();

for (var i = 0; i < procFiles.length; i++) {
if (procFiles[i] instanceof File) {
if(myAI == undefined) {
allFiles.push(procFiles);
}

if (procFiles[i].fullName.search(myAI) != -1) {
allFiles.push(procFiles[i]);
  }
}

else if (procFiles[i] instanceof Folder) {
sFolders.push(procFiles[i]);
scanSubFolders(procFiles[i], myAI);
    }
  }
}
return [allFiles, sFolders];
}

 

Script 2:

function main() {
    var folder = Folder(Folder.desktop + "/New Files");
    if (!folder.exists)
        folder.create();
    var fileList = File.openDialog("Selest Files", "All:*.eps*", true);
    for (var i = fileList.length-1; i >= 0; i--) {
        app.open(fileList[i]);
        var doc = app.activeDocument;
        try {
            //var _backgroudLayer = doc.layers.getByName('background');
            //_backgroudLayer.remove();
            var pI= doc.pathItems;
            var n = pI.length; 
            pI[n-1].selected;
            pI[n-1].remove();
            var fileName = doc.name;
            doc.saveAs(File(folder + "/" + fileName));
            doc.close(SaveOptions.DONOTSAVECHANGES);
        } catch (e) {

        }
    }
}

main()

 

m1b
m1bCorrect answer
Community Expert
June 25, 2021

Hi Daniel, @Disposition_Dev has well answered this question with a comprehensively structured script, but for your learning, here is your modified script 1, so you can see the changes. There aren't many.

function main() {

    var inputFolder = Folder.selectDialog("Select a folder to process")
    if (inputFolder != null) {
        var fileandfolderAr = scanSubFolders(inputFolder, /\.(ai|eps)$/i);
        var fileList = fileandfolderAr[0];
    } else {
        // user cancelled
        return;
    }
    for (var i = fileList.length - 1; i >= 0; i--) {
        app.open(fileList[i]);
        var doc = app.activeDocument;
        try {
            var _backgroundLayer = doc.layers.getByName('background');
            _backgroundLayer.remove();
        } catch (e) { }
        doc.close(SaveOptions.SAVECHANGES);
    }
}

main()

function scanSubFolders(tFolder, myAI) {
    var sFolders = [];
    var allFiles = [];
    sFolders[0] = tFolder;

    for (var j = 0; j < sFolders.length; j++) {
        var procFiles = sFolders[j].getFiles();

        for (var i = 0; i < procFiles.length; i++) {
            if (procFiles[i] instanceof File) {
                if (myAI == undefined) {
                    allFiles.push(procFiles);
                }

                if (procFiles[i].fullName.search(myAI) != -1) {
                    allFiles.push(procFiles[i]);
                }
            }

            else if (procFiles[i] instanceof Folder) {
                sFolders.push(procFiles[i]);
                scanSubFolders(procFiles[i], myAI);
            }
        }
    }
    return [allFiles, sFolders];
}

Regards,

Mark

Disposition_Dev
Community Expert
June 25, 2021

I started out by just trying to update the existing script by fixing a couple issues.. But the issues turned out to be pretty numerous, so it seemed better to just restructure everything.

 

The recursive logic in the scanSubFolders() function is incomplete. It's a recursive function that returns things, but the same recursive function gets called from inside the function without storing or using the return value. It's wasted compute cycles for no gain at all (and its worse, because you might think the script fixed everything, but really it may have left some stuff behind). If anyone out there is reading this and doesn't know what i mean, feel free to ask. Recursive logic is tough.

 

@danielj15115761 

I just realized why your eps files are getting double extensions. try this version instead:

//Script Name: i have no idea what to call it.. any ideas?
//Script Author: Adobe Support Forum User danielj15115761
//Adobe Discussion Post: https://community.adobe.com/t5/illustrator/script-export-as-eps-ai-png-all-in-the-same-folder/td-p/12135881
//Script Edited by: Dilliam Dowling
	//Email: illustrator.dev.pro@gmail.com
	//github: github.com/wdjsdev
	//paypal: https://www.paypal.com/paypalme/illustratordev

#target Illustrator
function main()
{

	//logic container vvvv


	//function for saving the doc
	//into each necessary format.
	function saveDoc(doc)
	{
		var fn = doc.fullName.toString();
		var docName = fn.substring(fn.lastIndexOf("/")+1,fn.length).replace(/\.[aipdfeps]{2,3}/i,"");
		var savePath = fn.substring(0,fn.lastIndexOf("/")+1);

		var saveFile;
		var ext;
		var opts;

		//save the ai file:
		ext = ".ai";
		doc.saveAs(File(savePath + docName + ext));

		//save the eps
		opts = new EPSSaveOptions();
		ext = ".eps";
		doc.saveAs(File(savePath + docName + ext),opts);

		ext = ".jpg"
		doc.exportFile(File(savePath + docName + ext),ExportType.JPEG);

		ext = ".png"
		doc.exportFile(File(savePath + docName + ext),ExportType.PNG24);
		

	}

	//generic function for identifying a layer by name
	//essentially this replaces the native getByName()
	//method that causes a runtime error if the layer
	//doesn't exist. this will just return undefined
	//if the layer doesn't exist.
	function findSpecificLayer(parent,layerName,crit)
	{
		var result,layers;

		if(parent.typename === "Layer" || parent.typename === "Document")
		{
			layers = parent.layers;	
		}
		else if(parent.typename === "Layers")
		{
			layers = parent;
		}
		
		for(var x=0,len=layers.length;x<len && !result;x++)
		{
			if(crit && crit === "any" && layers[x].name.toLowerCase().indexOf(layerName.toLowerCase())>-1)
			{
				result = layers[x];
			}
			else if(layers[x].name.toLowerCase() === layerName.toLowerCase())
			{
				result = layers[x];
			}
		}
		return result;
	}


	//depth first recursive search
	//no max depth. pushes all ai or eps files
	//anywhere inside the given folder to global
	//allFiles array for processing.
	function scanSubFolders(tFolder)
	{
		var files = tFolder.getFiles();

		for(var f=0;f<files.length;f++)
		{
			if(files[f] instanceof File && aiEpsRegex.test(files[f].name))
			{
				allFiles.push(files[f]);
			}
			else if(files[f] instanceof Folder)
			{
				scanSubFolders(files[f]);
			}
		}
	}


	//logic container ^^^^




	//procedure vvvv


	var sFolders = [];
	var allFiles = [];

	//ask the user for a folder to process
	var inputFolder = Folder.selectDialog("Select a folder to process")
	if (inputFolder === null)
	{
		return undefined;
	}


	var aiEpsRegex = /\.(ai|eps)$/i;

	//get the files from the given folder and its subfolders
	scanSubFolders(inputFolder);
	if(!allFiles.length === 0)
	{
		alert("No files found to process.");
		return undefined;
	}


	var docsToClose = [];
	for (var i = allFiles.length - 1; i >= 0; i--)
	{
		app.open(allFiles[i]);
		var doc = app.activeDocument;
		var docLayers = doc.layers;
		var bgLayer = findSpecificLayer(docLayers,"background");
		if(bgLayer && docLayers.length > 1)
		{
			bgLayer.remove();
		}

		saveDoc(doc);
		docsToClose.push(doc);
	}


	for(var d=docsToClose.length-1;d>=0;d--)
	{
		docsToClose[d].close(SaveChanges.DONTSAVECHANGES);
	}

	//procedure ^^^^
}

main()