0
[JS] Check if character is an inline frame
Enthusiast
,
/t5/indesign-discussions/js-check-if-character-is-an-inline-frame/td-p/1138949
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)
if (myFindsB[0].parent.characters[myIndex +1].contents == inlineFrame)
TOPICS
Scripting
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Community Expert
,
/t5/indesign-discussions/js-check-if-character-is-an-inline-frame/m-p/1138950#M279856
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
>myChar.parentTextFrames[0].parent
is a character. When the character is not in an inline, its parentTextframe's parent is a page.
Peter
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Community Beginner
,
/t5/indesign-discussions/js-check-if-character-is-an-inline-frame/m-p/1138951#M279857
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
if TypeName(mySel.Parent)="Character" then
end if
in JS it is called "mySel.parent.constructor.name" ??
robin
--
www.adobescripts.com
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Community Expert
,
/t5/indesign-discussions/js-check-if-character-is-an-inline-frame/m-p/1138952#M279858
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
>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
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Participant
,
/t5/indesign-discussions/js-check-if-character-is-an-inline-frame/m-p/1138953#M279859
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
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
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Participant
,
/t5/indesign-discussions/js-check-if-character-is-an-inline-frame/m-p/1138954#M279861
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
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
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Community Expert
,
/t5/indesign-discussions/js-check-if-character-is-an-inline-frame/m-p/1138955#M279863
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
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
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Explorer
,
/t5/indesign-discussions/js-check-if-character-is-an-inline-frame/m-p/1138963#M279872
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
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
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Community Expert
,
/t5/indesign-discussions/js-check-if-character-is-an-inline-frame/m-p/1138956#M279865
Feb 12, 2008
Feb 12, 2008
Copy link to clipboard
Copied
We were writing at cross-purposes...
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Fred Goldman
AUTHOR
Enthusiast
,
/t5/indesign-discussions/js-check-if-character-is-an-inline-frame/m-p/1138957#M279866
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.
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.
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Fred Goldman
AUTHOR
Enthusiast
,
/t5/indesign-discussions/js-check-if-character-is-an-inline-frame/m-p/1138958#M279867
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;
This is obviously not working becuse it is giving me the last character in the story:
var myIndex = myFindsB[0].parent.characters[-1].index;
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Community Expert
,
/t5/indesign-discussions/js-check-if-character-is-an-inline-frame/m-p/1138959#M279868
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
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
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Fred Goldman
AUTHOR
Enthusiast
,
/t5/indesign-discussions/js-check-if-character-is-an-inline-frame/m-p/1138961#M279870
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?
relative to the story?
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Community Expert
,
/t5/indesign-discussions/js-check-if-character-is-an-inline-frame/m-p/1138960#M279869
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
myFindsB[0].characters[-1].index
Peter
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Fred Goldman
AUTHOR
Enthusiast
,
/t5/indesign-discussions/js-check-if-character-is-an-inline-frame/m-p/1138964#M279873
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]
Should this return true?
myIndex = myFindsB[0].characters[0].index
myFindsB[0].characters[0].index == myFindsB[0].parent.characters[myIndex]
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Community Expert
,
/t5/indesign-discussions/js-check-if-character-is-an-inline-frame/m-p/1138962#M279871
Feb 12, 2008
Feb 12, 2008
Copy link to clipboard
Copied
We're writing at cross-purposes too!
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Community Expert
,
/t5/indesign-discussions/js-check-if-character-is-an-inline-frame/m-p/1138965#M279874
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
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
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Fred Goldman
AUTHOR
Enthusiast
,
/t5/indesign-discussions/js-check-if-character-is-an-inline-frame/m-p/1138967#M279876
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
>
>
>
>
> is the index of the first character of the story, because myFindsB[0].parent is a story.
Why? myIndex does not = 0
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Community Expert
,
/t5/indesign-discussions/js-check-if-character-is-an-inline-frame/m-p/1138966#M279875
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
> 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
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Explorer
,
/t5/indesign-discussions/js-check-if-character-is-an-inline-frame/m-p/1138980#M279891
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
>
> 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
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
LEGEND
,
/t5/indesign-discussions/js-check-if-character-is-an-inline-frame/m-p/1138981#M279892
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
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
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Fred Goldman
AUTHOR
Enthusiast
,
/t5/indesign-discussions/js-check-if-character-is-an-inline-frame/m-p/1138968#M279877
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]
myIndex = myFindsB[0].characters[0].index
myFindsB[0].characters[0].index == myFindsB[0].parent.characters[myIndex]
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Community Expert
,
/t5/indesign-discussions/js-check-if-character-is-an-inline-frame/m-p/1138969#M279880
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
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
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Fred Goldman
AUTHOR
Enthusiast
,
/t5/indesign-discussions/js-check-if-character-is-an-inline-frame/m-p/1138970#M279881
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?
myIndex = myFindsB[0].characters[0].index
What will this return?
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Community Expert
,
/t5/indesign-discussions/js-check-if-character-is-an-inline-frame/m-p/1138971#M279882
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.
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more


-
- 1
- 2