Skip to main content
Participant
October 25, 2023
Answered

How can I toggle the "Ignore Color" button with a script?

  • October 25, 2023
  • 4 replies
  • 3695 views

Hello there!

I'm still quite new to both scripting and Adobe Illustrator, but I would like to optimize my time by writing a script that can take full black and white images and remove the white.

My Frankenstein code starts by opening a test folder (I removed the location from the image Debug1, but I know where it points to) and grabbing an array of all files.

Then it converts these images to svg by the end of the function. In the midst of this transition, it places the image on a new document, traces it using a preset I call "Ignore White", and then redraws the image with the new paths (or at least that's how I think it works).

Anyway, my main problem is that when I call "options.loadFromPreset(tracingPresets[19])" (image in Debug2, line 67), the alert shows that my preset does load and that it has the correct name, but when I run the script, I can clearly see in Debug3 that the Image Trace settings did not update properly to "Ignore Color" (aka Ignore White), AND the preset is called "Custom" and not "Ignore White".

 

Does anyone know how I can toggle the "Ignore Color" button in real time so that my SVG will actually remove the white and be fully transparent?

P.S. (I already use "options.ignoreWhite = true;" on line 71, and that definitely doesn't work NOR does it show in Debug3 when the script is paused due to the alert on line 74)

 

Any and all help would be greatly appreciated!

This topic has been closed for replies.
Correct answer Isaac265865188lnf

I'm also having this issue with the ignore white option no longer working. The most recent version of illustrator has swapped the ignore white tik box with Ignore Color and an eyedropper which has to be the issue. I've tried adding the method for abutting and trying to add an option for ignoreColor into the script. but ignore white still doesnt work. There has to be another option that isnt in the documentation out there yet.

The only thing I've found that somewhat works is:
-creating a preset for the tracing settings you want
-going all the way into Users>you>app settings>roaming>adobe>the newest illustrator> en_US>x64>Vectorizing Presets, opening that with notepad
-copying the settings from the collection at the top (which will be the newest preset you made)
-and pasting them over the settings down in collection 7 (which is the default settings used when you click Image Trace).

at least that way, calling image trace defaults to the desired settings. but this is a very annoying work around which still leaves my scripts calling for an option that are no longer used. What would really help is if A) you could either set the default settings in illustrator (as well as for magic wand while we're at it) OR B) allow recorded actions to store image trace settings in the action rather than just using the default. Hoping somebody figures out what can be done about this change in javascript soon.

4 replies

Isaac265865188lnfCorrect answer
Inspiring
October 27, 2023

I'm also having this issue with the ignore white option no longer working. The most recent version of illustrator has swapped the ignore white tik box with Ignore Color and an eyedropper which has to be the issue. I've tried adding the method for abutting and trying to add an option for ignoreColor into the script. but ignore white still doesnt work. There has to be another option that isnt in the documentation out there yet.

The only thing I've found that somewhat works is:
-creating a preset for the tracing settings you want
-going all the way into Users>you>app settings>roaming>adobe>the newest illustrator> en_US>x64>Vectorizing Presets, opening that with notepad
-copying the settings from the collection at the top (which will be the newest preset you made)
-and pasting them over the settings down in collection 7 (which is the default settings used when you click Image Trace).

at least that way, calling image trace defaults to the desired settings. but this is a very annoying work around which still leaves my scripts calling for an option that are no longer used. What would really help is if A) you could either set the default settings in illustrator (as well as for magic wand while we're at it) OR B) allow recorded actions to store image trace settings in the action rather than just using the default. Hoping somebody figures out what can be done about this change in javascript soon.

m1b
Community Expert
Community Expert
October 27, 2023

Interesting! So, do you think this is a bug introduced when they changed "ignore white" to "ignore color"? In other words, they have updated the native feature but not the scripting API for it? If anyone has confirmed this bug (and it sounds like you have @Isaac265865188lnf) please post a bug report..

Inspiring
October 28, 2023

I am far too much of a scripting novice to know anything like that with certainty, sadly. That "solution" is something I found from another user (I would very much like to credit them for it but I've long since lost the page where I found it). I may have come in a little hot via frustration - my thinking it's an introduced bug is purely speculation and, again, I dont know nearly enough about any of this to present this theory as fact to anyone who actually knows the ins and outs of it. I was just excited to find other people who hit this wall too.

Participant
October 26, 2023

/* Here is the full code as text.
will process all jpg and png files from folder like :
e.g. take some file like my-exported-graphic.jpg, trace it and export it as my-exported-graphic.svg
*/

// SPECIFY YOUR OWN PATHS OR USE selectFolder() instead
var origin = Folder('');
var destination = Folder('');

var files = getFiles(origin);
convertFiles(files);
// select folder for import
function selectFolder() {
// allow user to select from dialog
return Folder.selectDialog('Please select the folder to be imported:', Folder('~/Development/sandbox/cyberpunk/refacto/my-tests'));
}

function getFiles(folder) {
return folder ? folder.getFiles() : undefined;
}

function convertFiles(files) {
if (files) {
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (!file || !(file instanceof File)) continue;
var filename = file.name;
var valid = (filename.indexOf('.jpg') != -1) || (filename.indexOf('.png') != -1)
if (!valid) {
continue;
} else {
convertFile(files[i]);
}
}
} else {
alert('no file to convert');
}
}

function write (document, filename) {
var options = new ExportOptionsSVG();
options.documentEncoding = SVGDocumentEncoding.UTF8;
var file = new File(destination + '/' + filename);
document.exportFile(file, ExportType.SVG, options);
}

function convertFile (file) {
var filename = file.name;
// create new document
var document = app.documents.add();
try {
var layer = document.layers[0];
layer.name = filename.substring(0, filename.indexOf('.'));
// place image file in the layer
var placed = layer.placedItems.add();
placed.file = file;
// position placed image in the document
placed.top = document.height;
placed.left = 0;
// trace image with custom options derived from [Default] preset
var plugin = placed.trace();
var tracing = plugin.tracing;
var options = tracing.tracingOptions;
var tracingPresets = app.tracingPresetsList;
plugin.name = layer.name;
alert(tracingPresets[19]);
options.loadFromPreset(tracingPresets[19]);
// apply tracing change and expand
alert('The Ignore White should be true: ' + options.ignoreWhite);
app.redraw();
options.ignoreWhite = true;
options.ignoreColor = true;
options.strokes = true;
alert('The Ignore White should be true: ' + options.ignoreWhite);
app.redraw();
alert('The app should have waited 5 seconds');
tracing.expandTracing().selected = true;
//tracing.expandTracing();
// resize artboard to traced image
var artboard = document.artboards[0];
artboard.artboardRect = [0, placed.height, placed.width, 0];
// easier to position group to origins first
var element = document.pageItems[0];
element.position = [0, document.artboards[0].artboardRect[1]];
// then actually ungroup
var group = document.groupItems[0];
var nested = group.pageItems;
var count = nested.length;
for (var i = count - 1; i >= 0; i--) {
nested[i].move(layer, ElementPlacement.PLACEATBEGINNING);
}
// export as SVG
var outname = layer.name + '.svg';
write(document, outname);
document.close();
} catch (error) {
document.close(SaveOptions.DONOTSAVECHANGES);
}
}

m1b
Community Expert
Community Expert
October 26, 2023

Hi @VMPrinting, no big deal, but in the future please use the code </> button in the editor toolbar to format code and if you have screenshots, post them inline with the graphic icon in the editor toolbar. Attached graphics (as opposed to inline graphics) are very slow to load and some people *cough* sorry... won't bother loading them.

Charu Rajput
Community Expert
Community Expert
October 25, 2023

@VMPrinting,

It will be easy if you have share code as a text as it helps to debug easily. Also, as per the documentation the setting of property ignoreWhite work only when tracingMethod is set to TracingMethodType.TRACINGMETHODABUTTING.

Best regards
Participant
October 26, 2023

@Charu Rajput, I have posted my code below in another comment. I will try updating the tracing method to determine if that fixes it and report back.

m1b
Community Expert
Community Expert
October 25, 2023

Hi @VMPrinting, it might help to set all the tracing options yourself. Have a look at the Tracing Object, and more importantly, TracingOptions.

 

Here is a script for example:

(function () {

    var doc = app.activeDocument,
        item = doc.selection[0];

    if (typeof item.trace !== 'function') {
        alert('Please select a traceable page item.');
        return;
    }

    var itemTrace = item.trace(),
        tracingOptions = itemTrace.tracing.tracingOptions;

    tracingOptions.tracingMode = TracingModeType.TRACINGMODEBLACKANDWHITE;
    tracingOptions.ignoreWhite = true;
    tracingOptions.threshold = 128;
    tracingOptions.pathFidelity = 50;
    tracingOptions.cornerFidelity = 20;
    tracingOptions.fills = true;
    tracingOptions.strokes = false;
    tracingOptions.cornerAngle = 135;

    // var tracedItem = tracingOptions.expandTracing();
    // tracedItem.selected = true;

})();

 

If you need to use a preset, then the TracingOptions object has "loadFromPreset" and "storeToPreset" methods. You could load the one you want, and then set the tracingOptions.ignoreWhite property.

- Mark

Participant
October 26, 2023

Hey Mark,

I appreciate you providing this sample code, and I do understand that I could set all of the tracing options myself, but as I previously stated, "my main problem is that when I call "options.loadFromPreset(tracingPresets[19])" (image in Debug2, line 67), the alert shows that my preset does load and that it has the correct name, but when I run the script, I can clearly see in Debug3 that the Image Trace settings did not update properly to "Ignore Color" (aka Ignore White), AND the preset is called "Custom" and not "Ignore White"."

 

It's not so much that I haven't tried using "loadFromPreset". It's that "loadFromPreset" does not work as intended. My preset SHOULD load and "ignoreWhite" should automatically be true. What is the point of loading a preset if I have to update everything myself anyway?

 

Again, I appreciate you trying, but this was not the answer I was looking for.

m1b
Community Expert
Community Expert
October 26, 2023

Hi @VMPrinting, my apologies I didn't read your post properly at all!

 

If you do not get a satisfactory answer, please do the following: create a test .ai document and simplified version of your script that (a) shows the bug with a minimum of extraneous features, and (b) can be reproduced on anyone's computer (hopefully) so because I don't have 19 tracing presets I can test with your preset—does the bug appear with a built-in preset? If you can set it up so people can run it and confirm (or otherwise) your bug with a minimum of confounding factors, then this is ideal and you will get the most help.

- Mark