Skip to main content
Known Participant
December 10, 2022
Answered

Move text objects inside group (but those text may or may not exist)

  • December 10, 2022
  • 1 reply
  • 621 views

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:

var text1Exists = activeDocument.textFrames.exists("TEXT1");)

 

What's going on? What am i doing wrong? Any help will be appreaciate.

Cheers!

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

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

}

 

1 reply

Charu Rajput
Community Expert
Charu RajputCommunity ExpertCorrect answer
Community Expert
December 10, 2022

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

}

 

Best regards
DelrestoAuthor
Known Participant
December 10, 2022

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!

Charu Rajput
Community Expert
Community Expert
December 10, 2022

Glad it works for you.

May be you saw in API of File and Folder where exists property exists.

Best regards