Change Page Header text if size is too long
Hello! I'm currently having an issue with headers with long texts, and I can't find a good way to solve it.
I'm using a TextVariable of "Running Header (Paragraph Style)" type in order to insert a Page Header in the top of every page. This Paragraph style i'm using matches a Chapter Name so when the chapter is changed, the page header is changed too.
The issue comes when this text is bigger than the width of the page, so it doesn't fits properly. I've tried solving this issue via scripts, but can't find a way of changing the value of the Text Variable.
Example of what I want to achieve:
Page 1:
Chapter name: "This is a big big big big big big chapter name, so it does not fit properly in my page boundries, I s should be cut."
Resulting Header for Page 1: "This is a big big big big big big chapter name, so it does not fit properly in my page boundries, so you should be cut."
Expected Header:"This is a big big..." (This is what I want to get, for example)
Page 5: (Here everything is ok, so no need to change it)
Chapter two: "Short beautiful name"
Resulting header: "Short beautiful name"
These are some approaches I've tried:
One of them was searching for the text with my paragraph Style for the Header, and change it to my desired text. The problem with this, is that the paragraph Style is tied with the variable, so I must change the paragraph Style text in order to change the value of the header. I can't change one without changing the other (or did not find a way to do it.)
function findText(para) {
//param is the paragraphStyle name
//Search the chapter Text
//If the text length is bigger than x number, it will slice the text when it finds a space after x chars, and places ...
app.findTextPreferences = NothingEnum.NOTHING;
paraStyle = app.activeDocument.
paragraphStyles.itemByName(para);
app.findTextPreferences.appliedParagraphStyle = paraStyle;var foundText = app.activeDocument.findText();
var textoAnterior;
for (var i = 0; i < foundText.length; i++) {var textoPara = foundText;
var texto = textoPara.contents;//if (textoAnterior == texto) {
if (texto.length > 40) {
var pStyle = app.activeDocument.paragraphStyles.itemByName(para);
var indexHastaEspacio = texto.indexOf(" ",40);
var textoHastaEspacio;
if (indexHastaEspacio > 0 && indexHastaEspacio < 66) {
textoHastaEspacio = texto.split(0, indexHastaEspacio);
} else {
textoHastaEspacio = texto.split(0, 40);
}textoHastaEspacio = textoHastaEspacio + "...";
alert(textoHastaEspacio);
findChange(texto, textoHastaEspacio, paraStyle, paraStyle, '', '');/* } else {
textoAnterior = texto;
}
*/
}}
}
function findChange(findWhat, changeTo, findStyle, changeStyle, findChrStyle, changeChrStyle) {
var doc = app.activeDocument;
try {
app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
if (findWhat) app.findTextPreferences.findWhat = findWhat;
if (changeTo) app.changeTextPreferences.changeTo = changeTo;
if (findStyle) app.findTextPreferences.appliedParagraphStyle = findStyle;
if (findChrStyle) app.findTextPreferences.appliedCharacterStyle = findChrStyle;
doc.changeText();
} catch (e) {
alert("Hola" + e);
}
app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
}
After some searching, seems that the "Running Header (Paragraph Style)" type" can't be changed easily as the Custom Text Type (It does not have a variableOptions.contents option for setting it).
Another solution I tried was changing the appliedParagraph option:
app.activeDocument.textVariables.item("HeaderVariable").variableOptions.appliedParagraphStyle = "AnotherParapraphStyle";
The problem with this, is that I do not have any other parapraph to apply, I just want a slice of my text.
If it helps I'm working in Indesign 2018, with a template, and placing a Word Document in order to get the text. The variables are set in the Master Pages.
Any help on how to deal with this? Maybe Text Variables are not the best thing for this?
Thanks!
