In addition to what my colleagues mentioned, three additional points:
1. It's more efficient first to check whether there are any pages with missing frames, and if there are, show them all and quit.
2. No need to bind the preset to a variable at every iteration of the loop: do it once (and check whether it exists).
3. You can access a labelled text frame directly, no need for a loop -- if you labelled them on the Layers panel.
4. To obtain the contents of a frame, address its parent story to avoid problems with overset text.
That's rather a lot, so I recast your script:
var myDocument = app.activeDocument;
// Use an array, much faster than a collection
var myPages = myDocument.pages.everyItem().getElements();
var myPage, myFile;
var missing = [];
//Check whether the preset is present
var myPDFExportPreset = app.pdfExportPresets.item("[PDF/X-3:2002]");
if (!myPDFExportPreset.isValid) {
alert ("Preset not found");
exit();
}
// Then check whether there are any pages without the frame
for (var i = 0; i < myPages.length; i++) {
if (!myPages[i].textFrames.item("nombreDestinatario").isValid) {
missing.push (myPages[i].name);
}
}
// Any missing frames? Show them and quit
if (missing.length > 0) {
alert("No se encontró la caja de texto 'nombreDestinatario' en las páginas " + missing.join(', '));
exit();
}
for (var i = 0; i < myPages.length; i++) {
myPage = myPages[i];
// Use the parentStory to avoid problems with overset
myFileName = myPage.textFrames.item("nombreDestinatario").parentStory.contents;
// ~/Desktop/ is a shortcut for the current user's desktop
myFile = File("~/Desktop/alumnos/" + myFileName + ".pdf");
app.pdfExportPreferences.pageRange = myPage.name;
myDocument.exportFile(ExportFormat.pdfType, myFile, false, myPDFExportPreset);
}
... View more