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

Why do I get the error "TextFrame is undefined" ?

Engaged ,
Oct 04, 2024 Oct 04, 2024

Why do I get the error "TextFrame is undefined" when for instance a shape selected?

  var doc = app.activeDocument;
  var textRef = doc.selection[0]; // assume text frame is selected
  alert(textRef.typename);

  if (!(textRef instanceof TextFrame)) {
    return;
  }

 

TOPICS
Scripting
492
Translate
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

Advocate , Oct 04, 2024 Oct 04, 2024

TextFrame est considéré comme une variable dans votre script.

 

 

// JavaScript Document
  var doc = app.activeDocument;
  var objRef = doc.selection[0]; // assume text frame is selected

  if (objRef.typename == "TextFrame") {
    alert("Text found");
  }
  else alert(objRef.typename);

 

 

 

Translate
Adobe
Advocate ,
Oct 04, 2024 Oct 04, 2024

TextFrame est considéré comme une variable dans votre script.

 

 

// JavaScript Document
  var doc = app.activeDocument;
  var objRef = doc.selection[0]; // assume text frame is selected

  if (objRef.typename == "TextFrame") {
    alert("Text found");
  }
  else alert(objRef.typename);

 

 

 

Translate
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 ,
Oct 04, 2024 Oct 04, 2024
var doc = app.activeDocument;
var textRef = doc.selection[0]; // assume text frame is selected 
if (textRef.constructor == TextFrame) {
    alert("It is a textframe");
}
Best regards
Translate
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 ,
Oct 04, 2024 Oct 04, 2024

Hi @Charu Rajput, when I refer to `TextFrame` in the example you give, I get the same error as @andyf65867865, but only if I launch the script from Illustrator—if I launch it from VSCode debugger it works fine. My sample code is:

(function () {

    var textRef = app.activeDocument.selection[0];

    if (textRef.constructor === TextFrame)
        alert('yes');
    else
        alert('no');

})();

So this code gives error if run from Illustrator, but not from VSCode debugger. Is your experience different? I am on MacOS 15.0 (AI 28.7.1).

 

Anyway, the idiom I use these days is almost always

if (textRef.constructor.name === 'TextFrame')

I prefer this to `typename` because I can use it with non-DOM objects, eg. Array. (Only Illustrator's objects have a `typename`.)

- Mark

Translate
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
Engaged ,
Oct 04, 2024 Oct 04, 2024
LATEST
the same question was asked here 

 

Translate
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