Troublshooting Script to replace page numbers with custom text

Community Beginner ,
Jan 08, 2022 Jan 08, 2022

Copy link to clipboard

Copied

Hello, hope someone can give me some hints how to solve the problem I have been troubshooting for severeal days now.

I need to automatically mark every spread of a document with a custom text that can be replaced later.

To do that I found a script that replaces page numbers with the text.  My master page spread contains two places with the "#" placeholder. The script uses page number calculation to replace '#' with the words like "Week_1" "Week_2" etc.  It works perfectly for the right side of the spread, yet for the left side instead of replacing existing TextFrame it creates a new one and places it in the middle of the spread. See screenshot. I tried so many things on a master page to find the reason, but I can't. Would really appreciate any suggestions on what might cause this issue and how to fix. Thank you very much!

 

Here are the screenshot and the code copy is below.

Master pages spread:

Karina_I_0-1641665683903.png

 

Document page spread after the script run: blue-circled is converted correctly, but the red-circled created a missplaced duplicate frame while leaving master frame as is:

Karina_I_1-1641665815185.png

 

Script:

 

//DESCRIPTION:Convert Page Number Placeholder '#' into Text
// based on the script from web by 'A Jongware Script 11-Dec-2011'

var allPages = app.activeDocument.pages;


for (iterate=0; iterate<allPages.length; iterate++) {
curPage = customPageNumber(allPages[iterate]);
}

function customPageNumber (aPage) {
if (aPage.appliedMaster == null) return;
keyPhrase = "#";

pageSide = (aPage.side == PageSideOptions.RIGHT_HAND) ? 1 : 0;
masterTextFrame = findMasterFrameContaining (aPage.appliedMaster, pageSide, keyPhrase);
if (masterTextFrame != null){
pageTextFrame = findByLabel(aPage.pageItems, "week number"+pageSide);
if (pageTextFrame != null) {
pageTextFrame.removeOverride();
}
pageTextFrame = masterTextFrame.override(aPage);
pageTextFrame.label = "week number"+pageSide;

placeholder = pageTextFrame.contents.indexOf(keyPhrase);
if (placeholder != -1) {
pageNumber = Number(aPage.name);
weekNumber = (pageNumber%2 == 1)?((pageNumber-5)/2):((pageNumber-4)/2);
pageTextFrame.characters[placeholder].contents = "Week_"+weekNumber;
}
}else{
//alert("Did not find MasterTextFrame with ["+keyPhrase+"] on page ["+Number(aPage.name)+"]");
}
}


function findMasterFrameContaining (master, side, text) {
var masterPage;
var i;

if (master.pages.length > 1) {
masterPage = master.pages[side];
}else{
masterPage = master.pages[0];
}
for (i=0; i<masterPage.textFrames.length; i++) {
if (masterPage.textFrames[i].contents.indexOf(text) > -1){
return masterPage.textFrames[i];
}
}
// Not found? Perhaps on this Master's Master?
if (master.appliedMaster != null) {
return findFrameContaining (master.appliedMaster, side, text);
}
return null;
}

function findByLabel (items, label) {
var i;
for (i=0; i<items.length; i++) {
if (items[i].label == label) return items[i];
}
return null;
}

 

 

TOPICS
Scripting

Views

99

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 1 Correct answer

Adobe Community Professional , Jan 08, 2022 Jan 08, 2022
Edit: Nevermind I looked at the image differently. Not sure why it's placing incorrectly but it's probably due to the .override(aPage) function. You may need to reposition the frame based on the master coordinates? Something like:  pageTextFrame.geometricBounds = master text frame.geoemtricBounds? Not sure why it would be behaving like that. 

Likes

Translate

Translate
Adobe Community Professional ,
Jan 08, 2022 Jan 08, 2022

Copy link to clipboard

Copied

Edit: Nevermind I looked at the image differently. Not sure why it's placing incorrectly but it's probably due to the .override(aPage) function. You may need to reposition the frame based on the master coordinates? Something like: 

pageTextFrame.geometricBounds = master text frame.geoemtricBounds? Not sure why it would be behaving like that. 

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
Community Beginner ,
Jan 08, 2022 Jan 08, 2022

Copy link to clipboard

Copied

Thank you very much @brianp311! Your suggestion solved the important part of the problem - the left page is now positioned in the right place, but still has the masterTextFrame underneath - see screenshot. Could you please point me to Adobe script documentation where I can find how to delete that residual masterTextFrame programmatically?  Thank you for providing a very helpful workaround - it moved my design forward! 

Karina_I_0-1641676395912.png

 

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 ,
Jan 08, 2022 Jan 08, 2022

Copy link to clipboard

Copied

remove() works for all page items, so masterTextFrame.remove();

 

Full searchable API documentation is available at https://www.indesignjs.de/extendscriptAPI/indesign-latest/

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
Community Beginner ,
Jan 08, 2022 Jan 08, 2022

Copy link to clipboard

Copied

Thank you again @brianp311 . Unfortunately masterTextFrame.remove() removes it from the master page as well, not just document pages. For now, after script run, I will manually hide it on the master page before converting file into pdf for print. I will search more in doc link you provided.

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
Advocate ,
Jan 08, 2022 Jan 08, 2022

Copy link to clipboard

Copied

Hello @Karina_I,

 

Try.......masterTextFrame.detach();

to see if it works for you.

 

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
Community Beginner ,
Jan 08, 2022 Jan 08, 2022

Copy link to clipboard

Copied

LATEST

Hi @mikeb41294032 

The  masterTextFrame.detach() detached the masterFrame, but it was still visible. I searched more forums and it turned out that shifting of the elements upon usage of .override(aPage)  has happened to other users. Most people used the method suggested by @brianp311 to apply geoemtricBounds. It indeed helped me too, so hopefully I am not breaking rules by marking his answer as correct. The problem for me was visibility of the underlying master frame. It was also mentioned that somehow this shifting was triggered by the specific layouts, so I decided to change the layout and replaced two master TextFrames with a single one having two columns. Then I updated the script to replace all occurencies of "#'. It worked!  Thank you everyone for helping out!

Changed master layout to use two columns:

 

Karina_I_0-1641698015666.png

 

Good result after running the script:

Karina_I_1-1641698543436.png

 

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