Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


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

Explorer ,
Jun 24, 2021 Jun 24, 2021

Copy link to clipboard

Copied

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

TOPICS
How to, Scripting

Views

909

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 2 Correct answers

Adobe Community Professional , Jun 24, 2021 Jun 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: illustra...

Likes

Translate

Translate
Enthusiast , Jun 24, 2021 Jun 24, 2021
Hi Daniel, @DilliamWowling 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; ...

Likes

Translate

Translate
Explorer ,
Jun 24, 2021 Jun 24, 2021

Copy link to clipboard

Copied

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()

 

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
Enthusiast ,
Jun 24, 2021 Jun 24, 2021

Copy link to clipboard

Copied

Hi Daniel, @DilliamWowling 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

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
Adobe Community Professional ,
Jun 25, 2021 Jun 25, 2021

Copy link to clipboard

Copied

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()


 

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
Adobe Community Professional ,
Jun 24, 2021 Jun 24, 2021

Copy link to clipboard

Copied

 

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()


 

 

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 ,
Jun 25, 2021 Jun 25, 2021

Copy link to clipboard

Copied

Great Work @DilliamWowling  and thank you @m1b too!

 

The new script works great, perfect actually. Thank you once again.

 

The only issue i found - its a very small issue - is that the exported images are saved with the new extension at the end of the previous file name. 

 

For example:

file-name.eps --> file-name.eps.png

 

But this is no big issue as Bridge can batch rename these files using the folder name.

 

The main issues and time saving has been solved. Thanks again! Great Work! 

Daniel

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
Adobe Community Professional ,
Jun 25, 2021 Jun 25, 2021

Copy link to clipboard

Copied

Something i forgot to note when i posted the script... PLEASE BE CAREFUL. This script could easily be described as "destructive" in that it is opening some files, changing them, then saving over them.. It's not saving duplicates, and it's not backing anything up. If you don't have proper backups, and this script encounters a file for which you do NOT want to delete the background layer for any reason... this script will delete that background layer and save over the file and you won't be able to get it back. 

 

Just so you're fully aware. Scripts are quick.. but they only ever do exactly what they're told.. nothing more. so if you set up a set of instructions that should apply 90% of the time.. you're going to have 10% of the processed files that are broken now and the script won't care one bit.

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
Enthusiast ,
Jun 25, 2021 Jun 25, 2021

Copy link to clipboard

Copied

Good point! Come to think of it, my own usual habit is to always save/export into a dated folder, which avoids most overwriting mistakes and also shows me when the file was processed if I come across it later.

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
Adobe Community Professional ,
Jun 25, 2021 Jun 25, 2021

Copy link to clipboard

Copied

I try to do the same when I can as well.. But in this use case I don't think that would be practical. Each subfolder could potentially have tons of different dated folders in it and if you ran the script again on the folder, it would process each dated folder as well (barring some kind of max depth search or conditional folder processing.) 

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
Enthusiast ,
Jun 25, 2021 Jun 25, 2021

Copy link to clipboard

Copied

True! But in this case I would aim to have script recreate the complete hierarchy in the dated folder at the level of the "root" folder. Or (more likely!) I would just duplicate the whole folder first, into a dated folder, and then run a quick-and-dirty script on that, just to get the job done, ha ha.

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
Adobe Community Professional ,
Jun 29, 2021 Jun 29, 2021

Copy link to clipboard

Copied

LATEST

A definite possibility. Based on my professional experience, i'm wary about doing that though. things could quickly get out of control if you have large folders filled with complex files. duplicating everything every time the script is run either fills up a hard drive quickly, or it creates a bunch of maintenance work in the form of keeping archives clean and preventing unneccessary or superfluous duplicates.

 

Personal preference i spose.

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 ,
Jun 25, 2021 Jun 25, 2021

Copy link to clipboard

Copied

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

danielj15115761_0-1624612230915.png

 

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

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 ,
Jun 25, 2021 Jun 25, 2021

Copy link to clipboard

Copied

This is what the folder structure looks like:

Main Folder > Image Folder > Image.eps

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 ,
Jun 25, 2021 Jun 25, 2021

Copy link to clipboard

Copied

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

Thanks again.

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
Adobe Community Professional ,
Jun 25, 2021 Jun 25, 2021

Copy link to clipboard

Copied

Are you still getting multiple extensions on certain files? 

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 ,
Jun 25, 2021 Jun 25, 2021

Copy link to clipboard

Copied

Yeah, the extenstions are still being added to the end of the original file name.

Can this be changed?

 

Daniel

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
Adobe Community Professional ,
Jun 25, 2021 Jun 25, 2021

Copy link to clipboard

Copied

Hey,  DJ.

 

Is it all the files, or just the eps files?

 

I made a fix that should prevent eps files from getting doubble extensions, but i can't see anything that would cause any other file types to mess up.

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 ,
Jun 25, 2021 Jun 25, 2021

Copy link to clipboard

Copied

I'll have to check this on Monday. Thank for all your help!!

DJ 🙂 

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