Script for Removing Illustrator "background" Layers from 1000+ files & then resave them to a folder

New Here ,
Dec 17, 2020

Copy link to clipboard

Copied

Hello everyone!

 

This is my first support post. Thank you all in advance for your help and advice.

 

So here is our issue:

  • We have 1000+ illustrator files.
  • Each file has a layer with the name "background".
  • We need to remove the "background" layer from the file and resave it to a folder on the desktop/or save over the original file. (whatever is easier).

 

My question is...is this possible with so many files?

 

TOPICS
Scripting

Views

150

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

Script for Removing Illustrator "background" Layers from 1000+ files & then resave them to a folder

New Here ,
Dec 17, 2020

Copy link to clipboard

Copied

Hello everyone!

 

This is my first support post. Thank you all in advance for your help and advice.

 

So here is our issue:

  • We have 1000+ illustrator files.
  • Each file has a layer with the name "background".
  • We need to remove the "background" layer from the file and resave it to a folder on the desktop/or save over the original file. (whatever is easier).

 

My question is...is this possible with so many files?

 

TOPICS
Scripting

Views

151

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
Dec 17, 2020 0
Adobe Community Professional ,
Dec 17, 2020

Copy link to clipboard

Copied

Hi,

Try following snippet

function main() {
    var folder = Folder(Folder.desktop + "/New Files");
    if (!folder.exists)
        folder.create();
    for (var i = app.documents.length; i <= app.documents.length; i--) {
        try {
            var _backgroudLayer = app.documents[i].layers.getByName('background');
            _backgroudLayer.remove();
            var fileName = app.documents[i].name;
            app.documents[i].saveAs(File(folder + "/" + fileName));
            app.documents[i].close(SaveOptions.DONOTSAVECHANGES);
        } catch (e) {

        }
    }
}

main()

 

This script will create a folder on Desktop with name "New Files" and all files will be saved inside the folder "New Files"

Best regards

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
Reply
Loading...
Dec 17, 2020 3
New Here ,
Dec 17, 2020

Copy link to clipboard

Copied

there are over 1000 files. I don't think they will all be open in Illustrator. I have slightly modified your code

function main() {
    var folder = Folder(Folder.desktop + "/New Files");
    if (!folder.exists)
        folder.create();
    var fileList = File.openDialog("Selest Files", "All:*.ai*", true);
    for (var i = fileList.length; i <= fileList.length; i--) {
        app.open(fileList[i]);
        var doc = app.activeDocument;
        try {
            var _backgroudLayer = doc.layers.getByName('background');
            _backgroudLayer.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
Reply
Loading...
Dec 17, 2020 2
New Here ,
Jan 08, 2021

Copy link to clipboard

Copied

OK, firstly i would just like to thank you guys for your quick replies. 

Secondly, i just got my hands on the files in question and none of them have a layer with the name "background".

But, is there a possibility to delete sub or grouped layers with the standard "<rectangle>" named layers?

 

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
Reply
Loading...
Jan 08, 2021 0
New Here ,
Jan 08, 2021

Copy link to clipboard

Copied

if you need to remove the bottom layer but try this

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

        }
    }
}

main()

if you need to delete only the bottom object, then try this

function main() {
    var folder = Folder(Folder.desktop + "/New Files");
    if (!folder.exists)
        folder.create();
    var fileList = File.openDialog("Selest Files", "All:*.ai*", true);
    for (var i = fileList.length; i <= fileList.length; 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
Reply
Loading...
Jan 08, 2021 0
New Here ,
Jan 08, 2021

Copy link to clipboard

Copied

Getting this error message...

image.png

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
Reply
Loading...
Jan 08, 2021 0
Most Valuable Participant ,
Jan 08, 2021

Copy link to clipboard

Copied

There is a typo in the loop.

 

for (var i = fileList.length; i <= fileList.length; i--) {

Should be:

for (var i = fileList.length-1; i >= 0; i--) {

 

 

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
Reply
Loading...
Jan 08, 2021 1
New Here ,
Jan 12, 2021

Copy link to clipboard

Copied

Thanks for the updated code. that worked.
but when I try to select the whole folder of EPS files, i get this error now.

danielj15115761_1-1610460786260.png

 

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
Reply
Loading...
Jan 12, 2021 0
New Here ,
Jan 12, 2021

Copy link to clipboard

Copied

Ignore the previous message. The errors came from corrupt files. The code is working perfecly.
Thank you all for your help!

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
Reply
Loading...
Jan 12, 2021 0
Resources