• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Automatisation for option Add to swatches

New Here ,
Jul 05, 2023 Jul 05, 2023

Copy link to clipboard

Copied

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?

TOPICS
Scripting

Views

197

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
Adobe
Community Expert ,
Jul 05, 2023 Jul 05, 2023

Copy link to clipboard

Copied

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 ;
}

 

 

 

Votes

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
Engaged ,
Jul 06, 2023 Jul 06, 2023

Copy link to clipboard

Copied

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.

Votes

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 ,
Jul 06, 2023 Jul 06, 2023

Copy link to clipboard

Copied

LATEST

A special workaround for global color has been applied.

 

Since the method of getting which group a swatch belongs to may be a bit tricky, I’ll hold off on that for now.

/**
  * @File duplicate selected swatches in document[0] to document[1]
  * @version 1.1.0
  * @author sttk3.com
  * @copyright © 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
  * @Param {Swatch} targetSwatch swatch to duplicate
  * @Param {Document} dstDoc destination document
  * @Param {Boolean} [override] whether to overwrite swatch with the same name or not. false if default
  * @Return {Swatch}
*/
function duplicateSwatch(targetSwatch, dstDoc, override) {
  var swatchName = targetSwatch.name ;
  var swatchColor = targetSwatch.color ;
  alert(targetSwatch.parent) ;
  var isSpot = false ;
  if(swatchColor.constructor.name == 'SpotColor') {
    isSpot = true ;
  }

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

Votes

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