Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Script: Alerts for Character Style and Preflight errors

Contributor ,
Jun 07, 2024 Jun 07, 2024

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

TOPICS
How to , Scripting
919
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Jun 07, 2024 Jun 07, 2024

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
...
Translate
Community Expert , Jun 14, 2024 Jun 14, 2024

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);
     
...
Translate
Community Expert ,
Jun 07, 2024 Jun 07, 2024

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!");
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jun 14, 2024 Jun 14, 2024

Thanks @Eugene Tyson! The alert for Character Style worked just as expected 🙂
Checking with @rob day regarding the script for preflight errors...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 08, 2024 Jun 08, 2024

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 08, 2024 Jun 08, 2024

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)
} 

 

 

Screen Shot 26.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jun 14, 2024 Jun 14, 2024

Hi @rob day, I just gave it a try, but no luck. I'm getting this error:

Rogerio5C09_0-1718378133254.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 14, 2024 Jun 14, 2024

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)
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jun 14, 2024 Jun 14, 2024

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?

for (var d=0; d<app.documents.length; d++)
{
var myDoc = app.documents.everyItem();
 
var wp = "Preflight"
 
var d = app.documents[d];
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("This document contains preflight errors.")  
   }
} else {
myDoc.close(SaveOptions.yes);
}        
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 14, 2024 Jun 14, 2024

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);
        }
    };   
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jun 14, 2024 Jun 14, 2024
LATEST

It worked like magic! Thanks a lot !:)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines