Skip to main content
RoyBoySolorio
Inspiring
October 26, 2023
Answered

Javascript to warn if an RGB file is linked to open InDesign document.

  • October 26, 2023
  • 2 replies
  • 1368 views

Hi all,

 

I use a javascript that exports 2 or 3 flavors of pdf.  Works great.  I'd like to add an alert that flags any linked rgb file (tiff).  And if clear, will just pass to the next process. So alerts if RGBs linked, no alert if CMYKs are linked.

Below is what I have, but it always says that there is no RGB, whether or not rgb is linked.  

 

// Function to run a system command and capture the output
function runShellCommand(command) {
try {
var process = new Process;
process.exec(command);
process.waitFor();
return process.stdout;
} catch (e) {
return null;
}
}

// Function to check the color mode of a linked image
function isRGBImage(imagePath) {
var exifToolCommand = 'exiftool -ColorSpace -n -T -q -q "' + imagePath + '"';
var colorProfileInfo = runShellCommand(exifToolCommand);

if (colorProfileInfo) {
return (colorProfileInfo.indexOf("RGB") !== -1);
}

return false;
}

var doc = app.activeDocument;
var linkedImages = doc.links;

var rgbImagesFound = false;

for (var i = 0; i < linkedImages.length; i++) {
if (isRGBImage(linkedImages[i].filePath)) {
rgbImagesFound = true;
alert("RGB image found: " + linkedImages[i].name);
}
}

if (!rgbImagesFound) {
alert("No RGB images found in the document.");
}
This topic has been closed for replies.
Correct answer RoyBoySolorio

So the CMYK files get saved with no embedded profile, and the InDesign PDF Export Color Conversion is set to No Conversion? Do your retouch artists make color corrections after the conversion to CMYK?

 

Can you post the full script?

 


All the CMYK images are tagged, and yes the job options are set to No Conversion.  Here's the scriot as it stands.  You will need to replace preset1-3 with joboptions within your export sets. There is a spot at the top of the code that I commented out indicating where the "break" might go?

 



d = app.activeDocument;

if (checkForRGB()) {
alert("Document contains RGB color");
//Insert a break here?

} else {
//alert("No RGB color")
}

/**
* Checks Document for RGB Color
* @ return true if RGB objects are found
*
*/
function checkForRGB(){
var d = app.activeDocument

//Make a new Preflight Profile
var pf = makePreflight("TestNoRGB");
 
var cs = makeRule(pf, "ADBE_Colorspace")
cs.flag = PreflightRuleFlag.returnAsError;
 
var csNoRGB = cs.ruleDataObjects.itemByName("no_rgb")
csNoRGB.properties = {dataType:RuleDataType.BOOLEAN_DATA_TYPE, dataValue:true}
 
d.preflightOptions.preflightOff = false;
d.preflightOptions.preflightWorkingProfile = pf;

var pfp = app.preflightProcesses.add(d, pf)
pfp.waitForProcess();
 
if (pfp.processResults.length > 6) {
return true
} else {
return false
}
}





/**
* Makes a new Preflight
* @ param preflight name
* @ return the new preflight
*/
function makePreflight(n){
if (app.preflightProfiles.itemByName(n).isValid) {
return app.preflightProfiles.itemByName(n);
} else {
return app.preflightProfiles.add({name:n});
}
}

/**
* Makes a new Preflight Rule
* @ param preflight name
* @ param the preflight rule constant name
* @ return the new preflight rule
*/
function makeRule(pf, n){
if (pf.preflightProfileRules.itemByName(n).isValid) {
return pf.preflightProfileRules.itemByName(n);
} else {
return pf.preflightProfileRules.add(n);
}
}

// BS"D
// All rights reserved (c) 2015 by Id-Extras.com
// Free to use and modify but do not delete this copyright attribution.
// This script will export 2 pdfs of the current document
// Choose the PDF presets by altering their names below
// The second PDF gets a suffix added to its name.
// Modify the line below beginning name2 = to change the suffix.
// For more InDesign scripts: www.Id-Extras.com
// XYZ modified for 3 type pdfs...also holds all characters but removes .indd
d = app.activeDocument;
// Here you can choose the PDF preset
preset1 = app.pdfExportPresets.itemByName("HiRes_joboption");
preset2 = app.pdfExportPresets.itemByName("MedRes_joboption");
preset3 = app.pdfExportPresets.itemByName("LoRes_joboption");
if (!(preset1.isValid)){
alert("One of the presets does not exist. Please go to Vol1-Photoshop_Illustrator_InDesign_Acrobat_Stuff/AcrobatJobOptions_B/for actions, and load them into your setting folder");
exit();
}
//if (!(cancel)){alert("PDFs will not be created. Whaaa");
//exit();
//}
if (d.saved){
thePath = String(d.fullName).replace(".indd", "") + ".pdf";
thePath = String(new File(thePath).saveDlg());
}
else{
thePath = String((new File).saveDlg());
}
thePath = thePath.replace(/\.pdf$/, "");
name1 = thePath+".pdf";
// Here you can set the suffix at the end of the name
name2 = thePath+"_MR.pdf";
name3 = thePath+"_LR.pdf";
d.exportFile(ExportFormat.PDF_TYPE, new File(name1), false, preset1);
d.exportFile(ExportFormat.PDF_TYPE, new File(name2), false, preset2);
d.exportFile(ExportFormat.PDF_TYPE, new File(name3), false, preset3);

2 replies

Inspiring
October 26, 2023

I don't now if my response it's stupid....why don't you see in the links pannel if there is a rgb image?

RoyBoySolorio
Inspiring
October 26, 2023

I think Rob has the solution, but I'm going to tweak it to pass to the pdf generation without the "No RGB" alert.

~Roy
rob day
Community Expert
October 26, 2023

Hi @RoyBoySolorio ,  If you want your PDF to Export as all CMYK, why not simply set the Export>Output Destination to a CMYK profile? That would convert all RGB color to CMYK in the exported PDF.

 

On your code, if I comment out the try in your runShellCommand function I always get an error—Process does not have a constructor. So colorProfileInfo is always returning null.

 

A more reliable approach might be to check for RGB color using InDesign’s preflightProfile object.

rsolorio
Participating Frequently
October 26, 2023

Thanks for the suggestion to convert all to a prescribed cmyk. However, clients and pubs all have specific profiles that would require many more

job options and the potential for mismatch. Yes our artist should look for errors in the pre flyt but this script would act as a safety. 

if I get something to work I'll post the code and you all can give it a try. The export various PDFs routine is a real time saver. 

rob day
Community Expert
October 26, 2023

rob day, this works great.  Is there a way to "break" in the script or exit the script after the rgb error dialog box is closed? 


The checkForRGB() function returns true or false, so you don’t need the alerts. I assume if there is no RGB color you want to make a PDF? So you can check if checkForRGB() returns as false (no RGB color) and add your PDF routine. if it returns true you can throw up a dialog and the script ends.

 

 

 

if (!checkForRGB()) {
	//Add function for the Export here
} else {
    alert("There’s RGB Color Now What?")
}

 

 

 

What will you do if there are RGB images? Open the images into Photoshop and convert to CMYK? What profile will you convert to?