Skip to main content
Known Participant
November 23, 2022
Answered

Script that finds specific text and change base from filename

  • November 23, 2022
  • 2 replies
  • 658 views

Is there a possible Batch Script that finds a specific text? for example,

Finds 4pt text in the file and changes the text base on the file name.

 

Thanks in advance.

 

 

 

This topic has been closed for replies.
Correct answer Sergey Osokin
/*
  Batch search for a TextFrame of a given size and replace its contents with the file name
  Discussion: https://community.adobe.com/t5/illustrator-discussions/script-that-finds-specific-text-and-change-base-from-filename/m-p/13365933#M344679
  Author: Sergey Osokin, email: hi@sergosokin.ru
  Check other scripts: https://github.com/creold
*/

//@target illustrator
app.preferences.setBooleanPreference('ShowExternalJSXWarning', false); // Fix drag and drop a .jsx file

// Main function
function main() {
  var size = 4; // pt
  var dir = Folder.selectDialog('Select the source folder...');
  if (dir !== null) {
    var files = getAllFiles(decodeURI(dir), '.ai');
    for (var i = 0, len = files.length; i < len; i++) {
      changeText(files[i], size);
    }
  }
}

function getAllFiles(dir, ext) {
  var fList = Folder(dir).getFiles(),
      files = [];
    for (var i = 0, len = fList.length; i < len; i++) {
    if (fList[i] instanceof Folder) {
      files = files.concat(getAllFiles(fList[i], ext));
    } else if (fList[i] instanceof File) {
      if (fList[i].name.indexOf(ext) > -1) {
        files.push(fList[i]);
      }
    }
  }
  return files;
}

function changeText(f, size) {
  var doc = app.open(f);
  var docName = doc.name.replace(/\.[^\.]+$/, '');
  var tf = doc.textFrames;
  for (var i = 0, len = tf.length; i < len; i++) {
    if (tf[i].textRange.characterAttributes.size == size) {
      tf[i].contents = docName;
    }
  }
  doc.save();
  doc.close();
}

// Run script
try {
  main();
} catch (e) {}

2 replies

Sergey Osokin
Inspiring
November 23, 2022

This is a simple way if all characters of the string are 4 pt

 

(function () {
  var doc = app.activeDocument;
  var docName = doc.name.replace(/\.[^\.]+$/, '');
  var tf = doc.textFrames;
  var size = 4; // pt

  for (var i = 0, len = tf.length; i < len; i++) {
    if (tf[i].textRange.characterAttributes.size == size) {
      tf[i].contents = docName;
    }
  }
})();

 

Known Participant
November 23, 2022

This is actually working somehow can u make it for a batch for example all of AI files under one folder?

Sergey Osokin
Sergey OsokinCorrect answer
Inspiring
November 23, 2022
/*
  Batch search for a TextFrame of a given size and replace its contents with the file name
  Discussion: https://community.adobe.com/t5/illustrator-discussions/script-that-finds-specific-text-and-change-base-from-filename/m-p/13365933#M344679
  Author: Sergey Osokin, email: hi@sergosokin.ru
  Check other scripts: https://github.com/creold
*/

//@target illustrator
app.preferences.setBooleanPreference('ShowExternalJSXWarning', false); // Fix drag and drop a .jsx file

// Main function
function main() {
  var size = 4; // pt
  var dir = Folder.selectDialog('Select the source folder...');
  if (dir !== null) {
    var files = getAllFiles(decodeURI(dir), '.ai');
    for (var i = 0, len = files.length; i < len; i++) {
      changeText(files[i], size);
    }
  }
}

function getAllFiles(dir, ext) {
  var fList = Folder(dir).getFiles(),
      files = [];
    for (var i = 0, len = fList.length; i < len; i++) {
    if (fList[i] instanceof Folder) {
      files = files.concat(getAllFiles(fList[i], ext));
    } else if (fList[i] instanceof File) {
      if (fList[i].name.indexOf(ext) > -1) {
        files.push(fList[i]);
      }
    }
  }
  return files;
}

function changeText(f, size) {
  var doc = app.open(f);
  var docName = doc.name.replace(/\.[^\.]+$/, '');
  var tf = doc.textFrames;
  for (var i = 0, len = tf.length; i < len; i++) {
    if (tf[i].textRange.characterAttributes.size == size) {
      tf[i].contents = docName;
    }
  }
  doc.save();
  doc.close();
}

// Run script
try {
  main();
} catch (e) {}
m1b
Community Expert
Community Expert
November 23, 2022

Hi @Roel Verano, could you clarify?

1. find all 4pt text

2. do what to it? Set baseline?

3. Something to do with filename?

 

If you are having trouble describing, you could try posting a sample file showing before and after texts.

- Mark

Known Participant
November 23, 2022

First, find 4pt font size text 

Second change the 4pt font size text into the file name base on the file name

The output should be like this.