Skip to main content
Bedazzled532
Inspiring
September 19, 2023
Answered

Anchor a text frame at insertion points

  • September 19, 2023
  • 4 replies
  • 3398 views

Hello

 

I am getting "object is invalid" error in the following code at line "tf = point.textFrames.add();"

 

Actually, I am looking for a unicode value "myUnicode" and storing its insertion points in an array.

 

Then at that point I wish to anchor a textframe (just after myUnicode char) with the contents the of "source" (1st para which is few chars only..like 4-5 chars). For the second match, add another textframe with second paragraph. "Source" has 17 paragraphs (each paragraph is few chars long only). "destination" has 17 "myUnicode" characters. 

Any suggestions where am I doing wrong ? 

Thanks

 

var myDoc = app.documents[0];
var myNum = [];
var iPoint = [];
var myUnicode = '\\x{002a}';
var myFrame = myDoc.textFrames.itemByName('source');


var myDestFrame = myDoc.textFrames.itemByName('destination');
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = myUnicode;
var myFound = myDestFrame.parentStory.findGrep();


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

iPoint.push(myFound[i].insertionPoints[0].index);

point = myFound[i].insertionPoints.item(iPoint[i]);

tf = point.textFrames.add();
    tf.properties = 
    {
      geometricBounds :[0,0,10,30],
      strokewidth: 0,
      fillColor : "None",
      name : "test"+(i+1).toString(),
      contents : txtFrame.itemByName('source').paragraphs[i].contents,
      appliedObjectStyle: doc.objectStyles.item("rukus")
    };
    tf.anchoredObjectSettings.properties = {
      anchoredPosition: AnchorPosition.ANCHORED,
      anchorPoint : AnchorPoint.TOP_RIGHT_ANCHOR,
      verticalReferencePoint : VerticallyRelativeTo.LINE_XHEIGHT
    };
}

 

This topic has been closed for replies.
Correct answer Peter Kahrel

Various problems. The error you see in 

myFound[i].insertionPoints.item(iPoint[i])

is caused by iPoint[i], which is an insertion point, but JavaScript expects a number there.

 

But you don't need the lines that start with iPoint and point -- just remove them. Then change this one

tf = point.textFrames.add();

with

tf = myFound[i].insertionPoints[1].textFrames.add();

Then change this line:

contents : txtFrame.itemByName('source').paragraphs[i].contents,

to

contents : myFrame.paragraphs[i].contents,

because txtFrame does not exist. Finally, in

appliedObjectStyle: doc.objectStyles.item("rukus")

change doc to myDoc

4 replies

Peter Kahrel
Community Expert
Peter KahrelCommunity ExpertCorrect answer
Community Expert
September 19, 2023

Various problems. The error you see in 

myFound[i].insertionPoints.item(iPoint[i])

is caused by iPoint[i], which is an insertion point, but JavaScript expects a number there.

 

But you don't need the lines that start with iPoint and point -- just remove them. Then change this one

tf = point.textFrames.add();

with

tf = myFound[i].insertionPoints[1].textFrames.add();

Then change this line:

contents : txtFrame.itemByName('source').paragraphs[i].contents,

to

contents : myFrame.paragraphs[i].contents,

because txtFrame does not exist. Finally, in

appliedObjectStyle: doc.objectStyles.item("rukus")

change doc to myDoc

Bedazzled532
Inspiring
September 19, 2023

@Peter Kahrel Thanks Peter.

I will make the modificaitons and update.

Bedazzled532
Inspiring
September 19, 2023

@Peter Kahrel Thanks Peter it works perfectly, however, the numbers are placed in reverse order. (I have attached the screenshot) Below is the modified code after your suggestions.

 
To correct the order, I changed this line "var myFound = myDestFrame.parentStory.findGrep(true)" to "var myFound = myDestFrame.parentStory.findGrep()" and change for loop to "for(i=myFound.length;i >=0 ;i--)", then it gives error "undefined is not an object" at line "tf = myFound[i].insertionPoints[1].textFrames.add();"
 
I even modified "contents : myFrame.paragraphs[i].contents" to "contents : myFrame.paragraphs[myFound.length-i].contents". This line also gives error.
 

 

var unicodeV = prompt ("", "", "Enter the unicode value where you want to anchor");

var myDoc = app.documents[0];
var myUnicode ='\\x{'+unicodeV+'}';
var myFrame = myDoc.textFrames.itemByName('source');

var myDestFrame = myDoc.textFrames.itemByName('destination');
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = myUnicode;
var myFound = myDestFrame.parentStory.findGrep(true);

for(i=0;i < myFound.length;i++){
tf = myFound[i].insertionPoints[1].textFrames.add();
    tf.properties = 
    {
      geometricBounds :[0,0,10,30],
      strokewidth: 0,
      fillColor : "None",
      name : "test"+(i+1).toString(),
  contents : myFrame.paragraphs[i].contents,
      appliedObjectStyle: myDoc.objectStyles.item("rukus")
    };
    tf.anchoredObjectSettings.properties = {
      anchoredPosition: AnchorPosition.ANCHORED,
      anchorPoint : AnchorPoint.TOP_RIGHT_ANCHOR,
      verticalReferencePoint : VerticallyRelativeTo.LINE_XHEIGHT
    };
}

 

Peter Kahrel
Community Expert
Community Expert
September 19, 2023

By the way, is your question here different from the one you posed here:

https://community.adobe.com/t5/indesign-discussions/cut-and-anchor-after-a-particular-unicode-char/m-p/14092937

 

If it isn't please remove either thread.

Bedazzled532
Inspiring
September 19, 2023

@Peter Kahrel Thanks for the reply.

Actually I was looking for a way to delete that thread, but I do not know how to access to the posts which I have made.

In the current post, the question is same but the script written is by me and I have not taken help from anyone.

From where can I access the posts. All it shows here is the notificaiton bell and an envelop icon.

Thanks

Peter Kahrel
Community Expert
Community Expert
September 19, 2023

I added a link to this thread in the other one and locked the other one.

Peter Kahrel
Community Expert
Community Expert
September 19, 2023

Change 

 

for(i=myFound.length;i >0;i--)

 

to

 

for(i=myFound.length-1; i >= 0; i--)

 

brian_p_dts
Community Expert
Community Expert
September 19, 2023

I think we want to stay 0 to n in the for loop since we're now finding the grep in reverse order.

Peter Kahrel
Community Expert
Community Expert
September 19, 2023

Oh, certainly!

brian_p_dts
Community Expert
Community Expert
September 19, 2023

Try changing this line to do the finds in reverse order, because when you add a tf to an insertion point, you change the index of everything that comes after the addition. 

 

var myFound = myDestFrame.parentStory.findGrep(true);

 

Bedazzled532
Inspiring
September 19, 2023

@brian_p_dts Thanks for the reply.

I have made the following modificaitons:

var myFound = myDestFrame.parentStory.findGrep(true);

and my for loop is now this:

for(i=myFound.length;i >0;i--)

 

I am now getting this error:

JavaScript Error!

Error Number: 21
Error String: undefined is not an object

Engine: main
File: C:\Users\shah\AppData\Roaming\Adobe\InDesign\Version 18.0-ME\en_AE\Scripts\Scripts Panel\testattach.jsx
Line: 40
Source: iPoint.push(myFound[i].insertionPoints[0].index);