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

How to apply swatches into document via script?

Explorer ,
Mar 02, 2017 Mar 02, 2017

Every day i have to do this steps many times:

1. Window -> Swatches -> Other Libraries -> "Select MySwatches.ai from Computer".

2. Then i click on swatches to apply them into swatch list

SwatchApply.jpg

Can u tell me please, how to apply swatches into document via script, if it possible?

TOPICS
Scripting
3.4K
Translate
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
Valorous Hero ,
Mar 03, 2017 Mar 03, 2017

Yea it's possible, a script can open your swatch file and copy all the swatches over. However, if your issue is putting them into a new document, you could alter one of the new document profiles to include the swatches so that your new default documents will already have them.

Translate
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
Explorer ,
Mar 03, 2017 Mar 03, 2017

Hello, dear Silly-V!

Unfortunately, all the documents I work with are not new. Please, tell me how to do this for already created documents.

Translate
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
Explorer ,
Mar 10, 2017 Mar 10, 2017

I found how to open swatches, but can't find how to copy them. Please take a look.

#target Illustrator

//  insert your path here, or a file picker prompt

var theFile = new File('path/to/file/file_name.ai');

var openOpt = new OpenOptions();

//  get swatches

openOpt.openAs=LibraryType.SWATCHES;

open(theFile,null,openOpt);

Translate
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
Valorous Hero ,
Mar 10, 2017 Mar 10, 2017

They will not do what you want when you're opening as library. What you want to do is open the document and duplicate the swatches from one document to another.

Translate
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
Valorous Hero ,
Mar 10, 2017 Mar 10, 2017

Here is an example of basic process swatches, assuming you have two documents open, both having the same color space.

#target illustrator

function test(){

  var doc = app.activeDocument, doc2 = app.documents[1];

  alert(doc.name + " , " + doc2.name);

  var thisSw, newSw;

  for(var i=0; i<doc2.swatches.length; i++){

    thisSw = doc2.swatches;

    if(thisSw.name == "[Registration]" || thisSw.name == "[None]"){

      continue;

    }

    newSw = doc.swatches.add();

    newSw.name = thisSw.name;

    newSw.color = thisSw.color;

  }

 

};

test();

Translate
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
Explorer ,
Mar 13, 2017 Mar 13, 2017

Thanks, but it's actually not that i want to.

I need copy swatches not from another document, i need to copy them from library into document swatches.

Translate
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
Valorous Hero ,
Mar 13, 2017 Mar 13, 2017

Your library is just another .ai document, so are all the other libraries such as graphic styles or symbols. You'd have to open such a file with your script and copy the items over.

Translate
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
Explorer ,
Mar 16, 2017 Mar 16, 2017
LATEST

Silly-V, can u please correct code to open, work with and close document with swatches file by name, not by documents index?

Translate
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
Explorer ,
Mar 13, 2017 Mar 13, 2017

Now i understand, thanks a lot Silly-V!

I am not strong in coding, by this one is works right.

#target Illustrator

//  insert your path here, or a file picker prompt

var theFile = new File('C:/mySwatch.ai');

// var openOpt = new OpenOptions();

//  get swatches

// openOpt.openAs=LibraryType.SWATCHES;

open(theFile,null/*,*openOpt*/);

  var doc2 = app.activeDocument, doc = app.documents[1];

  var thisSw, newSw;

  for(var i=0; i<doc2.swatches.length; i++){

    thisSw = doc2.swatches;

    if(thisSw.name == "[Registration]" || thisSw.name == "[None]"){

      continue;

    }

    newSw = doc.swatches.add();

    newSw.name = thisSw.name;

    newSw.color = thisSw.color;

  };

doc2.close(SaveOptions.DONOTSAVECHANGES);

Translate
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