Skip to main content
Known Participant
December 12, 2022
Answered

Clean script from redundant lines

  • December 12, 2022
  • 3 replies
  • 3163 views

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!

This topic has been closed for replies.
Correct answer Charu Rajput

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;
}

 

3 replies

Charu Rajput
Community Expert
Community Expert
December 12, 2022

Hi @Delresto 

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

Best regards
DelrestoAuthor
Known Participant
December 12, 2022

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.. 😞

Charu Rajput
Community Expert
Community Expert
December 12, 2022

@Delresto , 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
pixxxelschubser
Community Expert
Community Expert
December 12, 2022

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

DelrestoAuthor
Known Participant
December 12, 2022

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

Met1
Legend
December 12, 2022

Does it work?

DelrestoAuthor
Known Participant
December 12, 2022

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):

 

 

 

 

 

 

 

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!

Charu Rajput
Community Expert
Charu RajputCommunity ExpertCorrect answer
Community Expert
December 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();
        }
        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