Skip navigation
danq59
Currently Being Moderated

Find and replace script using info from file name

Jun 11, 2012 6:38 PM

Hello all,

 

Very new to scripting, but willing to learn.

 

Is it possible to write an Illustrator (CS4) script that would find and replace 2 instances of text using the actual file name to supply the changing text?

 

Our current file naming system uses a Part Number, Catalog Number and Run Date. (example: 123456_ABCDEF_2014 JN)

We are currently saving a master file as the new Part Number and simply doing 2 separate find and replace functions for the Catalog Number and Run Date. Unfortunately we have over 500 parts to change and thought we could narrow down the 1000 find and replace to just 500. Although this may not appear like such a time saver, the key factor would mean entering the changing data just once per file, minimizing errors and eliminating proof reading the actual file.

 

Any thoughts and suggestion would be most welcomed. Thanks.

 

Here is what I have used for a simple find and replace script, but I cannot seem to find any information to connect the file name data.

 

[code]

var active_doc = app.activeDocument;

 

var search_string = /ABCDEF/gi; // g for global search, remove i to make a case sensitive search

var replace_string = "GHIJKL";

 

var text_frames = active_doc.textFrames;

 

var search_string2 = /1234 XX/gi; // g for global search, remove i to make a case sensitive search

var replace_string2 = "5678 YY";

 

var text_frames2 = active_doc.textFrames;

 

if (text_frames.length > 0)

{

    for (var i = 0 ; i < text_frames.length; i++)

      {

          var this_text_frame = text_frames[i];

           var new_string = this_text_frame.contents.replace(search_string, replace_string);

       

          

           if (new_string != this_text_frame.contents)

               {

                    this_text_frame.contents = new_string;

               }

         

      }

}

 

if (text_frames.length > 0)

{

    for (var i = 0 ; i < text_frames.length; i++)

      {

          var this_text_frame = text_frames[i];

           var new_string2 = this_text_frame.contents.replace(search_string2, replace_string2);

          

 

          if (new_string2 != this_text_frame.contents)

               {

                    this_text_frame.contents = new_string2;

               }

      }

}

[/code]

 
Replies
  • Currently Being Moderated
    Jun 12, 2012 1:46 AM   in reply to danq59

    As long as the the app.activeDocument is a file that has been saved to the file system then it has a file object associated to it… Look at properties fullName, name & path… You get the string from these and parse it for your text strings… Do consider a batch script for this there are plenty of examples here… You can just select a folder and do a 1000 find & replace in one click… Would that be a time saver…

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (1)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points