Copy link to clipboard
Copied
Imagina que tienes un bloque de código HTML muy largo que ocupa varias páginas en InDesign. Cada vez que el texto “salta” de una página a la siguiente, quieres que aparezcan dos palabritas mágicas:
Para lograrlo con un script en JavaScript (ExtendScript) para InDesign:
css
CopiarEditar
← Continuación del código anterior
(con un retorno para separarlo).
css
CopiarEditar
El código continúa en la página siguiente →
En lenguaje sencillo:
(function(){
var doc = app.activeDocument;
if (!doc) {
alert(":cross_mark: Abre primero un documento.");
return;
}
// 0) Ajusta estos nombres si no coinciden con tus estilos
var styleCode = "CodigoHTML";
var styleContinua = "Etiquetacontinuacion"; // ← Continuación…
var styleSiguiente = "Etiquetacontinua"; // El código continúa…
// 1) Eliminar etiquetas antiguas
var olds = [
"← Continuación del código anterior",
"El código continúa en la página siguiente →"
];
for (var s = 0; s < doc.stories.length; s++){
var st = doc.stories[s];
for (var i = st.paragraphs.length - 1; i >= 0; i--){
var p = st.paragraphs[i];
var txt = String(p.contents)
.replace(/[\r\n]/g, " ")
.replace(/\s+/g, " ")
.replace(/^\s+|\s+$/g, "");
for (var k = 0; k < olds.length; k++){
if (txt === olds[k]) { p.remove(); break; }
}
}
}
// 2) Inserción de las dos etiquetas
for (var s2 = 0; s2 < doc.stories.length; s2++){
var st2 = doc.stories[s2];
for (var i2 = 0; i2 < st2.paragraphs.length - 1; i2++){
var p1 = st2.paragraphs[i2],
p2 = st2.paragraphs[i2 + 1];
// a) Solo bloques de código
if (!p1.appliedParagraphStyle || p1.appliedParagraphStyle.name !== styleCode) continue;
if (!p2.appliedParagraphStyle || p2.appliedParagraphStyle.name !== styleCode) continue;
if (!p1.characters.length || !p2.characters.length) continue;
// b) Detectar salto de página real
var pg1 = p1.characters[0].parentTextFrames[0].parentPage,
pg2 = p2.characters[0].parentTextFrames[0].parentPage;
if (!pg1 || !pg2 || pg1.name === pg2.name) continue;
// c) Insertar flecha “← Continuación…” al final de p1
var ipEnd = p1.insertionPoints.lastItem();
ipEnd.contents = "\r← Continuación del código anterior\r";
// Aplicar estilo
var newBack = st2.paragraphs[i2 + 1];
newBack.appliedParagraphStyle =
doc.paragraphStyles.itemByName(styleContinua);
// d) Insertar flecha “El código continúa…” al inicio de p2
// p2 habrá subido un índice, pasa a i2+2
var ipStart = st2.paragraphs[i2 + 2].insertionPoints.firstItem();
ipStart.contents = "El código continúa en la página siguiente →\r";
var newForward = st2.paragraphs[i2 + 2];
newForward.appliedParagraphStyle =
doc.paragraphStyles.itemByName(styleSiguiente);
// e) Saltamos los dos párrafos insertados
i2 += 2;
}
}
alert(":white_heavy_check_mark: Etiquetas de continuidad limpias e insertadas correctamente.");
})();
Copy link to clipboard
Copied
Do you have a question here or is this to share your script?
Copy link to clipboard
Copied
Gracias, pero no encuentro la solución, quiero me me coloque “El código continúa en la página siguiente →” en la primera página al final del código y la otra frase “← Continuación del código anterior” en la página posterior, estoy con un script pero no doy con la solución, ¿habría alguna manera de solucionarlo más fácil o me pasaran el código correcto?
Copy link to clipboard
Copied
The approach of the script you posted is all wrong. Where did you get it? Is this ChatGPT's work?
Adding paragraphs in the story is messy and makes it hard to delete the continuation prompts. It's much better to place the prompts in text frames on a dedicated layer. To remove the prompts, all you need to do is remove that layer.
The script below assumes the presence of object styles for the text frames. The paragraph styles for the prompts are applied in the object style, and so are size and position. So all the script needs to do is place a frame and add the style and the prompt. The attached InDesign document shows the set-up (you need to refine the styles).
To see how it works, open the document, select a story (a text frame or an insertion point) and run the script.
(function () {
if (app.selection.length === 0 || !app.selection[0].hasOwnProperty ('parentStory')) {
alert ('Select a story');
exit();
}
var d = app.activeDocument;
var layer = d.layers.item ('Continua');
var ostyleContinua = d.objectStyles.item ('Etiquetacontinua');
var ostyleContinuacion = d.objectStyles.item ('Etiquetacontinuacion');
var continuaText = 'El código continúa en la página siguiente →';
var continuacionText = '← Continuación del código anterior';
if (layer.isValid) {
layer.remove();
}
var layer = d.layers.add ({name: 'Continua'});
var frames = app.selection[0].parentStory.textContainers;
for (var i = 1; i < frames.length; i++) {
if (frames[i].parentPage.side === PageSideOptions.RIGHT_HAND) {
if (frames[i].lines[0].appliedParagraphStyle.name === 'CodigoHTML') {
frames[i-1].parentPage.textFrames.add ({
appliedObjectStyle: ostyleContinua,
contents: continuaText
});
frames[i].parentPage.textFrames.add ({
appliedObjectStyle: ostyleContinuacion,
contents: continuacionText
});
}
}
}
}());
Find more inspiration, events, and resources on the new Adobe Community
Explore Now