You are correct. the code i posted would have to be duplicated for the other array. There are two parallel arrays that contain the text frames. my code only handles one of them and, as previously stated, assumes the sorting has already been completed.
So you would run my loop twice (or better yet, change one text frame from each array within each execution of the loop, like so):
function test()
{
var docRef = app.activeDocument;
var listOne = []; //sorted array of text frames;
var listTwo = []; //sorted array of text frames;
if(listOne.length>0)
{
var length = listOne.length;
for(var x=0;x<length;x++)
{
listOne.contents = "(" + (x+1) + ") " + thisFrame.contents;
listTwo.contents = "(" + (x+1) + ") " + thisFrame.contents;
}
}
else
{
alert('No text frames available!');
}
}
test();
My previous responses addressed the fact that the sorting would need to be revisited because you cannot sort an array of text frame objects with a simple array.sort() like wolfEdition has in his/her original snippet.
The question i was attempting to answer is in the title of the post, and was clarified here in the initial response to my first answer:
| My issue is I don't know how to get the formatting I want of (#) inserted back into the textFrame when it finds an array item. |
So my answer became more long winded than it needed to, but all i was trying to convey is that in order to update the contents of the text frame, OP simply needs to re-assign the contents of the given text frame like so:
myTextFrame.contents = "(" + (index + 1) + ") " + myTextFrame.contents
The way i understand it, this is the question at hand. OP used the string.replace() method but never assigned the return value to the text frame at hand, it was simply updated in the array which is not associated with the text frame.
I know, this is not really the best way, but quick and dirty:
// required: only matching pairs of textFrames with same contents
// eg 111, 111, apple, apple, Banana, Banana // no other (single) textFrames allowed
// result (1) 111, (1) 111, (2) apple, (2) apple, (3) Banana, (3) Banana
var aDoc = app.activeDocument;
var allText = aDoc.textFrames;
var rePop = [];
for (var i = 0; i < allText.length; i++) {
allText.name = allText.contents;
rePop.push(allText.contents);
}
rePop.sort();
var listOne = [];
//var listTwo = []; // only need if you want to create a better error handling as in this snippet
for (var i = 0; i < rePop.length; i+=2) {
listOne.push ( rePop );
//listTwo.push ( rePop[(i+1)] );
}
if (rePop.length % 2 == 0) {
for (var i = 0; i < listOne.length; i++) {
try {
changeTextframes ( listOne );
changeTextframes ( listOne );
} catch (e) { alert ("missing pairs"); }
}
} else { alert ("different lists.length") };
function changeTextframes ( x ) {
var tf = aDoc.textFrames.getByName ( x );
tf.contents = "(" + (i+1) + ") " + x;
tf.name = tf.contents;
}
wolfeEdition and williamadowling
Have fun
