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

how delete empty layers with a methods setActiveArtboardIndex and selectObjectsOnActiveArtboard

Explorer ,
Jan 04, 2022 Jan 04, 2022

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')
    }
}

 

Best regards
TOPICS
Scripting , SDK
1.0K
Translate
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 2 Correct answers

Community Expert , Jan 04, 2022 Jan 04, 2022

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()
       
...
Translate
Community Expert , Jan 05, 2022 Jan 05, 2022

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
...
Translate
Adobe
Community Expert ,
Jan 04, 2022 Jan 04, 2022

Hi,

Would you like to remove the empty layer or empty artboards?

Best regards
Translate
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 04, 2022 Jan 04, 2022

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.

Best regards
Translate
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 04, 2022 Jan 04, 2022

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.

Best regards
Translate
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 05, 2022 Jan 05, 2022

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')
    }
}
Best regards
Translate
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 05, 2022 Jan 05, 2022

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.

 

Best regards
Translate
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 06, 2022 Jan 06, 2022

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!!

Best regards
Translate
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 06, 2022 Jan 06, 2022

@Inacio Felipe 

@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?

Translate
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 06, 2022 Jan 06, 2022
LATEST

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.

Best regards
Translate
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