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:
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:
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;
}
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.
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.
Copy link to clipboard
Copied
Thank you very much @brian_p_dts! 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!
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/
Copy link to clipboard
Copied
Thank you again @brian_p_dts . 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.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Hi @Mike Bro
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 @brian_p_dts 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:
Good result after running the script: