Illustrator Script to Remove Sublayers By Name Automatically

New Here ,
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

1.5K

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Valorous Hero ,
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
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Community Beginner ,
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
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Explorer ,
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
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Valorous Hero ,
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
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Explorer ,
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
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Contributor ,
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
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
BRODZELi LATEST
Community Beginner ,
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
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more