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

Illustrator Script to Remove Sublayers By Name Automatically

Guest
Jan 31, 2018 Jan 31, 2018

Copy link to clipboard

Copied

I've over 2000 file and all these files have named "Karışım" sublayers under main layer which named "Ana Ikonlar" and I've to remove all sublayers. İf I remove one by one, it takes over 2 days but I need to make it automatically with  scripts.

Anybody know how to remove them automatically with scripts?

I tried lots of scripting but none of them remove sublayers.

2018-01-31_23-32-46.png

TOPICS
Scripting

Views

2.6K

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
Valorous Hero ,
Jan 31, 2018 Jan 31, 2018

Copy link to clipboard

Copied

var main = function() {

var doc, layers, n = 0, layerName = "yolo";

if ( !app.documents.length ) {

alert("This scripts needs an open document !");

return;

}

doc  = app.activeDocument;

layers = doc.layers;

n = layers.length;

while ( n-- ) removeLayer ( layers, layerName );

}

function removeLayer ( layer, layerName ) {

var subLayers = layer.layers, n = subLayers.length;

if ( layer.name == layerName ) {

layer.remove();

return;

}

while ( n-- ) removeLayer ( subLayers, layerName );

}

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
Community Beginner ,
Jul 31, 2019 Jul 31, 2019

Copy link to clipboard

Copied

I'm trying to use this script to remove any pageItems called "yolo", but as it is, this script will only delete a sub group/folder called that, how can I make it remove or just select actual individual page items (elipses) with the name "yolo"?

Thanks!

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 ,
Jan 31, 2018 Jan 31, 2018

Copy link to clipboard

Copied

Hi, you can try this — but with caution — on a folder containing only the files that you need to modify (designed for files with extension ai)

#target "Illustrator";

var myFolder = Folder.selectDialog("Select folder");

var myFiles = myFolder.getFiles ("*.ai");

for (i=0; i<myFiles.length; i++) {

    var myDoc = app.open(myFiles);

    try {

        myDoc.layers.getByName("Ana Ikonlar").layers.getByName("Karışım").remove();

        myDoc.close(SaveOptions.SAVECHANGES);

    } catch (e) {

        //alert (e);

        myDoc.close(SaveOptions.DONOTSAVECHANGES);

    }

}

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
Valorous Hero ,
Jan 31, 2018 Jan 31, 2018

Copy link to clipboard

Copied

hi robinfredericf

Lets'merge the best of our respective scripts:

#target "Illustrator"; 

var main = function() {

var myFolder = Folder.selectDialog("Select folder"),

myFiles, i = 0, n = 0, nDoc, layers, layerName = "yolo";;

if( !myFolder ) return;

myFiles = myFolder.getFiles ("*.ai");

n = myFiles.length;

if( !n) {

alert("No files found, sorry" );

return;

}

 

for (i=0; i<n; i++) { 

nDoc = app.open(myFiles); 

layers = nDoc.layers;

n = layers.length;

while ( n-- ) removeLayer ( layers, layerName );

nDoc.close ( SaveOptions.SAVECHANGES );

}

}

function removeLayer ( layer, layerName ) {

var subLayers = layer.layers, n = subLayers.length;

if ( layer.name == layerName ) {

layer.remove();

return;

}

while ( n-- ) removeLayer ( subLayers, layerName );

}

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 ,
Jan 31, 2018 Jan 31, 2018

Copy link to clipboard

Copied

I was trying to edit my 1st answer to say more precisely

Hi, you can try this — but with caution — on a folder containing only the files that you need to modify, to remove only the unwanted "Karışım" sublayer from all these files (assuming that the files are in *ai format and can be simply saved in the current used Illustrator version without changing any option)

if this is actually the only task Mr. Ulas wants to achieve, but it is useful to try to write a more versatile script instead of a customized for unique use.

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
Community Beginner ,
Aug 05, 2021 Aug 05, 2021

Copy link to clipboard

Copied

Hi there! I'm trying to use this script for a similar task but my files aren't .ai files they are .pdf files. 
When I try running the script it gets stuck on 

var myDoc = app.open(myFiles); 

 And I'm assuming that's because when you're defining myFiles you're using .ai. 
Do you know a way around this by using .pdf's?

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
Advisor ,
Aug 05, 2021 Aug 05, 2021

Copy link to clipboard

Copied

From looking at the script (not tested), make the following changes. 

 

1.  Change

myFiles = myFolder.getFiles ("*.ai");

to

myFiles = myFolder.getFiles ();

 

2.  Change

nDoc = app.open(myFiles);  

to

nDoc = app.open(myFiles[i]); 

 

3.  Change

while ( n-- ) removeLayer ( layers, layerName );

to

while ( n-- ) removeLayer ( layers[n], layerName );

 

4.  Change

while ( n-- ) removeLayer ( subLayers, layerName );

to

while ( n-- ) removeLayer ( subLayers[n], layerName );

 

Lastly, either your layer/sublayer is named "yolo", or change "yolo" in the script to the name of your layer/sublayer.

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 ,
Jul 31, 2019 Jul 31, 2019

Copy link to clipboard

Copied

You have to change layers to pageItems, so iterate through pageItems, and if it has correct name add it to selection.

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

Copy link to clipboard

Copied

 

// Deletes all layers whose name begins with "Karışım" in all open documents

var layersDeleted = 0;
for (var i = 0; i < app.documents.length; i++) {
  var targetDocument = app.documents[i];
  var layerCount = targetDocument.layers.length;

  // Loop through layers from the back, to preserve index
  // of remaining layers when we remove one
  for (var ii = layerCount - 1; ii >= 0; ii--) {
    var targetLayer = targetDocument.layers[ii];
    var layerName = new String(targetLayer.name);
    if (layerName.indexOf("Karışım") == 0) {
      targetDocument.layers[ii].remove();
      layersDeleted++;
    }
  }
}

 

 

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
New Here ,
Nov 15, 2022 Nov 15, 2022

Copy link to clipboard

Copied

I'm trying to use a script for illustrator 2023 to delete a layer named "o" for individual documents.

What could I use for that?

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
Advisor ,
Nov 15, 2022 Nov 15, 2022

Copy link to clipboard

Copied

LATEST

You can change the name of the layer you want to delete in line 1.

var targetName = "o";
recurse(app.activeDocument.layers);
function recurse(_layers) {
    for (var i = _layers.length - 1; i > -1 ; i--) {
        if (_layers[i].name == targetName) {
            _layers[i].remove();
            continue;
        }
        recurse(_layers[i].layers);
    }
}

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