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!
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();
...
Copy link to clipboard
Copied
Does it work?
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):
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!
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;
}
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!
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
Copy link to clipboard
Copied
What do you think what exactly your merged script should do?
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...
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?
Copy link to clipboard
Copied
Irrespective of what script doing, what exactly you want to achive? Run on multiple documents?
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.. 😞
Copy link to clipboard
Copied
@Kangi Sportswear , 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.
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.
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.
}
}
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)
Please show us a Screenshot with your layers structure in the layers panel and These three items selected.
Copy link to clipboard
Copied
Yes correct, Well I actually did not improve the logic, I just clean up variables as required.. 🙂
Copy link to clipboard
Copied
I know.
😉
And that was well done.
Kind regards
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Hi @Kangi Sportswear ,
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.. 🙂
Copy link to clipboard
Copied
Hi @Charu Rajput
This is too great an honour. @Kangi Sportswear 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));
Copy link to clipboard
Copied
@pixxxelschubser - Awesome... 8) 👏
I never think of regular expression at first, unless I really need it😊
Copy link to clipboard
Copied
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.