Copy link to clipboard
Copied
Hi guys, my name is Inácio and I'm Brazilian, sorry for my English. I'm also new to scripting Adobe software and need help with a method that doesn't seem to be working well. I would be grateful if someone could tell me what is wrong, or provide me with an alternative, to the following code:
delEmptyArtboards()
function delEmptyArtboards(){
if (app.documents.length>0){
var doc=app.activeDocument
var artbs=doc.artboards
var log='Verified ' + artbs.length + ' artboards:'
for(i=0;i<artbs.length;i++){
doc.selection=null
doc.artboards.setActiveArtboardIndex(i)
sel=doc.selectObjectsOnActiveArtboard()
redraw()
log=log+'artboard ' + [i] + ' ->' + sel.length + ';\n'
if(sel==false){
doc.artboards.remove(i)
//or artbs[i].remove()
redraw()
}
}
return alert(log)
}else{
alert('Is necessary any document for continue')
}
}
Hi,
You can try the following snippet either to delete the empty layers or empty artboards. Following snippet has both methods
delEmptyArtboards()
function delEmptyArtboards() {
if (app.documents.length > 0) {
var doc = app.activeDocument;
var artbs = doc.artboards;
for (i = artbs.length - 1; i >= 0; i--) {
app.selection = null;
doc.artboards.setActiveArtboardIndex(i)
doc.selectObjectsOnActiveArtboard()
redraw()
...
Hi,
In your statement
sel=doc.selectObjectsOnActiveArtboard()
sel have boolean value(true) it does not return the selected elements. So, the correct version of the above version will be using app.selection.length instead of sel.length in log statement.
delEmptyArtboards()
function delEmptyArtboards() {
if (app.documents.length > 0) {
var doc = app.activeDocument
var artbs = doc.artboards
var log = 'Verified ' + artbs.length + ' artboards:'
for (i = artbs.len
...
Copy link to clipboard
Copied
Hi,
Would you like to remove the empty layer or empty artboards?
Copy link to clipboard
Copied
Oops! Sorry Charu, I got confused by the illustrator's terms. I would like to remove empty artboards. I'll take a look at your codes, thanks for now.
Copy link to clipboard
Copied
Hi,
You can try the following snippet either to delete the empty layers or empty artboards. Following snippet has both methods
delEmptyArtboards()
function delEmptyArtboards() {
if (app.documents.length > 0) {
var doc = app.activeDocument;
var artbs = doc.artboards;
for (i = artbs.length - 1; i >= 0; i--) {
app.selection = null;
doc.artboards.setActiveArtboardIndex(i)
doc.selectObjectsOnActiveArtboard()
redraw()
if (app.selection.length == 0) {
doc.artboards.remove(i)
}
}
app.selection = null;
} else {
alert('Is necessary any document for continue')
}
}
function delEmptyLayers() {
if (app.documents.length > 0) {
var doc = app.activeDocument;
var layers = doc.layers;
for (var i = layers.length - 1; i >= 0; i--) {
if (layers[i].pageItems.length == 0) {
layers[i].remove();
}
}
} else {
alert('Is necessary any document for continue')
}
}
I hope it helps.
Copy link to clipboard
Copied
Charu, first of all I would like to thank you for your availability and promptness. Your code was perfect after the "for" and no "if" changes, thanks for that. But the problem with the "alert" still remains. When I try to output the user, Illustrator crashes. Could you help me with that too?
delEmptyArtboards()
function delEmptyArtboards(){
if (app.documents.length>0){
var doc=app.activeDocument
var artbs=doc.artboards
var log='Verified ' + artbs.length + ' artboards:'
for(i=artbs.length-1;i>=0;i--){ //perfect change
doc.selection=null
doc.artboards.setActiveArtboardIndex(i)
sel=doc.selectObjectsOnActiveArtboard()
redraw()
log=log+'artboard ' + [i] + ' ->' + sel.length + ';\n'
if (app.selection.length == 0) { //perfect change
doc.artboards.remove(i)
redraw()
}
}
alert(log) // this function is crashing the illustrator
}else{
alert('Is necessary any document for continue')
}
}
Copy link to clipboard
Copied
Hi,
In your statement
sel=doc.selectObjectsOnActiveArtboard()
sel have boolean value(true) it does not return the selected elements. So, the correct version of the above version will be using app.selection.length instead of sel.length in log statement.
delEmptyArtboards()
function delEmptyArtboards() {
if (app.documents.length > 0) {
var doc = app.activeDocument
var artbs = doc.artboards
var log = 'Verified ' + artbs.length + ' artboards:'
for (i = artbs.length - 1; i >= 0; i--) { //perfect change
doc.selection = null
doc.artboards.setActiveArtboardIndex(i)
doc.selectObjectsOnActiveArtboard();
redraw()
log = log + 'artboard ' + [i] + ' ->' + app.selection.length + ';\n'
if (app.selection.length == 0) { //perfect change
doc.artboards.remove(i)
redraw()
}
}
alert(log) // this function is crashing the illustrator
} else {
alert('Is necessary any document for continue')
}
}
I hope this helps you.
Copy link to clipboard
Copied
Hi Charu, once again, your perception of the problem is correct. I made the correction here.
Thanks!!
But it still doesn't work. I believe the windows version of Illustrator 26.0 has a problem with the "setActiveArtboardIndex()" method, because every time I invoke it, the software crashes. I'll see if I can check this in another Illustrator install, if I can't, I'll make a new post with the particular question.
Thanks again!!
Copy link to clipboard
Copied
@Charu Rajput's answer above (https://community.adobe.com/t5/illustrator-discussions/how-delete-empty-layers-with-a-methods-setact...) works for me on Windows without any problems.
Do you have an example document that is causing the problems?
Copy link to clipboard
Copied
Hi guys, pixxxelschubser and Charu Rajput, I was programming on my notebook, after I tested the scripts that were crashing on the desktop they worked perfectly. Therefore, the installation of the illustrator on the notebook is having problems.
Thanks to the 2 for your promptness!! All right from now on.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now