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

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

Explorer ,
Dec 10, 2022 Dec 10, 2022

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:

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

 

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

Cheers!

TOPICS
Scripting

Views

346

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 10, 2022 Dec 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 
...

Votes

Translate

Translate
Adobe
Community Expert ,
Dec 10, 2022 Dec 10, 2022

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

}

 

Best regards

Votes

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

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!

Votes

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

Copy link to clipboard

Copied

LATEST

Glad it works for you.

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

Best regards

Votes

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