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

Script that finds specific text and change base from filename

Explorer ,
Nov 22, 2022 Nov 22, 2022

Copy link to clipboard

Copied

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.

 

 

 

TOPICS
Feature request , How to , Scripting , Tools

Views

116

Likes

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

correct answers 2 Correct answers

Contributor , Nov 22, 2022 Nov 22, 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;
    }
  }
})();

 

 change base from filename.gif

Likes

Translate

Translate
Contributor , Nov 22, 2022 Nov 22, 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
...

Likes

Translate

Translate
Community Expert ,
Nov 22, 2022 Nov 22, 2022

Copy link to clipboard

Copied

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

Likes

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
Explorer ,
Nov 22, 2022 Nov 22, 2022

Copy link to clipboard

Copied

First, find 4pt font size text 

RoelVerano_0-1669173869799.png

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

RoelVerano_1-1669173900798.png

The output should be like this.

RoelVerano_2-1669173941285.png

 



 

 

Likes

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
Contributor ,
Nov 22, 2022 Nov 22, 2022

Copy link to clipboard

Copied

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;
    }
  }
})();

 

 change base from filename.gif

Likes

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
Explorer ,
Nov 22, 2022 Nov 22, 2022

Copy link to clipboard

Copied

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

Likes

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
Contributor ,
Nov 22, 2022 Nov 22, 2022

Copy link to clipboard

Copied

/*
  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) {}

Likes

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
Explorer ,
Nov 22, 2022 Nov 22, 2022

Copy link to clipboard

Copied

This is amazing! Thanks a lot! 

Likes

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
Community Expert ,
Nov 23, 2022 Nov 23, 2022

Copy link to clipboard

Copied

LATEST

Great job, Sergey.👍

Likes

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