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.
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();
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!
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);
}
}
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();
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.
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?
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.
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.
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++;
}
}
}
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?
Copy link to clipboard
Copied
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);
}
}