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

Clean script from redundant lines

Explorer ,
Dec 12, 2022 Dec 12, 2022

Copy link to clipboard

Copied

Hello everyone, i've got a script made of a couple of script merged together.

I was wondering if some lines of code could me removed to make it cleaner.

Maybe i can get rid of some "var"? I'm not reall familiar with scripting...

 

This is the script:

 

var docRef = app.activeDocument; 
var layers = docRef.layers;  
var myLayer = layers["LEVEL"]; //this defines the layer that you want to get the selection from  
var text = myLayer.textFrames["INDEX"].contents;
var design = myLayer.pageItems["GROUP"];


// here there was some part of the script, it was long and not needed for the purpouse of this post so i get rid of it.


var group = activeDocument.groupItems.getByName("GROUP");
var names = ["FIRST", "SECOND", "THIRD"];
var selectedElements = [];
var elements = group.pageItems;
for (var i = 0; i < elements.length; i++) {
  var element = elements[i];
  if (names.indexOf(element.name) != -1) {
    selectedElements.push(element);
  }
}
for (var i = 0; i < selectedElements.length; i++) {
  selectedElements[i].selected = true;
}
selection = app.activeDocument.selection;
for (i = 0; i < selection.length; i++) {
  selection[i].hidden = true;
}

 

Thanks in advance!

TOPICS
Scripting

Views

1.0K

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

correct answers 1 Correct answer

Community Expert , Dec 12, 2022 Dec 12, 2022

You will not find indexOf() method in jsx script. You need to include the proptotype of the indexOf() method. Not sure what exactly you want to achieve. If you can explain better with some screenshot that will be helpful. Also, you can try following version that includes prototype of the indexOf()

 

if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function (searchElement /*, fromIndex */) {
        "use strict";
        if (this == null) {
            throw new TypeError();
        
...

Likes

Translate

Translate
Guide ,
Dec 12, 2022 Dec 12, 2022

Copy link to clipboard

Copied

Does it work?

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 ,
Dec 12, 2022 Dec 12, 2022

Copy link to clipboard

Copied

Thanks for replying @Met1 .

It seems to work but i batch execute it and on every file it gives me an error (Error 24: names.indexOf is not a funcion):

 

error.JPG

 

 

 

 

 

 

I press ok and the script keep going on the next document and so on..

The result seems to be ok but i don't know why this error.

Any idea?

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
Community Expert ,
Dec 12, 2022 Dec 12, 2022

Copy link to clipboard

Copied

You will not find indexOf() method in jsx script. You need to include the proptotype of the indexOf() method. Not sure what exactly you want to achieve. If you can explain better with some screenshot that will be helpful. Also, you can try following version that includes prototype of the indexOf()

 

if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function (searchElement /*, fromIndex */) {
        "use strict";
        if (this == null) {
            throw new TypeError();
        }
        var t = Object(this);
        var len = t.length >>> 0;
        if (len === 0) {
            return -1;
        }
        var n = 0;
        if (arguments.length > 0) {
            n = Number(arguments[1]);
            if (n != n) { // shortcut for verifying if its NaN  
                n = 0;
            } else if (n != 0 && n != Infinity && n != -Infinity) {
                n = (n > 0 || -1) * Math.floor(Math.abs(n));
            }
        }
        if (n >= len) {
            return -1;
        }
        var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
        for (; k < len; k++) {
            if (k in t && t[k] === searchElement) {
                return k;
            }
        }
        return -1;
    }
}

var docRef = app.activeDocument;
var layers = docRef.layers;
var myLayer = layers["LEVEL"]; //this defines the layer that you want to get the selection from  
var text = myLayer.textFrames["INDEX"].contents;
var design = myLayer.pageItems["GROUP"];


// here there was some part of the script, it was long and not needed for the purpouse of this post so i get rid of it.


var group = activeDocument.groupItems.getByName("GROUP");
var names = ["FIRST", "SECOND", "THIRD"];
var selectedElements = [];
for (var i = 0; i < group.pageItems.length; i++) {
    var element = group.pageItems[i];
    try {
        if (names.indexOf(element.name) != -1) {
            selectedElements.push(element);
        }
    } catch (e) { }
}

for (var i = 0; i < selectedElements.length; i++) {
    selectedElements[i].selected = true;
}

for (i = 0; i < app.activeDocument.selection.length; i++) {
    app.activeDocument.selection[i].hidden = true;
}

 

Best regards

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 ,
Dec 12, 2022 Dec 12, 2022

Copy link to clipboard

Copied

Sorry i didn't noticed this message of yours!

It works! I place it at the end of the big script but without this part:

 

var docRef = app.activeDocument;
var layers = docRef.layers;
var myLayer = layers["LEVEL"]; //this defines the layer that you want to get the selection from  
var text = myLayer.textFrames["INDEX"].contents;
var design = myLayer.pageItems["GROUP"];

 

cause it was already, identycal, at the beginning of the script.

 

So i guess that also solves the question: how to get rid of redundant lines!

Thank you very much for this!

 

I have a question, what is a "prototype" and why is needed?

Anyway, thanks again to all!

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 Expert ,
Dec 12, 2022 Dec 12, 2022

Copy link to clipboard

Copied

jsx scripting is old way of scripting, so some of the String array or more methods are not available directly in API of jsx scripting. So you need to include polyfill of those methods. A polyfill is a piece of code (usually JavaScript on the Web) used to provide modern functionality on older browsers that do not natively support it.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf

 

Best regards

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 Expert ,
Dec 12, 2022 Dec 12, 2022

Copy link to clipboard

Copied

What do you think what exactly your merged script should do?

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 ,
Dec 12, 2022 Dec 12, 2022

Copy link to clipboard

Copied

Ops sorry, once i opened the files i recognize it didn't actually woked. I didn't noticed it from thumbnils cause, to answer to @pixxxelschubser, the script should select and then hide objects FIRST, SECOND and THIRD, which are really thin lines that i didn't noticed they still are there...

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 ,
Dec 12, 2022 Dec 12, 2022

Copy link to clipboard

Copied

I did one more test, not batch executing but with only one file: it works.

Is the "batch" incompatible with this script?

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 Expert ,
Dec 12, 2022 Dec 12, 2022

Copy link to clipboard

Copied

Hi @Magenta s.r.l. 

Irrespective of what script doing, what exactly you want to achive? Run on multiple documents?

Best regards

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 ,
Dec 12, 2022 Dec 12, 2022

Copy link to clipboard

Copied

Hi @Charu Rajput thanks for replying.

What i want to achieve: i want to save this script into an action and execute it to multiple documents in the same folder (via "barch" menu on action panel.

 

I tried another test: i made a new script with only the last part of the script (the part that should hide those three objects) and put it into an action. I batch execute it: same error.

I swear it worked the firs time.. 😞

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 Expert ,
Dec 12, 2022 Dec 12, 2022

Copy link to clipboard

Copied

@Magenta s.r.l. , If you want to run multiple document without actions, you can use the following version.

if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function (searchElement /*, fromIndex */) {
        "use strict";
        if (this == null) {
            throw new TypeError();
        }
        var t = Object(this);
        var len = t.length >>> 0;
        if (len === 0) {
            return -1;
        }
        var n = 0;
        if (arguments.length > 0) {
            n = Number(arguments[1]);
            if (n != n) { // shortcut for verifying if its NaN  
                n = 0;
            } else if (n != 0 && n != Infinity && n != -Infinity) {
                n = (n > 0 || -1) * Math.floor(Math.abs(n));
            }
        }
        if (n >= len) {
            return -1;
        }
        var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
        for (; k < len; k++) {
            if (k in t && t[k] === searchElement) {
                return k;
            }
        }
        return -1;
    }
}

var folder = Folder('~/Desktop/Test');      // Your folder path where multiple ai file exists
var _files = folder.getFiles('*.ai');   // It will get all ai files from the above folder

for (var f = 0; f < _files.length; f++) {       //It will loop all files and open in Illustartor and perform following operation
    var docRef = app.open(File(_files[f]));
    var myLayer = docRef.layers["LEVEL"];

    var group = activeDocument.groupItems.getByName("GROUP");
    var names = ["FIRST", "SECOND", "THIRD"];
    var selectedElements = [];
    for (var i = 0; i < group.pageItems.length; i++) {
        var element = group.pageItems[i];
        try {
            if (names.indexOf(element.name) != -1) {
                selectedElements.push(element);
            }
        } catch (e) { }
    }

    for (var i = 0; i < selectedElements.length; i++) {
        selectedElements[i].selected = true;
    }

    for (i = 0; i < app.activeDocument.selection.length; i++) {
        app.activeDocument.selection[i].hidden = true;
    }

    //You can add a code to save and close the document here if required.
}

This above script will open the all ai files from the path that you will specify for the folder, does the same operation of hiding the items.

Best regards

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 ,
Dec 12, 2022 Dec 12, 2022

Copy link to clipboard

Copied

Hi @Charu Rajput  and thanks again for your tips and codes.

The folder will change at every work so i don't think this second script will fit.

I put the script into an action to batch execute it and seems to work just fine.

Do you think there's a bettere way to do it?

Best regards.

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 Expert ,
Dec 12, 2022 Dec 12, 2022

Copy link to clipboard

Copied

Yes, Batch will server your porpose. And for another approach, you can ask the the folder location from the user, instead of hard code path.

if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function (searchElement /*, fromIndex */) {
        "use strict";
        if (this == null) {
            throw new TypeError();
        }
        var t = Object(this);
        var len = t.length >>> 0;
        if (len === 0) {
            return -1;
        }
        var n = 0;
        if (arguments.length > 0) {
            n = Number(arguments[1]);
            if (n != n) { // shortcut for verifying if its NaN  
                n = 0;
            } else if (n != 0 && n != Infinity && n != -Infinity) {
                n = (n > 0 || -1) * Math.floor(Math.abs(n));
            }
        }
        if (n >= len) {
            return -1;
        }
        var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
        for (; k < len; k++) {
            if (k in t && t[k] === searchElement) {
                return k;
            }
        }
        return -1;
    }
}

var folder = Folder.selectDialog('Select folder');      // Ask for folder location, user will choose the folder here where ai files exists
if (folder) {
    var _files = folder.getFiles('*.ai');   // It will get all ai files from the above folder

    for (var f = 0; f < _files.length; f++) {       //It will loop all files and open in Illustartor and perform following operation
        var docRef = app.open(File(_files[f]));
        var myLayer = docRef.layers["LEVEL"];

        var group = activeDocument.groupItems.getByName("GROUP");
        var names = ["FIRST", "SECOND", "THIRD"];
        var selectedElements = [];
        for (var i = 0; i < group.pageItems.length; i++) {
            var element = group.pageItems[i];
            try {
                if (names.indexOf(element.name) != -1) {
                    selectedElements.push(element);
                }
            } catch (e) { }
        }

        for (var i = 0; i < selectedElements.length; i++) {
            selectedElements[i].selected = true;
        }

        for (i = 0; i < app.activeDocument.selection.length; i++) {
            app.activeDocument.selection[i].hidden = true;
        }

        //You can add a code to save and close the document here if required.
    }
}
Best regards

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 Expert ,
Dec 12, 2022 Dec 12, 2022

Copy link to clipboard

Copied

Well spoken @Charu Rajput 

😀

 

But I really think there are better ways to hide this three items (without search them and afterwards select them and afterwards hide them)

 

@Magenta s.r.l. 

Please show us a Screenshot with your layers structure in the layers panel and These three items selected.

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 Expert ,
Dec 12, 2022 Dec 12, 2022

Copy link to clipboard

Copied

@pixxxelschubser 

Yes correct, Well I actually did not improve the logic, I just clean up variables as required.. 🙂

Best regards

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 Expert ,
Dec 12, 2022 Dec 12, 2022

Copy link to clipboard

Copied

I know.

😉

And that was well done.

Kind regards

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 ,
Dec 12, 2022 Dec 12, 2022

Copy link to clipboard

Copied

Hi @pixxxelschubser and thank you for your time.

Here's the screenshot:

 

screenshot.JPG

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Best regards.

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 Expert ,
Dec 12, 2022 Dec 12, 2022

Copy link to clipboard

Copied

Hi @Magenta s.r.l. ,

If you know there are single items with name "FISRT", "SECOND" and so on then you don't need to loop over the each page items of the GROUP,  you can directly access the items with name, but the following version will work only for single item .

 

var names = ["FIRST", "SECOND", "THIRD"];
var folder = Folder.selectDialog('Select folder');
if (folder) {
    var _files = folder.getFiles('*.ai');
    for (var f = 0; f < _files.length; f++) {
        var docRef = app.open(File(_files[f]));
        var myLayer = docRef.layers["LEVEL"];
        var group = activeDocument.groupItems.getByName("GROUP");
        for (var n = 0; n < names.length; n++) {
            try {
                var _item = group.pageItems[names[n]];
                _item.hidden = true;
            } catch (e) { }
        }
    }
}

 

 

If there are multiple items with same name and you want to hide them all, then use below version

 

var names = ["FIRST", "SECOND", "THIRD"];
var folder = Folder.selectDialog('Select folder');
if (folder) {
    var _files = folder.getFiles('*.ai');
    for (var f = 0; f < _files.length; f++) {
        var docRef = app.open(File(_files[f]));
        var myLayer = docRef.layers["LEVEL"];
        var group = activeDocument.groupItems.getByName("GROUP");
        for (var i = 0; i < group.pageItems.length; i++) {
            var element = group.pageItems[i];
            try {
                if (names.indexOf(element.name) != -1) {
                    element.hidden = true;
                }
            } catch (e) { }
        }
    }
}

 

In short summary, you don't need to select the items to hide, unless you are not doing more behind the scenes..

Thanks to @pixxxelschubser ,  you get clean version of the script.. 🙂

 

Best regards

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 Expert ,
Dec 12, 2022 Dec 12, 2022

Copy link to clipboard

Copied

Hi @Charu Rajput 
This is too great an honour. @Magenta s.r.l. owes it all to you.

 

A small question about this:

How do you like this snippet (instead of IndexOf)?

var docRef = app.activeDocument;
var pgI = docRef.layers["LEVELS"].groupItems["GROUP"].pageItems;
var reg = /(?i)(first|second|third)/;

alert(pgI[0].name.match(reg));
alert(pgI[1].name.match(reg));
alert(pgI[2].name.match(reg));

 

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 Expert ,
Dec 13, 2022 Dec 13, 2022

Copy link to clipboard

Copied

@pixxxelschubser - Awesome... 8) 👏 

I never think of regular expression at first, unless I really need it😊

Best regards

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 Expert ,
Dec 13, 2022 Dec 13, 2022

Copy link to clipboard

Copied

LATEST

Before I solve a problem, I very often first think about possible alternatives. This almost always helps to find a simpler and more effective solution.

😉

 

But that's exactly why I always admire your quick and uncomplicated help. With me it usually takes longer.

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