Error in earlier used script for anchored textboxes.

Explorer ,
Jun 07, 2021 Jun 07, 2021

Copy link to clipboard

Copied

Dear all,

i have a document of few pages but one story. It has a main text box and on many places anchored text boxes are placed.

The below script cut each anchor-text-box from its place and paste at its insertion/anchor point and delete all empty anchored boxes. But now this script showed error at this line: for (var j = 0; j < myItems.length; j++)

Could someone please help? I am using InDesign 2021.


var myDoc = app.activeDocument;

var myPages = myDoc.pages.everyItem().getElements();

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

{

{
var myPage = myPages;
// alert (myPage.length);
//Checks that the page is valid, and that it is not a master page. If either is true, skips to the next page.

if (myPage.isValid == false) continue;

if (myPage.parent instanceof MasterSpread) continue;

var myItems = myPage.allPageItems;
// alert (myPage.allPageItems.length);

for (var j = 0; j < myItems.length; j++)

{
//Current item.
var myItem = myItems;

//If myItem doesn't have a Character parent, it is not anchored.
//The first and third text frames would fail this test.

if (!(myItem.parent instanceof Character)) continue;

//We only care about text frames.

if (!(myItem instanceof TextFrame)) continue;

//I think the only way this would happen would be if you had an image or
//something else unexpected within the frame. I check for it so no content
//is inadvertently lost.

else if (myItem.texts.length > 1) continue;

//If we're still in this iteration of the loop, all qualifications are met.
//Flatten the text frame.
//I don't use layers that often so, to me, flatten makes sense. You may want
//to use a different term if there's a chance for confusion.

flattenItem(myItem);

}

}

}

 

function flattenItem(funcItem)


{

//Hold onto the anchor character.
var myParent = funcItem.parent;

//Duplicate the text from within the frame so that it appears right after the anchor.
//There may be other methods, but this works for me. I try to avoid copy/paste
//so as not to deal with any clipboard mishaps. I added a check in case of empties.

if (funcItem.texts.length > 0){funcItem.texts[0].duplicate(LocationOptions.AFTER, myParent.insertionPoints[0]);}

//Replace the anchor character itself with a space (or whatever) which also
//deletes the text frame it was anchoring.

myParent.contents = " ";

}

TOPICS
Scripting

Views

182

Likes

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

correct answers 2 Correct Answers

Enthusiast , Jun 07, 2021 Jun 07, 2021
Hello, I didn't test your code, but try changing..... //Change this line var myPage = myPages; //To var myPage = myPages[i]; Regards,Mike

Likes

Translate

Translate
Adobe Community Professional , Jun 08, 2021 Jun 08, 2021
Another similar typo that needs to be fixed is var myItem = myItems; //Change the above to the following var myItem = myItems[j]; -Manan

Likes

Translate

Translate
Enthusiast ,
Jun 07, 2021 Jun 07, 2021

Copy link to clipboard

Copied

Hello,

 

I didn't test your code, but try changing.....

 

 

//Change this line
var myPage = myPages;

//To
var myPage = myPages[i];

 

 

Regards,

Mike

Likes

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 ,
Jun 08, 2021 Jun 08, 2021

Copy link to clipboard

Copied

hi Mike,

That worked but later on I get the error here:

if (funcItem.texts.length > 0){funcItem.texts[0].duplicate(LocationOptions.AFTER, myParent.insertionPoints[0]);}

 

It seems many things has been changed.

Likes

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
Adobe Community Professional ,
Jun 08, 2021 Jun 08, 2021

Copy link to clipboard

Copied

Another similar typo that needs to be fixed is

var myItem = myItems;
//Change the above to the following
var myItem = myItems[j];

-Manan

 

Likes

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 ,
Jun 09, 2021 Jun 09, 2021

Copy link to clipboard

Copied

LATEST

Thanks Manan.

Likes

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