Script to invert colors, save new file by replacing a word in the filename

Community Beginner ,
Apr 15, 2022 Apr 15, 2022

Copy link to clipboard

Copied

Hi, I need a script that will select all on the canvas, invert colors, then save a new file by replacing the word "Black" with "White" in my filenames, as a Batch process.  I've only been able to create a Batch Action that will do everything but save out the new filename, and I assume that can only be done as a script?

 

Original Filename would be something like: Title_Logo_Black_RGB.eps

New Filename would be: Title_Logo_White_RGB.eps

 

Is there a way to include my action in the script and then run a "find and replace" string or something to switch out "Black" for "White" and then save?

TOPICS
Scripting

Views

128

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
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 15, 2022 Apr 15, 2022

Hi,

You can try below version

for (var i = app.documents.length - 1; i >= 0; i--) {
    var doc = app.documents[i];
    app.activeDocument = doc;
    var fileName = doc.fullName;
    app.executeMenuCommand('selectall')
    app.executeMenuCommand('Colors6')
    var saveOpts = new EPSSaveOptions();
    var _newFileName = fileName.fsName.replace('Black', 'White')
    doc.saveAs(File(_newFileName), saveOpts);
    app.executeMenuCommand('deselectall')
    doc.close( SaveOptions.DONOTSAVECHANGES);
}

Likes

Translate

Translate
Community Expert ,
Apr 15, 2022 Apr 15, 2022

Copy link to clipboard

Copied

Hi,

Try following snippet

for (var i = 0; i < app.documents.length; i++) {
    var doc = app.documents[i];
    var fileName = doc.fullName;
    app.executeMenuCommand('selectall')
    app.executeMenuCommand('Colors6')
    var saveOpts = new EPSSaveOptions();
    var _newFileName = fileName.fsName.replace('Black', 'White')
    doc.saveAs(File(_newFileName), saveOpts);
    app.executeMenuCommand('deselectall')
}

It will run for all the documents that are alreday open in the application

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
community guidelines
Community Beginner ,
Apr 15, 2022 Apr 15, 2022

Copy link to clipboard

Copied

Thanks. I tried it, and it does rename all the files changing "Black" to "White" but only selects all and inverts the colors on the one file which I am viewing in the list of opened files, and it seems to do the "select all - Invert colors" twice on this one file.  All other open files only get the filename change, but not the actual color change of the shapes inside. I appreciate your help.

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
community guidelines
Community Expert ,
Apr 15, 2022 Apr 15, 2022

Copy link to clipboard

Copied

Hi,

You can try below version

for (var i = app.documents.length - 1; i >= 0; i--) {
    var doc = app.documents[i];
    app.activeDocument = doc;
    var fileName = doc.fullName;
    app.executeMenuCommand('selectall')
    app.executeMenuCommand('Colors6')
    var saveOpts = new EPSSaveOptions();
    var _newFileName = fileName.fsName.replace('Black', 'White')
    doc.saveAs(File(_newFileName), saveOpts);
    app.executeMenuCommand('deselectall')
    doc.close( SaveOptions.DONOTSAVECHANGES);
}
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
community guidelines
Community Beginner ,
Apr 15, 2022 Apr 15, 2022

Copy link to clipboard

Copied

That worked perfectly! Thank you so much!

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
community guidelines
Community Beginner ,
May 24, 2022 May 24, 2022

Copy link to clipboard

Copied

LATEST

Hi there. Thanks again for helping with this. We've just discovered an issue that has been causing us problems. Every time we run this script, it checks this box again when we go to save our next file. This causes an issue when brining the EPS files into Photoshop as it causes the #000 Black colors in the EPS to not be #000 Black in Photoshop (pure black). Is there some code you could add to this to make sure during the Save As process when renaming "Black" to "White" that this box remains unchecked? 

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
community guidelines
Community Expert ,
Apr 15, 2022 Apr 15, 2022

Copy link to clipboard

Copied

It might be a good idea to loop backwards and close the respective active document after saving.

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
community guidelines