Copy link to clipboard
Copied
Hello everyone,
i have this simple script
var layer1 = activeDocument.layers.getByName("LEVEL");
var group = layer1.groupItems.getByName("GROUP");
var text1 = layer1.textFrames.getByName("TEXT1");
var text2 = layer1.textFrames.getByName("TEXT2");
var mask = group.groupItems.getByName("CLIPPING MASK");
if (text1) {
player1.moveToBeginning(mask);
}
if (text2) {
number.moveToBeginning(mask);
}
It's working fine but since TEXT1 or TEXT2 may or may not exist,i want the script to keep running with no interruption nor alert. So i tried this:
var layer1 = activeDocument.layers.getByName("LEVEL");
var group = layer1.groupItems.getByName("GROUP");
var text1Exists = activeDocument.textFrames.exists("TEXT1");
var text2Exists = activeDocument.textFrames.exists("TEXT2");
if (text1Exists) {
var text1 = activeDocument.textFrames.getByName("TEXT1");
var mask = group.groupItems.getByName("CLIPPING MASK");
text1.moveToBeginning(mask);
}
if (text2Exists) {
var text2 = activeDocument.textFrames.getByName("TEXT2");
var mask = group.groupItems.getByName("mask");
text2.moveToBeginning(mask);
}
but it's not working (on Visual Studio Code the error says: no such element on this line:
What's going on? What am i doing wrong? Any help will be appreaciate.
Cheers!
Hi,
There is no exists method in Illustartor API that can tell you if its exists or not. So you need to use the getByName to get the element.
Try to use try and catch, like below
var layer1 = activeDocument.layers.getByName("LEVEL");
var group = layer1.groupItems.getByName("GROUP");
//Text 1
try {
var text1 = activeDocument.textFrames.getByName("TEXT1");
var mask = group.groupItems.getByName("CLIPPING MASK");
text1.moveToBeginning(mask);
} catch (e) { }
//Text 2
try {
var
...
Copy link to clipboard
Copied
Hi,
There is no exists method in Illustartor API that can tell you if its exists or not. So you need to use the getByName to get the element.
Try to use try and catch, like below
var layer1 = activeDocument.layers.getByName("LEVEL");
var group = layer1.groupItems.getByName("GROUP");
//Text 1
try {
var text1 = activeDocument.textFrames.getByName("TEXT1");
var mask = group.groupItems.getByName("CLIPPING MASK");
text1.moveToBeginning(mask);
} catch (e) { }
//Text 2
try {
var text2 = activeDocument.textFrames.getByName("TEXT2");
var mask = group.groupItems.getByName("mask");
text2.moveToBeginning(mask);
} catch (e) {
}
Copy link to clipboard
Copied
Thank you, it works like a charm!
I saw the method "Exists" somewhere else and i thought to tried it, i didn't know about Ai API.
Thanks a lot, i really appreciate it.
Have a nice day!
Copy link to clipboard
Copied
Glad it works for you.
May be you saw in API of File and Folder where exists property exists.