Skip to main content
Participant
July 5, 2023
Question

Automatisation for option Add to swatches

  • July 5, 2023
  • 1 reply
  • 336 views

How to do automatisation for option Add to swatches? 
I have saved swatches and need to add this swatches to another file. How to do automatisation for option Add to swatches? I tried action, but this option did not write in action. 
Maybe someone can suggest how to do it?

This topic has been closed for replies.

1 reply

Legend
July 6, 2023

Does that mean you want to duplicate the swatches in document A to document B? I don’t know how to do that directly, but you can substitute by creating swatches with the same name and color.

 

 

/**
  *  duplicate selected swatches in document[0] to document[1]
  *  1.0.0
  *  sttk3.com
  *  © 2023 sttk3.com
*/

(function() {
  if(app.documents.length < 2) {return ;}
  var docA = app.documents[0] ; // src
  var docB = app.documents[1] ; // dst

  var selectedSwatches = docA.swatches.getSelected() ;
  var swatchLength = selectedSwatches.length ;
  if(swatchLength <= 0) {return ;}

  for(var i = 0 ; i < swatchLength ; i++) {
    var currentSwatch = selectedSwatches[i] ;
    duplicateSwatch(currentSwatch, docB, false) ;
  }
})() ;

/**
  * duplicate a swacth to destination document
  *  {Swatch} targetSwatch swatch to duplicate
  *  {Document} dstDoc destination document
  *  {Boolean} [override] whether to overwrite swatch with the same name or not. false if default
  *  {Swatch}
*/
function duplicateSwatch(targetSwatch, dstDoc, override) {
  var swatchName = targetSwatch.name ;
  var swatchColor = targetSwatch.color ;

  var newSwatch ;
  try {
    newSwatch = dstDoc.swatches.getByName(swatchName) ;
    if(override) {newSwatch.color = swatchColor ;}
  } catch(e) {
    newSwatch = dstDoc.swatches.add() ;
    newSwatch.name = swatchName ;
    newSwatch.color = swatchColor ;
  }
  
  return newSwatch ;
}

 

 

 

Egor Chistyakov
Inspiring
July 6, 2023

There is another script that does that:
https://github.com/alexander-ladygin/illustrator-scripts/blob/master/transferSwatches.jsx

However, both of them don’t work properly for me when I try to transfer global swathces.

This writes several empty steps into history, and when I check the other doc to see if selected swatches got added, I don’t see them, and when I return back to the origianl doc — selected swatches get deleted, just like with the Alexander’s script 😞

And it’s alos important to transfer not only selected swatches but their groups also.