Copy link to clipboard
Copied
Hi everyone,
Can I have a script that pop-ups an alert in case there's no 'Character Style 1' applied on text in the document?
-
Also, I'm using the code below to save and close all docs, but I was wondering if it's possible to create an alert in case of any preflight errors (like overset text, missing links, fonts etc)? In this case, the script should not close the document.
for (var d=0; d<app.documents.length; d++)
{
var myDoc = app.documents[d];
app.documents.everyItem().close(SaveOptions.yes);
}
alert("Done!");
Thanks in advance for the help! 🙂
Rogerio
Well here's a start - for the life of me I can't get it to trigger when the document is closing
for (var d = 0; d < app.documents.length; d++) {
var myDoc = app.documents[d];
var characterStyleExists = false;
// Check if 'Character Style 1' is applied anywhere in the document
for (var i = 0; i < myDoc.stories.length; i++) {
var story = myDoc.stories[i];
for (var j = 0; j < story.characters.length; j++) {
if (story.characters[j].appliedCharacterStyle.n
...
Maybe this?:
//your preflight profile name
var wp = "Preflight";
var tp = app.preflightProfiles.itemByName(wp);
//gets all of the open documents
var docs = app.documents.everyItem().getElements();
//make sure the preflight profile exists
if (!tp.isValid) {
alert("No Profile Named: " + wp)
}else{
var d, pf;
//loop thru the documents
for (var i = 0; i < docs.length; i++){
//the document to check
d = docs[i];
pf = app.preflightProcesses.add(d, tp);
Copy link to clipboard
Copied
Well here's a start - for the life of me I can't get it to trigger when the document is closing
for (var d = 0; d < app.documents.length; d++) {
var myDoc = app.documents[d];
var characterStyleExists = false;
// Check if 'Character Style 1' is applied anywhere in the document
for (var i = 0; i < myDoc.stories.length; i++) {
var story = myDoc.stories[i];
for (var j = 0; j < story.characters.length; j++) {
if (story.characters[j].appliedCharacterStyle.name === "Character Style 1") {
characterStyleExists = true;
break;
}
}
if (characterStyleExists) {
break;
}
}
if (!characterStyleExists) {
alert("Alert: 'Character Style 1' is not applied to any text in the document '" + myDoc.name + "'.");
continue; // Skip closing this document and move to the next one
}
// Perform preflight check for overset text
var oversetTextExists = false;
for (var k = 0; k < myDoc.textFrames.length; k++) {
if (myDoc.textFrames[k].overflows) {
oversetTextExists = true;
break;
}
}
// Check for missing links
var missingLinks = [];
for (var i = 0; i < myDoc.links.length; i++) {
if (myDoc.links[i].status === LinkStatus.LINK_MISSING) {
missingLinks.push(myDoc.links[i].name);
}
}
// Check for missing fonts
var missingFonts = [];
for (var j = 0; j < myDoc.fonts.length; j++) {
if (myDoc.fonts[j].status === FontStatus.MISSING) {
missingFonts.push(myDoc.fonts[j].name);
}
}
var preflightErrors = [];
if (oversetTextExists) {
preflightErrors.push("Overset text exists.");
}
if (missingLinks.length > 0) {
preflightErrors.push("Missing links: " + missingLinks.join(", "));
}
if (missingFonts.length > 0) {
preflightErrors.push("Missing fonts: " + missingFonts.join(", "));
}
if (preflightErrors.length > 0) {
alert("Preflight errors found in document '" + myDoc.name + "':\n" + preflightErrors.join("\n"));
continue; // Skip closing this document and move to the next one
} else {
myDoc.close(SaveOptions.yes);
}
}
alert("Done!");
Copy link to clipboard
Copied
Thanks @Eugene Tyson! The alert for Character Style worked just as expected 🙂
Checking with @rob day regarding the script for preflight errors...
Copy link to clipboard
Copied
To check if the style is used or not we can use the find/change API. That might be more efficient that iterating each character
var myDocument = app.activeDocument;
app.findTextPreferences = NothingEnum.nothing;
app.findTextPreferences.appliedCharacterStyle = myDocument.characterStyles.item("Character Style 1");
var foundItems = myDocument.findText();
app.findTextPreferences = NothingEnum.nothing;
if (foundItems.length > 0) {
alert("The character style is used in the document.");
} else {
alert("The character style is not used in the document.");
}
-Manan
Copy link to clipboard
Copied
Hi @Rogerio5C09 , A script can check if the document’s working preflight profile is returning any errors—something like this:
var d = app.activeDocument
var tp = app.preflightProfiles.itemByName(d.preflightOptions.preflightWorkingProfile)
var pf = app.preflightProcesses.add(d, tp);
pf.waitForProcess();
pf.processResults;
if (pf.aggregatedResults[2] != "") {
alert(d.name + " Has Preflight Problems\rWorking Profile: " + tp.name)
}
Copy link to clipboard
Copied
Hi @rob day, I just gave it a try, but no luck. I'm getting this error:
Copy link to clipboard
Copied
Might be because you don’t have a working profile selected. Try this—replace "Your Profile Name" with the profile you want to use:
var wp = "Your Profile Name"
var d = app.activeDocument
var tp = app.preflightProfiles.itemByName(wp)
if (tp.isValid) {
var pf = app.preflightProcesses.add(d, tp);
pf.waitForProcess();
pf.processResults;
if (pf.aggregatedResults[2] != "") {
alert(d.name + " Has Preflight Problems\rWorking Profile: " + tp.name)
}
} else {
alert("No Profile Named: " + wp)
}
Copy link to clipboard
Copied
Thanks for that @rob day! I'm trying to adapt it to meet my needs (see script bellow), but for some reason the open documents are not closing, even if there aren't preflight errors. Am I misssing anything?
Copy link to clipboard
Copied
Maybe this?:
//your preflight profile name
var wp = "Preflight";
var tp = app.preflightProfiles.itemByName(wp);
//gets all of the open documents
var docs = app.documents.everyItem().getElements();
//make sure the preflight profile exists
if (!tp.isValid) {
alert("No Profile Named: " + wp)
}else{
var d, pf;
//loop thru the documents
for (var i = 0; i < docs.length; i++){
//the document to check
d = docs[i];
pf = app.preflightProcesses.add(d, tp);
pf.waitForProcess();
pf.processResults;
//if item 3 of the aggregated results is empty there are no errors
if (pf.aggregatedResults[2] == "") {
$.writeln(d.name + ": Contains no preflight errors—save and close.")
d.close(SaveOptions.yes);
}
};
}
Copy link to clipboard
Copied
It worked like magic! Thanks a lot !:)
Find more inspiration, events, and resources on the new Adobe Community
Explore Now