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

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

Explorer ,
Dec 17, 2020 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

1.5K

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

Advisor , Jun 22, 2021 Jun 22, 2021

Hello,

I'm not sure what script in this thread you were refering to so I grabed the one that removes the bottom layer.

The script below will look in folders\subfolders for .ai files, remove the bottom layer and save over the original file(s).

 

function main() {

        var inputFolder = Folder.selectDialog("Select a folder to process")
         if (inputFolder != null){
        var fileandfolderAr = scanSubFolders(inputFolder, /\.(ai)$/i);
        var fileList = fileandfolderAr[0];
        }
     
...

Votes

Translate

Translate
Adobe
Community Expert ,
Dec 17, 2020 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

Votes

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
New Here ,
Dec 17, 2020 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()

Votes

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 ,
Jan 08, 2021 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!!

Votes

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
New Here ,
Jan 08, 2021 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()

Votes

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 ,
Jan 08, 2021 Jan 08, 2021

Copy link to clipboard

Copied

Getting this error message...

image.png

Votes

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
Community Expert ,
Jan 08, 2021 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--) {

 

 

Votes

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 ,
Jan 12, 2021 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

 

Votes

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 ,
Jan 12, 2021 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!

Votes

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

Copy link to clipboard

Copied

Hi Guys,

 

I have a question about this code. But, first here is my situation...

We have received 1000+ vector files, each with its own folder. So the vector files all need stay in this filing system. 

 

My question: Is there a way of updating this script to include these folders and to search for the vector files through the main folder? AND, resaving or save over the original file?

 

Example from the Filing System:

Main Folder > Image Folder > Image.ai

 

Thanks in advance for your help and feedback!

Votes

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

Copy link to clipboard

Copied

How can I combine the below script with the script in this thread?

Any and all help is greatly appreciated.

 

 //   Illustrator  convert to legacy . jsx //  https :// gist . github . com / joonaspaakko / df2f9e31bdb365a6e5df //   Finds  all . ai files from the input folder +  its subfolders and converts them to the version given below in  a variable called "targetVersion" 

 //   Tested   in   Illustrator  cc 2014   ( Mac ) 
 //   Didn 't bother to do a speed test with my macbook air... #target illustrator // If set to false, a new file will be written next to the original file. // The new file will have (legacyFile) in the name. // Files with (legacyFile) in the file name are always ignored. var overwrite = true, // boolean // Accepted values: // 8, 9, 10, 11 (cs), 12 (cs2), 13 (cs3), 14 (cs4), 15 (cs5), 16 (cs6), 17 (cc) targetVersion = 13; if ( app.documents.length > 0 ) { alert("ERROR: \n Close all documents before running this script." ); } // Run the script else { var files, folder = Folder.selectDialog("Input folder..."); // If folder variable return null, user most likely canceled the dialog or // the input folder and it subfolders don' t contain any . ai files . 
     if   (  folder !=  null )   { 

         //  returns an array of file paths in  the selected folder . files =   GetFiles (  folder ); 

         //   This  is where things actually start happening ... process (  files ); 

     } 

 } 


 function  process (  files )   { 

     //   Loop  through the list of . ai files : 
     //   Open   >   Save   >   Close   >  LOOP for   (  i =   0 ;  i <  files . length ;  i ++   )   { 

         //   Current  file var file =  files [ i ] 

         //   Open app . open (  file ); 

         //   If  overwrite is false ,  create a new file ,  otherwise use "file"  variable ; file =   ! overwrite ?  new File (  file . toString (). replace ( ".ai" ,   " (legacyFile).ai" )   )   :  file ; 

         //   Save app . activeDocument . saveAs (  file ,   SaveOptions_ai ()   ) 

         //   Close app . activeDocument . close (   SaveOptions . DONOTSAVECHANGES ); 

     } 

     //   For  better of for  worse ... alert (   "Script is done."   ); 

 } 

 function   SaveOptions_ai ()   { var saveOptions =  new IllustratorSaveOptions (); saveOptions . compatibility =   Compatibility [   "ILLUSTRATOR"   +  targetVersion ]; saveOptions . flattenOutput =   OutputFlattening . PRESERVEAPPEARANCE ; saveOptions . compressed =  true ;   //   Version   10  or later saveOptions . pdfCompatible =  true ;   //   Version   10  or later saveOptions . embedICCProfile =  true ;   //   Version   9  or later saveOptions . embedLinkedFiles =  false ;   //   Version   7  or later return  saveOptions } 

 function   GetFiles (  folder )   { var i ,  item , 
         //   Array  to store the files in ... files =   [], 
         //   Get  files ... items =  folder . getFiles (); 

     //   Loop  through all files in  the given folder for   (  i =   0 ;  i <  items . length ;  i ++   )   { item =  items [ i ]; 

         //   Find   . ai files var fileformat =  item . name . match (/ \.ai$ / i ), legacyFile =  item . name . indexOf ( "(legacyFile)" )   >   0 ; 

         //   If  item is a folder ,  check the folder for  files . 
         if   (  item instanceof Folder   )   { 

             //   Combine  existing array with files found in  the folder files =  files . concat (   GetFiles (  item )   ); 


         } 
         //   If  the item is a file ,  push it to the array . 
         else   if   (  item instanceof File   &&  fileformat &&   ! legacyFile )   { 

             //   Push  files to the array files . push (  item ); 

         } 
     } 

     return  files ; 
 } 

 

 

Votes

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

Copy link to clipboard

Copied

Obviously, I don't need to save my files as legacy .ai. I just need to access the image files which are inside folders and subfolders.

 

Thanks in advance.

Votes

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
Advisor ,
Jun 22, 2021 Jun 22, 2021

Copy link to clipboard

Copied

Hello,

I'm not sure what script in this thread you were refering to so I grabed the one that removes the bottom layer.

The script below will look in folders\subfolders for .ai files, remove the bottom layer and save over the original file(s).

 

function main() {

        var inputFolder = Folder.selectDialog("Select a folder to process")
         if (inputFolder != null){
        var fileandfolderAr = scanSubFolders(inputFolder, /\.(ai)$/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];
}

 

 

Regards,

Mike

Votes

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

Copy link to clipboard

Copied

Thank you Mike! Your script works perfectly.

Sorry to add another point. But, can this script be updated to include .EPS files too? 

 

I mean, can this script export more than one file option. We would need the following files .AI, .EPS, .PNG & .JPG

 

Thanks again! 

Votes

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
Advisor ,
Jun 23, 2021 Jun 23, 2021

Copy link to clipboard

Copied

LATEST

Hello,

 

To inclued .eps files

 

//change
var fileandfolderAr = scanSubFolders(inputFolder, /\.(ai)$/i);
//to
var fileandfolderAr = scanSubFolders(inputFolder, /\.(ai|eps)$/i);

 

 

Re: the export options, you really should create a new post.

Regards,

Mike

Votes

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