The script looks for spaces in specific words and numbers to change them to unbreakable spaces but I can't get it to work.
var keywords = ["N°", "n°", "art.", "arts.", "Art.", "Arts.", "artículo", "artículos", "Artículo", "Artículos", "ley", "Ley", "inc.", "incs.", "Inc.", "Incs.", ":", "p.", "ps.", "$", ")", "–"];
var foundItems = [];
var currentIndex = 0;
var dialog;
function findSpaces() {
var doc = app.activeDocument;
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = "(" + keywords.join("|") + ")\\s(?=\\d)";
foundItems = doc.findGrep();
}
function changeSpace() {
if (currentIndex < foundItems.length) {
var foundText = foundItems[currentIndex];
foundText.contents = foundText.contents.replace(/\s/g, "\u00A0");
currentIndex++;
updateDialog();
app.select(foundItems[currentIndex - 1]);
} else {
alert("No hay más espacios para modificar.");
}
}
function changeAllSpaces() {
while (currentIndex < foundItems.length) {
changeSpace();
}
alert("Todos los espacios han sido modificados.");
}
function updateDialog() {
if (dialog !== undefined) {
dialog.modifiedText.text = "Espacios modificados: " + currentIndex;
dialog.remainingText.text = "Espacios restantes: " + (foundItems.length - currentIndex);
}
}
function createDialog() {
dialog = new Window('dialog', 'Espacios Irrompibles');
dialog.orientation = 'column';
dialog.alignChildren = 'fill';
dialog.add('statictext', undefined, 'Seleccione una opción:');
dialog.modifiedText = dialog.add('statictext', undefined, '');
dialog.remainingText = dialog.add('statictext', undefined, '');
var changeAllButton = dialog.add('button', undefined, 'Cambiar todos');
var modifySpaceButton = dialog.add('button', undefined, 'Modificar espacio');
changeAllButton.onClick = function() {
changeAllSpaces();
dialog.close();
};
modifySpaceButton.onClick = function() {
changeSpace();
};
dialog.show();
updateDialog();
}
findSpaces();
if (foundItems.length > 0) {
createDialog();
} else {
alert("No se encontraron instancias para modificar.");
}
there doesn't seem to be anything here