Copy link to clipboard
Copied
How do I check, whether a pageItem is a textFrame?
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");
}
}
Copy link to clipboard
Copied
var thePageItem = app.selection[0];
if (thePageItem.constructor.name == "TextFrame") alert("A text frame is selected");
Copy link to clipboard
Copied
Yeah, or like this:
if(app.selection[0] instanceof TextFrame)alert("It's a text frame");
--
tomaxxi
http://indisnip.wordpress.com/
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
You need to use ...pageItems[0].getElements()[0] to resolve a PageItem object. PageItem behaves as an abstract "class", like Swatch, etc.
@+
Marc
Copy link to clipboard
Copied
alert (app.activeWindow.activePage.pageItems[0].getElements()[0].constructor.name);
Cool! This was something that has been bothering me for quite some time ...
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Hey Kasyan!
Thing is that "instanceof" has slightly faster performance than "constructor".
--
tomaxxi
http://indisnip.wordpress.com/
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Not much faster
Thanks Kasyan!
--
tomaxxi
http://indisnip.wordpress.com/
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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");
}
}
Copy link to clipboard
Copied
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();
Find more inspiration, events, and resources on the new Adobe Community
Explore Now