Copy link to clipboard
Copied
My document contains 2 places that I want to change a particular number.
Here is the code that I currently have to change the number I want.
#target illustrator
var doc = app.activeDocument;
var allText = doc.textFrames;
for (var i = 0; i < allText.length; i++) {
if (allText.contents.substring(0,11) == "UENR3640-05"){
var mediaNumber = allText.contents.replace("UENR3640-05", "UENR3640-06");
allText.contents = mediaNumber;
}
}
The yellow box is a multi-line that contains 3 different font size. So when I run my above script it changes the number in the gray box fine. However the yellow box changes to this....
Any help that would ignore the formatting of the text and just change the text I have setup? I would love to also have the option for this to run on ALL open documents.
This is on a Windows 7 64-bit PC with Adobe Illustrator CS4
Thanks in advance!
Make your code above into a function which accepts a document object as an argument and make that inside of a loop which goes through all the documents.
...#target illustrator
function test(){
function processDoc(doc){
var s = /UENR3640-05/gi;
var replacer = "UENR3640-06", result;
var allText = doc.textFrames;
for (var i = 0; i < allText.length; i++) {
while (result = s.exec(allText.contents)) {
try {
aCon = allText.characters[result.index];
Copy link to clipboard
Copied
Hello!
We just did this a few days ago, you will probably be able to use the answer here for your exact goals!
Copy link to clipboard
Copied
I saw that yesterday. The problem I have is that if I run the code....
#target illustrator
var s = /UENR3640-05/gi;
var replacer = "UENR3640-06", result;
var atf = activeDocument.textFrames[0];
while (result = s.exec(atf.contents)) {
try {
aCon = atf.characters[result.index];
aCon.length = result[0].length;
aCon.contents = replacer;
} catch (e) {};
}
It will change the text in the gray box....but not in the yellow box. So it isn't changing my formatting....but it isn't changing my text either
Copy link to clipboard
Copied
Ok so update....I realized that I wasn't looping through all of the text in the documents. So how can I make this updated code loop through and find ALL of the matches....on all open documents??
#target illustrator
var doc = app.activeDocument;
var s = /UENR3640-05/gi;
var replacer = "UENR3640-06", result;
var allText = doc.textFrames;
for (var i = 0; i < allText.length; i++) {
while (result = s.exec(allText.contents)) {
try {
aCon = allText.characters[result.index];
aCon.length = result[0].length;
aCon.contents = replacer;
} catch (e) {};
}
}
Copy link to clipboard
Copied
Make your code above into a function which accepts a document object as an argument and make that inside of a loop which goes through all the documents.
#target illustrator
function test(){
function processDoc(doc){
var s = /UENR3640-05/gi;
var replacer = "UENR3640-06", result;
var allText = doc.textFrames;
for (var i = 0; i < allText.length; i++) {
while (result = s.exec(allText.contents)) {
try {
aCon = allText.characters[result.index];
aCon.length = result[0].length;
aCon.contents = replacer;
} catch (e) {};
}
}
};
for(var i=0; i<app.documents.length; i++){
app.documents.activate();
processDoc(app.activeDocument);
};
};
test();
Copy link to clipboard
Copied
That works great! I still have to have it run the function multiple times to catch all the instances...but it runs fast! Thanks again Silly-V​
Copy link to clipboard
Copied
I am sure you can change it so that you don't have to run it multiple times, by adding another argument, an array of search strings, and another loop to go through them all. Instead of making your regexp with slashes, you can use the new RegExp() constructor and put your string & flags in there.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now