Skip to main content
Inspiring
Oct 4, 2024 7:27 PM UTC
Answered

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

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;
  }

 

This topic is closed to new replies. Start a new post to keep the conversation going.
renél80416020
renél80416020Correct answer
Inspiring
Oct 4, 2024 8:09 PM UTC

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

 

 

 

4 replies

Inspiring
Oct 5, 2024 6:35 AM UTC
the same question was asked here 

 

Translate
m1b
Community Expert
Community Expert
Oct 4, 2024 11:41 PM UTC

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
Charu Rajput
Community Expert
Community Expert
Oct 4, 2024 10:53 PM UTC
var doc = app.activeDocument;
var textRef = doc.selection[0]; // assume text frame is selected 
if (textRef.constructor == TextFrame) {
    alert("It is a textframe");
}
Translate
Best regards
renél80416020
renél80416020Correct answer
Inspiring
Oct 4, 2024 8:09 PM UTC

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