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

[JS] Check if character is an inline frame

Enthusiast ,
Feb 11, 2008 Feb 11, 2008

Copy link to clipboard

Copied

How would if check if a character is an inline frame?

if (myFindsB[0].parent.characters[myIndex +1].contents == inlineFrame)
TOPICS
Scripting

Views

2.8K

Translate

Translate

Report

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 ,
Feb 11, 2008 Feb 11, 2008

Copy link to clipboard

Copied

In an inline,
>myChar.parentTextFrames[0].parent

is a character. When the character is not in an inline, its parentTextframe's parent is a page.

Peter

Votes

Translate

Translate

Report

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 Beginner ,
Feb 12, 2008 Feb 12, 2008

Copy link to clipboard

Copied

in VB there is TypeName function

if TypeName(mySel.Parent)="Character" then
end if

in JS it is called "mySel.parent.constructor.name" ??

robin

--
www.adobescripts.com

Votes

Translate

Translate

Report

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 ,
Feb 12, 2008 Feb 12, 2008

Copy link to clipboard

Copied

Robin,

>in JS it is called "mySel.parent.constructor.name"?

Right -- you would query with something like
>if ( myChar.parentTextFrames[0].parent.constructor.name == "Character")

or
>if ( myChar.parentTextFrames[0].parent typeOf Character)

Peter

Votes

Translate

Translate

Report

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
Participant ,
Feb 12, 2008 Feb 12, 2008

Copy link to clipboard

Copied

Peter,

I never realized that typeOf was a JS operator, but it doesn't seem to work the way you have it there. I'd use instanceOf in that case.

I did some timing tests the other day and discovered that instanceOf is 40% faster than using constructor.name -- although, of course, both are very fast, so it hardly amounts to a hill of beans.

Dave

Votes

Translate

Translate

Report

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
Participant ,
Feb 12, 2008 Feb 12, 2008

Copy link to clipboard

Copied

typeof (all lowercase, by the way) is used like this:

typeof app.selection[0]

But it only returns "Object" for a any kind of object, not the specific kind.

instanceof (also all lowercase) can be used like this:

if (myChar.parentTextFrames[0].parent instanceof Character)

etc.

Dave

Votes

Translate

Translate

Report

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 ,
Feb 12, 2008 Feb 12, 2008

Copy link to clipboard

Copied

Of course, instanceof! Thanks. But typeof is a JS operator, though of limited use, it seems to me, as it distinguishes just string, number, and object:

x = 'abc';
typeof x

returns 'string'. And typeof x == 'string' retrurns true.

I know that instanceof is quicker than constructor.name -- we talked about that here a few months ago. You then decided to stick with constructor.name because instanceof doesn't always work. And as you say, you notice the difference only when constructor.name is called hundreds of times.

Peter

Votes

Translate

Translate

Report

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
Explorer ,
Feb 12, 2008 Feb 12, 2008

Copy link to clipboard

Copied

There is one very good reason (for me, at least) to not use x.constructor.name.
If you have
function f(){}
x = new f();

everything works as expected (x.constructor.name == 'f).

But if you have:
f = function(){}
x = new f();

x.constructor.name returns 'anonymous' and not 'f'. Using the first form is
usually ok, but if you are send code/data back and forth to Bridge or other
CS2/3 apps, there is no way to force the scope of the function/class to be at
the global level, which you frequently need to do. Using something like
instanceof or a 'typename' property generally works out better as a way of
determining the underlying class in this situation.

-X

Votes

Translate

Translate

Report

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 ,
Feb 12, 2008 Feb 12, 2008

Copy link to clipboard

Copied

We were writing at cross-purposes...

Votes

Translate

Translate

Report

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
Enthusiast ,
Feb 12, 2008 Feb 12, 2008

Copy link to clipboard

Copied

Hi guys,

I just tried:

if(myFindsB[0].parent.characters[myIndex+1].parentTextFrames[0].parent
instanceof Character)

and I am getting an object is invalid error. does there have to be a ==
true after instanceof Character

BTW, did you guys know that a character has a different index relative
to the document as opposed to relative to the story? I thought the index
is always counted form the first character in the story and I couldn't
figure out why I was getting the wrong character.

Votes

Translate

Translate

Report

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
Enthusiast ,
Feb 12, 2008 Feb 12, 2008

Copy link to clipboard

Copied

One second, I'm doing something else wrong. How do you get the last character in myFinds relative to the story?

This is obviously not working becuse it is giving me the last character in the story:

var myIndex = myFindsB[0].parent.characters[-1].index;

Votes

Translate

Translate

Report

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 ,
Feb 12, 2008 Feb 12, 2008

Copy link to clipboard

Copied

Fred,

If you try
>if(myFindsB[0].parent.characters[myIndex+1].parentTextFrames[0].parent
instanceof Character)

when myIndex is the last position in a story, then you would get an error.
>does there have to be a == true after instanceof Character

No, == isn't used with instanceof.

Peter

Votes

Translate

Translate

Report

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
Enthusiast ,
Feb 12, 2008 Feb 12, 2008

Copy link to clipboard

Copied

Right, so how do I get the index of the last character in myFinds
relative to the story?

Votes

Translate

Translate

Report

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 ,
Feb 12, 2008 Feb 12, 2008

Copy link to clipboard

Copied

>How do you get the last character in myFinds relative to the story?

myFindsB[0].characters[-1].index

Peter

Votes

Translate

Translate

Report

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
Enthusiast ,
Feb 12, 2008 Feb 12, 2008

Copy link to clipboard

Copied

I don't know this isn't working for me.

Should this return true?


myIndex = myFindsB[0].characters[0].index

myFindsB[0].characters[0].index == myFindsB[0].parent.characters[myIndex]

Votes

Translate

Translate

Report

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 ,
Feb 12, 2008 Feb 12, 2008

Copy link to clipboard

Copied

We're writing at cross-purposes too!

Votes

Translate

Translate

Report

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 ,
Feb 12, 2008 Feb 12, 2008

Copy link to clipboard

Copied

>myFindsB[0].characters[0].index

is the index the first character of myFindsB relative to the beginning of the story.
>myFindsB[0].parent.characters[myIndex]

is the index of the first character of the story, because myFindsB[0].parent is a story.

Peter

Votes

Translate

Translate

Report

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
Enthusiast ,
Feb 12, 2008 Feb 12, 2008

Copy link to clipboard

Copied

> myFindsB[0].parent.characters[myIndex]
>
>
>
>
> is the index of the first character of the story, because myFindsB[0].parent is a story.

Why? myIndex does not = 0

Votes

Translate

Translate

Report

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 ,
Feb 12, 2008 Feb 12, 2008

Copy link to clipboard

Copied

xbytor,

> There is one very good reason (for me, at least) to not use x.constructor.name

So there are circumstances in which constructor.name doesn't work. But there are also circumstances where instanceof doesn't work, though i can't remember now which they were. It'd be nice if there is one that always works!

Peter

Votes

Translate

Translate

Report

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
Explorer ,
Feb 12, 2008 Feb 12, 2008

Copy link to clipboard

Copied

Peter Kahrel wrote:
>
> So there are circumstances in which constructor.name doesn't work. But there are also circumstances where instanceof doesn't work, though i can't remember now which they were.

instanceof does not recognize inheritance hierarchies. Superclass/subclass
relationships are difficult to work with in JS because that's not how the
underlying JS type system really works. There are some JS libs out there that
add this kind of capability, but it is via add-on methods and properties and not
seamless at all. Coming from programming in Java, it took me at least a year to
really grok how JS really works to the point where I could comfortably do any
kind of meaningful OO work.


> It'd be nice if there is one that always works!

Ultimately it depends on the context. You have just have to _know_ be it by
testing or by documentation. I use a relatively consistent coding style so that
I've reduced the possibility of this blindsiding me.

The next rev of JS is supposed to address some of these issues.

-X

Votes

Translate

Translate

Report

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
LEGEND ,
Feb 12, 2008 Feb 12, 2008

Copy link to clipboard

Copied

Peter,

instanceof always works if the object was created as an object (using
"new"). If it is a literal, it won't work.

for example: string = 'test'; if(string instanceof String) -- returns false.

so does: string = String('test');if(string instanceof String)

However, string = new String('test');if(string instanceof String) --
returns true.

The same applies to Number literals and Booleans. These are the only
instances that I know of in which instanceof does not work.

Harbs

Votes

Translate

Translate

Report

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
Enthusiast ,
Feb 12, 2008 Feb 12, 2008

Copy link to clipboard

Copied

Ok, I just tried this and it does in fact return false. What do I have to do to have it return true?

myIndex = myFindsB[0].characters[0].index

myFindsB[0].characters[0].index == myFindsB[0].parent.characters[myIndex]

Votes

Translate

Translate

Report

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 ,
Feb 12, 2008 Feb 12, 2008

Copy link to clipboard

Copied

>What do I have to do to have it return true?

What the comparison tells you if you get true is that myFindsB[0] is the first word in its story. What you have to do then I don't know.

Peter

Votes

Translate

Translate

Report

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
Enthusiast ,
Feb 12, 2008 Feb 12, 2008

Copy link to clipboard

Copied

I apologize for my confusion here, but let's go back to square one:

myIndex = myFindsB[0].characters[0].index

What will this return?

Votes

Translate

Translate

Report

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 ,
Feb 12, 2008 Feb 12, 2008

Copy link to clipboard

Copied

Not at all. It returns the index of the first character of myFindsB[0], measered from the beginning of the story in which it occurs.

Votes

Translate

Translate

Report

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