Copy link to clipboard
Copied
Hi there,
Can anybody tell me if it's possible to JavaScript Illustrators Find And Replace text function?
I know it's possible in InDesign but I can't find anything for Illustrator.
Many Thanks,
Nik
Hi Nik, no need to dig down to words, we can search the whole textFrame.contents. Here's a working sample
...var active_doc = app.activeDocument;
var search_string = /blah/gi; // g for global search, remove i to make a case sensitive search
var replace_string = "lalala";
var text_frames = active_doc.textFrames;
if (text_frames.length > 0)
{
for (var i = 0 ; i < text_frames.length; i++)
{
var this_text_frame = text_frames;
var new_string = this_text_frame.contents.replace(search
Copy link to clipboard
Copied
Hi Carlos! I'm trying to use your code with a folder full of documents, but Illustrator keeps crashing on me. Can you please help?
// Illustrator Find And Replace
// Original code from: https://forums.adobe.com/thread/858021
var search_string = /blah/gi; // g for global search, remove i to make a case sensitive search
var replace_string = "lalala";
var sourceFolder, files, fileType, sourceDoc;
// Select the source folder.
sourceFolder = Folder.selectDialog( 'Select folder with Illustrator files', '~' );
// If a valid folder is selected
if ( sourceFolder != null )
{
files = new Array();
fileType = '*.ai';
// Get all files matching the pattern
files = sourceFolder.getFiles( fileType );
if ( files.length > 0 )
{
for ( i = 0; i < files.length; i++ )
{
sourceDoc = app.open(files); // returns the document object
//REPLACE CODE BEGIN
var text_frames = sourceDoc.textFrames;
if (text_frames.length > 0)
{
for (var i = 0 ; i < text_frames.length; i++)
{
var this_text_frame = text_frames;
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;
}
}
}
//REPALCE CODE END
sourceDoc.save();
// Close current without saving
sourceDoc.close(SaveOptions.DONOTSAVECHANGES);
}
}
else
{
alert( 'No matching files found' );
}
}
Copy link to clipboard
Copied
Hi Carlos,
Is it possible to have a similar script that would find numeric values in a text frame? Example "Smith12" to find and select only "12". I don't want to replace it with anything just a selection of the number. Any help would be great. Thank you
Copy link to clipboard
Copied
Hi born2wrestle, here you go
select one or more text frames to process selection, or don't select anything to process all frames in the active document
// script.name = highlight_numbers,jsx
// script.description = highlights all numbers in either the selected frames or the whole doucument if nothing is selected.
// script.requirement = Illustrator must be open before running the script
// script.parent = CarlosCanto // 11/23/19
// script.support = canto29 at gmail d0t c0m;
function main () {
var idoc = app.activeDocument;
if (idoc.selection.length == 0) {
var tframes = idoc.textFrames;
}
else {
var tframes = [];
var tframe;
for (var a=0; a<idoc.selection.length; a++) {
tframe = idoc.selection[0];
if (tframe.typename == 'TextFrame') {
tframes.push(tframe);
}
}
}
var range, framecontent;
var reg = /\d+/g;
var rangesToSelect = [];
for (var b=0; b<tframes.length; b++) {
tframe = tframes[b];
framecontent = tframe.contents;
var result;
while (result = reg.exec(framecontent)) { // this always returns 1 element
//$.writeln(result[0] + " | " + result.index);
//$.writeln(result[0].length);
range = tframe.characters[result.index];
range.length = result[0].length;
rangesToSelect.push(range);
}
}
idoc.selection = null;
idoc.selection = rangesToSelect;
}
main();
Copy link to clipboard
Copied
Hi Carlos
i want to select all "*"
because i want change color and formating to super script
i have more than 100 artboards
is this possible
Copy link to clipboard
Copied
Hi raviraj, what do you mean? do you want a script that only selects all text items and stops there?
Copy link to clipboard
Copied
Yes i want to find and select all at once whatever i find
Copy link to clipboard
Copied
Hi @CarlosCanto
Is this possible ?
Copy link to clipboard
Copied
I'm new to scripting and I can't get the simplest possible code to work. I found this thread and I copied one lines, which was "var docRef = app.activeDocument; ". It won't compile in the mac script editor. See this screenshot:
I just can't write the simplest possible code. Please help. 
Copy link to clipboard
Copied
Save the file as a .jsx file. Then, while your document is open in Illustrator, go to File > Scripts > Other Script. Find your script and open it. This will run the script.
Note that var docRef = app.activeDocument is just a reference to the document, so running a script with just this line won't do anything.