Skip to main content
Inspiring
October 4, 2024
Answered

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

  • October 4, 2024
  • 4 replies
  • 574 views

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 has been closed for replies.
Correct answer renél80416020

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
October 5, 2024
the same question was asked here 

 

m1b
Community Expert
Community Expert
October 4, 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

Charu Rajput
Community Expert
Community Expert
October 4, 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
renél80416020
renél80416020Correct answer
Inspiring
October 4, 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);