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

Check if pageItem is textFrame

Community Beginner ,
Sep 08, 2010 Sep 08, 2010

How do I check, whether a pageItem is a textFrame?

TOPICS
Scripting
8.4K
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

Contributor , Apr 12, 2016 Apr 12, 2016

var arrayPageItems = app.activeDocument.pageItems.everyItem().getElements();

app.activeDocument.pageItems.everyItem().getElements(); 

    for (var i=0; i<arrayPageItems.length; i++){ 

        if (arrayPageItems == "[object TextFrame]"){

            alert("YES - TEXTFRAME");

        } else {

            alert("NO TEXTFRAME");

        }

    }

Translate
Valorous Hero ,
Sep 08, 2010 Sep 08, 2010
var thePageItem = app.selection[0];
if (thePageItem.constructor.name == "TextFrame") alert("A text frame is selected");
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
Advisor ,
Sep 08, 2010 Sep 08, 2010

Yeah, or like this:

if(app.selection[0] instanceof TextFrame)alert("It's a text frame");

--

tomaxxi

http://indisnip.wordpress.com/

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 ,
Sep 09, 2010 Sep 09, 2010

Gents, then why doesn't this work? (Neither fast nor slow )

alert (app.activeWindow.activePage.pageItems[0].constructor.name);

-- it insists it's just a PageItem. This behaves the same as instanceof, so that's not the solution:

if (app.activeWindow.activePage.pageItems[0] instanceof TextFrame)
alert ("TextFrame");
else
alert (app.activeWindow.activePage.pageItems[0].constructor.name);

also shows "PageItem" -- of course I first made sure the first page item *is*, in fact, a textframe, as this ugly construction

if (app.activeWindow.activePage.pageItems[0] == app.activeWindow.activePage.textFrames[0] )
alert ("TextFrame");
else
alert (app.activeWindow.activePage.pageItems[0].constructor.name);

*does* work.

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
Guide ,
Sep 09, 2010 Sep 09, 2010

You need to use ...pageItems[0].getElements()[0] to resolve a PageItem object. PageItem behaves as an abstract "class", like Swatch, etc.

@+

Marc

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 ,
Sep 09, 2010 Sep 09, 2010
alert (app.activeWindow.activePage.pageItems[0].getElements()[0].constructor.name);

Cool! This was something that has been bothering me for quite some time ...

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
Valorous Hero ,
Sep 09, 2010 Sep 09, 2010

This was mentioned in InDesign CS2 Scripting Guide:

When you are iterating through a collection, JavaScript insists that the type of the item is the type of the collection. When you iterate through a pageItems collection, for example, the type of each item will be PageItem, rather than Rectangle, Oval, Polygon, GraphicLine, or TextFrame. To get the specific type of an item when you are iterating through a collection of unlike items, use the getElements() method, as shown in the following example:

for(myCounter = 0; myCounter < app.activeDocument.pages.item(0).pageItems.length; myCounter ++){
     var myPageItem = app.activeDocument.pages.item(0).pageItems.item(myCounter);
     var myPageItemType = myPageItem.getElements()[0].constructor.name;
     alert(myPageItemType);
}

However since CS3 this information is missing.

Kasyan

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
Advisor ,
Sep 09, 2010 Sep 09, 2010

Hey Kasyan!

Thing is that "instanceof" has slightly faster performance than "constructor".

--

tomaxxi

http://indisnip.wordpress.com/

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
Valorous Hero ,
Sep 09, 2010 Sep 09, 2010

app.selection[0] instanceof TextFrame; — 1215 nanoseconds

app.selection[0].constructor.name == "TextFrame"; — 1543 nanoseconds

Your version is about 200 nanoseconds faster.

(One nanosecond is to one second as one second is to 31.7 years.)

Kasyan

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
Advisor ,
Sep 09, 2010 Sep 09, 2010

Not much faster

Thanks Kasyan!

--

tomaxxi

http://indisnip.wordpress.com/

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
Guide ,
Sep 09, 2010 Sep 09, 2010

Kasyan Servetsky wrote:

app.selection[0] instanceof TextFrame; — 1215 nanoseconds

app.selection[0].constructor.name == "TextFrame"; — 1543 nanoseconds

Your version is about 200 nanoseconds faster.

(One nanosecond is to one second as one second is to 31.7 years.)

Kasyan

So we can conclude that a huge loop which takes about 12 seconds using instanceof will take about 15 seconds (i.e. +3 seconds) using the constructor test. I experienced it today 🙂

@+

Marc

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
Contributor ,
Apr 12, 2016 Apr 12, 2016

var arrayPageItems = app.activeDocument.pageItems.everyItem().getElements();

app.activeDocument.pageItems.everyItem().getElements(); 

    for (var i=0; i<arrayPageItems.length; i++){ 

        if (arrayPageItems == "[object TextFrame]"){

            alert("YES - TEXTFRAME");

        } else {

            alert("NO TEXTFRAME");

        }

    }

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
Guest
Sep 22, 2016 Sep 22, 2016
LATEST

function isTextFrames()

{

var PageItems = app.activeDocument.pageItems.everyItem().getElements();  

    for (var i=0; i<PageItems.length; i++){   

        if (PageItems.constructor.name == "TextFrame"){ 

            alert("TEXTFRAME"); 

        } else { 

            alert("No TEXTFRAME"); 

        } 

    }

}

isTextFrames();

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