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

New to Scripting

New Here ,
Jun 16, 2017 Jun 16, 2017

Copy link to clipboard

Copied

I was wondering if I could get some help. I am writing my first script. I have hundreds of AI files that use multiple layers. One of the layers is called "CIRCUITS" and I need to change all of the color abbreviations to a new standard and am trying to automate doing it all at once instead of using find and replace for each color I need to change. Ultimately, I wish I could batch this task to a specific folder and run the script where it would rip through all the AI files, change the colors and save them back in the folder but I have no clue how to go about doing that.

So, I am trying to write a script that will run a find and replace on all open documents in AI. The script is finding and replacing BUT...it is doing so on every layer instead of just the specific layer "CIRCUITS" that resides on every AI file. I do not know how to specify the search and replace to only run on that layer.

Here is the script I wrote. Please keep in mind that I am a novice and this is the first script I have ever written:

function FindAndReplaceScript_AllOpenDocuments(){     

        for(var i=app.documents.length -1; i > -1;  i--){     

            app.documents.activate(); 

            var aDoc = app.documents

            var  fileName = app.documents.name

            var doc = app.activeDocument;

           

           

            //declare search and replace variables

            var searchBrown = "YE";    

            var replaceBrown = 'TEST';

           

            var searchWhite = "WT";    

            var replaceWhite = 'WH';

           

            var searchOrange = "OR";    

            var replaceOrange = 'OG';

           

            var searchYellow = "YL";    

            var replaceYellow = 'YE';

           

            var searchLgreen = "LG";    

            var replaceLgreen = 'GN';

           

            var searchLblue = "LB";    

            var replaceLblue = 'BU';

           

            var searchTan = "TN";    

            var replaceTan = 'BG';

            var theTF = aDoc.textFrames;     

           

            if (theTF.length > 0) {     

              

              

               //Where the replace happens

                for (var a = 0 ; a <theTF.length; a++) {     

                    var aTF = theTF;     

                   

                    //add ".replace(search variable, replace variable to end of this argument to add another variable)

                    var newString = aTF.contents.replace(searchBrown, replaceBrown).replace(searchWhite, replaceWhite).replace(searchOrange, replaceOrange).replace(searchYellow, replaceYellow).replace(searchLgreen, replaceLgreen).replace(searchLblue, replaceLblue).replace(searchTan, replaceTan);     

                    if (newString != aTF.contents) {     

                        theTF.contents = newString;     

                  //End replace     

                      

                    }     

                } 

            //Save the file

            aDoc.save();

           // aDoc.saveAs ("/c/Convert/test.ai",".svgz")();

            } 

        } 

    };     

    FindAndReplaceScript_AllOpenDocuments();  

I would sure appreciate any help/guidance you could provide.

TOPICS
Scripting

Views

234

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
Valorous Hero ,
Jun 16, 2017 Jun 16, 2017

Copy link to clipboard

Copied

LATEST

One optimization you can use is to make your search/replace values into an object or a couple of arrays so that you can do your replacements in a loop instead of the chained replace commands.

For only targeting text in a certain layer, you can do this a number of ways.

The simplest is to just process the text in that layer:

var myLayer = doc.layers.getByName("CIRCUITS");

var textFrames = myLayer.textFrames;

This will reference all the text frames included immediately in the layer, but only if they are not nested in any way. For example, text frames inside groups or nested layers are not found using the above lines. If you have nested items, it's still possible to get at them but you will have to get creative with it. Such as, you can select all art (layer.hasSelectedArtwork = true) on the target top-level layer which will select all nested items (which must not be locked or hidden) and then when you go through the doc.textFrames which does take into account all items nested or not, you can see which ones are selected like this:

function test(){

  var doc = app.activeDocument;

  doc.layers.getByName("Layer 2").hasSelectedArtwork = true; // assume your Layer 2 has a nested layer or groups containing more text frames

  for(var i=0; i<doc.textFrames.length; i++){

    if(doc.textFrames.selected){

      alert(doc.textFrames.contents);

    }

  };

};

test();

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