2 Replies Latest reply on Feb 1, 2017 9:30 AM by biddenback

    Used swatch names on art board

    biddenback

      *Disclaimer - I'm new to scripting. First timer, here.

       

      Let me explain what I do.

      I take artwork files and place them on an illustrator template. What we've been doing in the past was manually creating a text box and typing in the file name and the colors used in the document. We print in color separation, so I change the file name to [Registration] and each of the colors used to their color so that each plate that is printed is properly labeled.

       

      Recently I wrote a script to automatically insert the file name in registration. Saves us a lot of time and cuts down on errors. I've been trying to alter my script so that it includes the name of the colors used in the placed files and changes the text to the correct color but I've hit a few snags.

       

      Here's my script so far:

      var docRef = app.activeDocument;
      var fileName = docRef.textFrames.add();
      //Create a new text box for placed file name
      fileName.contents = docRef.placedItems[0].file.name;
      charAttr = fileName.textRange.characterAttributes;
      charAttr.textFont = app.textFonts.getByName("Helvetica-Light");
      charAttr.size = 12;
      charAttr.fillColor = docRef.swatches[1].color;
      //Create a new text box for colors used in the file
      var colorNames = docRef.textFrames.add();
      var sL = docRef.swatches;
      var text = '';
      for (var i = 0; i < sL.length; i++) {
        if (sL[i].name != '[None]' && sL[i].name != '[Registration]') {
        text += sL[i].name + ' ';
        }
      }
      colorNames.contents = text
      colAttr = colorNames.textRange.characterAttributes;
      colAttr.textFont = app.textFonts.getByName("Helvetica-Light");
      colAttr.size = 12;
      

       

      1. My first problem is that I don't know how to make the color of the text match the color name. We can have anywhere from 1 to 6 colors on a single file. Most of the time the files are only black. If they have color, it's usually spot colors. We do have a few CMYK files but those aren't very common. I'm certain this can be scripted, I'm just not sure how to do it.

       

      2. Another problem is that most of the files are 100% Black instead of Process Black. I've set up an action that runs the Default "Delete Unused Panel Items" action and then this script. We often will reuse the templates so we don't have to close and reopen the same template, but if the first file was 100% Black and the second file is only Reflex Blue, the script will include Black in the color list because the Delete Unused Panel Items action doesn't delete the Black swatch. Alternatively, if the Black swatch is deleted manually and a 100% Black file is placed on the template, the Black swatch will not reappear in the Swatches Panel.

       

      I'm sorry if I'm not being very clear. Here's an example (I would include screenshots but I don't have any sample files at the moment):

       

      I have a PDF file that is 100% Black and 185 Red. I open my template (swatches panel is empty except for [None], [Registration], and Black). I place my PDF on the document and 185 Red pops into the swatches panel. I run my script and the filename and colors used in the document are placed on the art board. Great! It worked just fine (except the for the color text issue, of course).

      Say I've got another PDF that is Reflex Blue and 423 Gray. It needs to go on the same template, so instead of closing the file and reopening a blank template, I just delete everything on the art board and place the new PDF. I run the Delete Unused Panel Items action and my script. It deletes the 185 Red since it's not used in the current document, but it doesn't delete the Black swatch, so Black is listed in my color list.

      Alternatively, I could just manually delete the Black swatch and it's fine. UNTIL I reuse the template again and my next placed file is 100% Black. The Black swatch does not pop back into the Swatches panel even though it is used on the art board.

       

      So my problem is that the Swatches panel is unreliable, I guess. I need my script to insert ALL OF and ONLY the colors used in the placed file. I am completely open to any suggestions. Help?

       

      Please let me know if I am being unclear about something!

        • 1. Re: Used swatch names on art board
          Silly-V Adobe Community Professional

          Hey it sounds like you should put in the next document and only then do the swatch removal - which should remove the swatches only contained in the document that is no longer present.

           

          Edit: I read without paying attention to the problem you describe - in which case, have a method for resetting the text box to a color with a special name for your own purposes. It could be some color named "TextColor". Then when you run the swatch-name writing script, make it write down all color names except "TextColor".

          • 2. Re: Used swatch names on art board
            biddenback Level 1

            I got my script working!

             

            Instead of worrying about getting the Black swatch to only appear when it was used (I read on a few forums that this swatch sticking has been an issue for a while), I just deleted it. The only consequence is that the black plate will never be labeled. But since it will be the only plate not labeled, the press will know which one is supposed to be black. We rarely labeled the black plates in the past anyway, so it's not an issue.

             

            I used part of John Wundes' script for rendering a color swatch legend. It took a lot of trial and error for me to adapt it to my needs but it's working great! I put together action items that delete unused swatches first, then run the script.

             

            Here's my script if anyone is interested:

             

            var docRef = app.activeDocument;
            var fileName = docRef.textFrames.add();
            fileName.contents = docRef.placedItems[0].file.name;
            fileName.position = [15,0];
            charAttr = fileName.textRange.characterAttributes;
            charAttr.textFont = app.textFonts.getByName("Helvetica-Light");
            charAttr.size = 12;
            charAttr.fillColor = docRef.swatches[1].color;
            var newGroup = docRef.groupItems.add();
            newGroup.name = "NewGroup";
            newGroup.move( docRef, ElementPlacement.PLACEATBEGINNING );
            for(var c=2,len=docRef.swatches.length;c<len;c++)
            {
             var swatchGroup = docRef.groupItems.add();
             swatchGroup.name = docRef.swatches[c].name;
             var cols = 8
             var w=150;
             var h=50;
             var h_pad = 0;
             var v_pad = 10;
             var t_h_pad = 5;
             var t_v_pad = 10;
             var x=null;
             var y=null;
                    x= (w+h_pad)*(c% cols);
                    y=(h+v_pad)*(Math.floor((c+.01)/cols))*-1 ;
                    var textRectRef =  docRef.pathItems.rectangle(y- t_v_pad,x+ t_h_pad, w-(2*t_h_pad),h-(2*t_v_pad));
                    var colorNames = docRef.textFrames.areaText(textRectRef);
             colorNames.contents = docRef.swatches[c].name;
             colorNames.textRange.fillColor = docRef.swatches[c].color;
             colorNames.textRange.characterAttributes.textFont = app.textFonts.getByName("Helvetica-Light");
             colorNames.move( swatchGroup, ElementPlacement.PLACEATEND );
             swatchGroup.move( newGroup, ElementPlacement.PLACEATEND );
            }
            newGroup.position = [15,-15];