Getting stroked colorCodes from 'script' opened PDFs

New Here ,
Jun 03, 2020

Copy link to clipboard

Copied

Hi,

I am having a very interesting behaviour within my script. I try to set colors of specified lines (those are specified by grepping the color) to pure black value (so no RGB but the 'K' value to 100). The interesting part is: as I open the file manually, my script works perfectly fine. But if open the PDF also via script, the values I check for are all undefined. I already tried to add a wait in case the file is not yet loaded completely and therefore I get no values, but nothing. It still remains undefined. If I run the script with already open files, and 'reopen' them the script also works fine. Just the combination of opening and immediate changing is not working. Here is the code so far:

#target Illustrator

(function init() {
var dir = Folder.selectDialog("Where?");	// Here I am asking for the folder to parse through; continue in line 66 for the error
var files = dir.getFiles("*.pdf");

for(var f = 0; f < files.length; f++){
    var doc = app.open(files[f]);		//opening the pdf
    unlockDelete ();				//doing changes
    removeSwatches ();				// -''-
    convertBlacks();				// -''-
    doc.close(SaveOptions.SAVECHANGES);		// closing out and saving the current pdf
}
})();

// Unloack All Layers and Delete Hidden Layers
function unlockDelete () {
var myDoc=app.activeDocument;
var layerCount=myDoc.layers.length;

for (var ii = layerCount - 1; ii >= 0; ii--) {
    var currentLayer = myDoc.layers[ii];
    currentLayer.locked = false;
    if (currentLayer.visible == false){
        currentLayer.visible = true;
        currentLayer.remove();
        }
    }
}

// Remove All Swatches except for Pantone 485C and 185C
function removeSwatches () {
var myDoc=app.activeDocument;
var sw = myDoc.swatches;
var slen = sw.length;

for (var ii = slen - 1; ii >= 0; ii--) {
    var currentSw = sw[ii];
    if (currentSw.name == "PANTONE 185 C" || currentSw.name == "PANTONE 485 C"){
        } else {
            currentSw.remove();
        }
    }
    }

// Convert CMYK Blacks to just K Blacks
function convertBlacks() {
    
  var myDoc = app.activeDocument;

  with (myDoc) {

    // Convert if document isn't CMYK
    if(documentColorSpace !== DocumentColorSpace.CMYK) {
      app.executeMenuCommand('doc-color-cmyk');
    }

    // Create flat black Color
    flatBlack = new CMYKColor();
    flatBlack.black = 100;

    // Iterate through all path items
    for (var i = 0; i < myDoc.pathItems.length; i++) {
	item = myDoc.pathItems[i];
      with (item) {
	alert(stroked + " " + strokeColor.cyan + " " + strokeColor.magenta + " " + strokeColor.yellow + " " + strokeColor.black );		// here I made some alerts to see what's wrong; interesting enough: if I come from a just opened file, the item.strokeColor.cyan is undefined as well as all the other
																		// values, except the .stroked flag
	if (item.stroked == true && item.strokeColor.cyan > 37 && item.strokeColor.cyan < 39 && item.strokeColor.magenta > 29 && item.strokeColor.magenta < 31 && item.strokeColor.yellow > 29 && item.strokeColor.yellow < 31 && item.strokeColor.black > 16 && item.strokeColor.black < 18){
		strokeColor = flatBlack;
        }
	if (stroked == true && strokeColor.cyan > 60 && strokeColor.cyan < 62 && strokeColor.magenta > 50 && strokeColor.magenta < 52 && strokeColor.yellow > 48 && strokeColor.yellow < 50 && strokeColor.black > 61 && strokeColor.black < 63){
		strokeColor = flatBlack;
        }
      }
    }
  }
}

Any suggestions to fix that are highly appreciated 😉

 

TOPICS
Scripting

Views

326

Likes

Translate

Translate

Report

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

Getting stroked colorCodes from 'script' opened PDFs

New Here ,
Jun 03, 2020

Copy link to clipboard

Copied

Hi,

I am having a very interesting behaviour within my script. I try to set colors of specified lines (those are specified by grepping the color) to pure black value (so no RGB but the 'K' value to 100). The interesting part is: as I open the file manually, my script works perfectly fine. But if open the PDF also via script, the values I check for are all undefined. I already tried to add a wait in case the file is not yet loaded completely and therefore I get no values, but nothing. It still remains undefined. If I run the script with already open files, and 'reopen' them the script also works fine. Just the combination of opening and immediate changing is not working. Here is the code so far:

#target Illustrator

(function init() {
var dir = Folder.selectDialog("Where?");	// Here I am asking for the folder to parse through; continue in line 66 for the error
var files = dir.getFiles("*.pdf");

for(var f = 0; f < files.length; f++){
    var doc = app.open(files[f]);		//opening the pdf
    unlockDelete ();				//doing changes
    removeSwatches ();				// -''-
    convertBlacks();				// -''-
    doc.close(SaveOptions.SAVECHANGES);		// closing out and saving the current pdf
}
})();

// Unloack All Layers and Delete Hidden Layers
function unlockDelete () {
var myDoc=app.activeDocument;
var layerCount=myDoc.layers.length;

for (var ii = layerCount - 1; ii >= 0; ii--) {
    var currentLayer = myDoc.layers[ii];
    currentLayer.locked = false;
    if (currentLayer.visible == false){
        currentLayer.visible = true;
        currentLayer.remove();
        }
    }
}

// Remove All Swatches except for Pantone 485C and 185C
function removeSwatches () {
var myDoc=app.activeDocument;
var sw = myDoc.swatches;
var slen = sw.length;

for (var ii = slen - 1; ii >= 0; ii--) {
    var currentSw = sw[ii];
    if (currentSw.name == "PANTONE 185 C" || currentSw.name == "PANTONE 485 C"){
        } else {
            currentSw.remove();
        }
    }
    }

// Convert CMYK Blacks to just K Blacks
function convertBlacks() {
    
  var myDoc = app.activeDocument;

  with (myDoc) {

    // Convert if document isn't CMYK
    if(documentColorSpace !== DocumentColorSpace.CMYK) {
      app.executeMenuCommand('doc-color-cmyk');
    }

    // Create flat black Color
    flatBlack = new CMYKColor();
    flatBlack.black = 100;

    // Iterate through all path items
    for (var i = 0; i < myDoc.pathItems.length; i++) {
	item = myDoc.pathItems[i];
      with (item) {
	alert(stroked + " " + strokeColor.cyan + " " + strokeColor.magenta + " " + strokeColor.yellow + " " + strokeColor.black );		// here I made some alerts to see what's wrong; interesting enough: if I come from a just opened file, the item.strokeColor.cyan is undefined as well as all the other
																		// values, except the .stroked flag
	if (item.stroked == true && item.strokeColor.cyan > 37 && item.strokeColor.cyan < 39 && item.strokeColor.magenta > 29 && item.strokeColor.magenta < 31 && item.strokeColor.yellow > 29 && item.strokeColor.yellow < 31 && item.strokeColor.black > 16 && item.strokeColor.black < 18){
		strokeColor = flatBlack;
        }
	if (stroked == true && strokeColor.cyan > 60 && strokeColor.cyan < 62 && strokeColor.magenta > 50 && strokeColor.magenta < 52 && strokeColor.yellow > 48 && strokeColor.yellow < 50 && strokeColor.black > 61 && strokeColor.black < 63){
		strokeColor = flatBlack;
        }
      }
    }
  }
}

Any suggestions to fix that are highly appreciated 😉

 

TOPICS
Scripting

Views

327

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jun 03, 2020 0
Enthusiast ,
Jun 03, 2020

Copy link to clipboard

Copied

Salut!

Si le pdf est en RGB les couleurs même après conversion CMYK restent RGB,

il faut dans ce cas faire le test sur des couleurs R-G-B

Je n'ai pas fait de test ?

elleere

PS attention à l'utilisation de with

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 03, 2020 0
Adobe Community Professional ,
Jun 05, 2020

Copy link to clipboard

Copied

Hi,

I have just tried your script and it's just working perfectly fine in both the cases, either you open it manually or or via script. If possible could you please share the pdf file in which you are facing an issue.

 

Best regards

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 05, 2020 0
Resources