Skip to main content
LynxKx
Inspiring
November 24, 2021
Answered

InDesign Script to manage swatches

  • November 24, 2021
  • 3 replies
  • 2579 views

I put together a script to automatically import swatches and styles from another InDesign file. Is there anything extra I can add to the code that would keep it from duplicating my CMYK blend? it duplicates that color if i already have it in a file.

 

 

I'm also replacing old swatches with new and cant eliminate the tint swatch thats in my file. How do i include the tint swatch in my code to remove or replace it.

 

var myDoc = app.activeDocument;
var preFile = File.openDialog ("Select file");

//load swatches
var _loadSwatches = preFile;
if ( _loadSwatches != null ) {
try { app.activeDocument.loadSwatches ( _loadSwatches ) }
catch ( _ ) { alert ( _ ) }
}

//replace swatches
try { myDoc.colors.item("PANTONE Neutral Black C 90%").remove("PANTONE 426 C");}
catch (e) {}

 

 

 

This topic has been closed for replies.
Correct answer LynxKx

This worked like a dream thank you so much! Is this possible with paragraph and character styles?


totally! in the same InDesign file you saved the swatches in, add any new styles so they can be pulled in at the same time

 

var myDoc = app.activeDocument;
var preFile = File.openDialog ("Select file");
var paraStyles = myDoc.allParagraphStyles;
var charStyles = myDoc.allCharacterStyles;

//load swatches
var _loadSwatches = preFile;
if ( _loadSwatches != null ) {
try { app.activeDocument.loadSwatches ( _loadSwatches ) }
catch ( _ ) { alert ( _ ) }}

//load styles
myDoc.importStyles(ImportFormat.PARAGRAPH_STYLES_FORMAT, preFile);
myDoc.importStyles(ImportFormat.CHARACTER_STYLES_FORMAT, preFile);
myDoc.importStyles(ImportFormat.OBJECT_STYLES_FORMAT, preFile);
myDoc.importStyles(ImportFormat.TABLE_AND_CELL_STYLES_FORMAT, preFile);

//replace swatches
try { myDoc.colors.item("OLD").remove("NEW");}catch (e) {}
try { myDoc.colors.item("OLD").remove("NEW");}catch (e) {}
try { myDoc.colors.item("OLD").remove("NEW");}catch (e) {}


// Rename styles
for (a = 2; a < paraStyles.length; a++) {
    if (paraStyles[a].name == "OLD") { paraStyles[a].name = "NEW"; }
    if (paraStyles[a].name == "OLD") { paraStyles[a].name = "NEW"; }
    if (paraStyles[a].name == "OLD") { paraStyles[a].name = "NEW"; }}
for (a = 2; a < charStyles.length; a++) {
    if (charStyles[a].name == "OLD") { charStyles[a].name = "NEW"; }
    if (charStyles[a].name == "OLD") { charStyles[a].name = "NEW"; }
    if (charStyles[a].name == "OLD") { charStyles[a].name = "NEW"; }} 

// Replace styles in a group if file for sure has all of them, if one is missing then it will skip the rest
try{    
    paraStyles.item("OLD").remove(paraSyles.item("NEW"));
    charStyles.item("OLD").remove(charSyles.item("NEW"));
    paraStyles.item("OLD").remove(paraSyles.item("NEW"));
}catch(e){}

// Replace styles individually
try { paraStyles.item("OLD").remove(paraSyles.item("NEW");}catch (e) {}
try { charStyles.item("OLD").remove(paraSyles.item("NEW");}catch (e) {}

// Done
alert("Alakazam!");

3 replies

Participant
September 8, 2022

Hello, I am very new to scripts and am not seeing any solutions for what I am trying to do. Hoping you can help possibly? We have proposals and there are about 30+/- color swatches. Many are duplicates. I am hoping there is a way to do the following with scripting: 

  • Import two .ASE files
  • Identify swatch by specified name > delete swatch > replace swatch with new color
    (ie: after importing ASE file the script will then look for custom names like
    "Cyan 100" > Delete/Replace with "Pantone 2925C"
    "65K 12" > Delete/Replace with "65K"
    "Pantone 2925C Copy" > Delete/Replace with "Pantone 2925C" 

Any help would be appreciated! I am not the best at reading javascript and hoping to learn. Thank you!

LynxKx
LynxKxAuthor
Inspiring
September 8, 2022

I like to load swatches from an Indesign file b/c it wont duplicate swatches as copies (just create an InDesign file with all the new swatches you want to pull in) Below is some code that will prompt you to open an InDesign, you will select it and the script will load the swatches from that file and then replace out the swatches you specify.  let me know how this works!

 

var myDoc = app.activeDocument;
var preFile = File.openDialog ("Select file");

//load swatches
var _loadSwatches = preFile;
if ( _loadSwatches != null ) {
try { app.activeDocument.loadSwatches ( _loadSwatches ) }
catch ( _ ) { alert ( _ ) }}

//replace swatches
try { myDoc.colors.item("Cyan 100").remove("PANTONE 2925 C");}catch (e) {}
try { myDoc.colors.item("65K 12").remove("65K");}catch (e) {}
try { myDoc.colors.item("OLD").remove("NEW");}catch (e) {}


// Done
alert("Abracadabra!!");

 

 

Participant
September 8, 2022

This worked like a dream thank you so much! Is this possible with paragraph and character styles?

LynxKx
LynxKxAuthor
Inspiring
November 30, 2021

i also got this script from someone that will eliminate tint swatches

//remove swatch tint
removeTintsOfColor(myDoc, "PANTONE Neutral Black C");
function removeTintsOfColor(doc, colorOrName) {
    var color;
    if (colorOrName.constructor.name == 'String') {
        color = doc.swatches.itemByName(colorOrName);
    } else if (colorOrName.constructor.name == 'Color') {
        color = colorOrName;
    }
    if (color == undefined || !color.isValid) return;
    var tints = doc.tints;
    for (var i = tints.length - 1; i >= 0; i--) {
        if (tints[i].baseColor == color)
            tints[i].remove("PANTONE Neutral Black C");}}

 

LynxKx
LynxKxAuthor
Inspiring
November 24, 2021

disregard duplicating swatch question, I'm not able to reproduce the issue I was having and i'm able to import swatches without duplicating any.

LynxKx
LynxKxAuthor
Inspiring
November 30, 2021

If anyone sees this later needing a solution ...  If I load swatch from an .ase file it will duplicate any CMYK blend swatches I may already have,  however if I import swatches from an InDesign file, it will not duplicate the swatch.