Copy link to clipboard
Copied
So I am wanting to find a part number in a document. There could be several part numbers. Thanks to some of the users here I have code that currently looks like this....
#target illustrator
function changeData() {
function processDoc(doc) {
// OLD PART NUMBER
var oldPart = /111-1111/gi;
// NEW PART NUMBER
var replacePart = "111-1111",
result;
var allText = doc.textFrames;
// SEARCH AND CHANGE LOOP FOR PART #
while (result = oldPart.exec(allText.contents)) {
try {
aCon = allText.characters[result.index];
aCon.length = result[0].length;
aCon.contents = replacePart;
} catch (e) {};
}
// SCAN THRU ALL OPEN DOCUMENTS
for (var i = 0; i < app.documents.length; i++) {
app.documents.activate();
processDoc(app.activeDocument);
};
};
changeData();
Now please excuse my lack of knowledge but I was wondering if it is possible to do a list search and list change. I know this isn't the syntax to use but just to get the point across....
// OLD PART NUMBER
var oldPart = /111-1111 || 222-2222 || 333-3333/gi;
// NEW PART NUMBER
var replacePart = "444-4444 || 555-555 || 666-6666", result;
So if part 111-1111 is found it would change it to 444-4444. if part 222-2222 is found it would change it to 555-5555. if part 333-3333 is found it would change it to 666-6666.
Can anyone explain how to do this within my function??
Thanks in advance!
Copy link to clipboard
Copied
Looks like you can just put your old part numbers and new ones into 2 arrays and go through them.
Copy link to clipboard
Copied
Ok so how do you apply an array to a regexp?
Copy link to clipboard
Copied
Ok so I understand how to search for multiple items in a regexp...but how do i make it match to an array position for the new part or results???
#target illustrator
function changeData() {
function processDoc(doc) {
// OLD PART NUMBER
var oldPart = /111-1111|222-2222|333-3333/gi;
// NEW PART NUMBER
var replacePart = "111-1111", result;
var allText = doc.textFrames;
// SEARCH AND CHANGE LOOP FOR PART #
while (result = oldPart.exec(allText.contents)) {
try {
aCon = allText.characters[result.index];
aCon.length = result[0].length;
aCon.contents = replacePart;
} catch (e) {};
}
// SCAN THRU ALL OPEN DOCUMENTS
for (var i = 0; i < app.documents.length; i++) {
app.documents.activate();
processDoc(app.activeDocument);
};
};
changeData();
Copy link to clipboard
Copied
Just use a simple array and make the 'process all documents' loop be inside of a loop that goes through your array of items to replace.
var replaceThese = ["111-1111", "222-2222"];
var withThese = ["333-3333", "555-5555"];
for(var i=0; i<replaceThese.length; i++){
for (var j = 0; j < app.documents.length; j++) {
app.documents[j].activate();
processDoc(app.activeDocument, replaceThese, withThese);
};
};
Of course you will have to modify your processDoc function to accept the replacee with the replacer as 2 additional arguments.
Copy link to clipboard
Copied
I understand what you are saying and thank you for that. However I guess I didn't give all the information....sorry about that.
The part number I am looking to replace is within a sentence (meaning it isn't in a text frame by itself).
Plus I also need to keep the formatting.
Here is an example of what the document would contain.
PART NUMBER: 111-1111, CHANGE: 05, VERSION: - (All of that is in one text frame)
PART NUMBER: 222-2222, CHANGE: 01, VERSION: HE (All of that is in one text frame)
Both of the above text frames could be on one document....or could be on separate documents.
Copy link to clipboard
Copied
Ya so you would need to change up your processDoc function to go something like this:
function processDoc(doc, oldTxt, newTxt) {
// OLD PART NUMBER
var oldPart = new RegExp(oldTxt, "gi");
// NEW PART NUMBER
var replacePart = newTxt, result;
Copy link to clipboard
Copied
I'm sorry Silly-V​.... I appreciate your help but this is more advanced than I know how to put together. I'm new to regexp and I don't usually do functions.... lol
So basically what I am saying is I am a graphic designer and suck at programming.
Copy link to clipboard
Copied
Don't worry you don't suck, you can do it, you have the awesome powers of greatness and you can overcome any obstacle!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now