Search code examples
javascriptadobe-illustratorextendscript

javascript- replace text with part of the file name


I'm trying to create a script in adobe illustrator that will check if a file name contains "ph" +5 numbers. If it has been found then it will replace a part of a text with the match from the file name.

This is what I have so far I just can't get it to work, the text is replaced with "null"

var doc = app.activeDocument;

var name = doc.name;
var match = name.match(/ph\d{5}/);

for (i = 0; i < doc.textFrames.length; i++)
{
    doc.textFrames[i].contents = doc.textFrames[i].contents.replace(/ph00000/gi, match);
}

Solution

  • I'd try this:

    var doc = app.activeDocument;
    
    var match = doc.name.match(/ph\d{5}/);
    
    if (match != null) {
      for (i = 0; i < doc.textFrames.length; i++) {
        doc.textFrames[i].contents = doc.textFrames[i].contents.replace(/ph00000/gi, match[0]);
      }
    }