0

i have indesign document run with Javascript in it. which contains multiple pages.

there are some text frames with name txtDate.

in all pages, i want to change all of them with javascript.

with everyItem() => it changes all text frames

with itemByName("txtDate") => it changes only one item.

what is your suggestion any tip for change all text with same label(txtDate)

thanks for suggestions.

planet360
  • 3
  • 2

2 Answers2

2

http://www.indiscripts.com/post/2013/05/indesign-scripting-forum-roundup-4#hd4sb1

Then

var items = app.activeDocument.allPageItems.whose ( function ( item ) {
 return (item instanceof TextFrame ) && item.label == "txtDate";
});
Loic
  • 2,173
  • 10
  • 13
  • Should be item.name instead of item.label, I think. Also I would check for the name first (i.e. place it before the &&), since the frames named "txtDate" are most likely a subset of all textFrames, so the script has to check less and will run faster. – mdomino Sep 29 '16 at 21:49
  • item.label is the correct way to reference text frame script label – Nicolai Kant Sep 30 '16 at 06:16
  • @mdomino allPageItems is not a collection of frames but a collection of ALL page items. Reversing the order in an && clause will have absolutely no effect on the end result. – Nicolai Kant Sep 30 '16 at 06:29
  • @NicolaiKant Sorry, you are right regarding the script labels of course, I thought he needs to check for script names. – mdomino Sep 30 '16 at 07:13
  • @NicolaiKant Yes, reversing the order in the && clause *does* have an effect. That's because of how && clauses work internally. If the first checked operand evaluates to false, the following values will not even be checked, instead the script continues. On the other hand if the first operand evaluates to true, then the following operand also needs to be evaluated. Therefore it makes sense to always put the operand first that is less likely to evaluate to true. I just tested and timed this specific scenario here and checking the label check first was consistently faster. – mdomino Sep 30 '16 at 07:19
  • OK, I did not realise about short circuiting on the left site of && operator. My other point was that if you loop through all page items and there are many which are not text frames then reversing the order will become less efficient. – Nicolai Kant Sep 30 '16 at 08:30
  • @NicolaiKant No, then it will not become less efficient. It needs to check every item anyways, so whenever a pageItem is not labeled "txtDate" it cancels right away, only on those items that are labeled "txtDate" it will also check, if it is a textFrame or not. Example: You have 2000 pageItems, 500 of those are textFrames, 50 of those are labled "txtDate". When the script checks the lable first, it will cancel right after the first check in 1950 cases (and only in the 50 relevant cases make the second). If it checks for textFrame first, it will only cancel after the first check in 1500 cases. – mdomino Sep 30 '16 at 11:32
  • @NicolaiKant The only case where it would be less efficient to switch the entries is, when there are a lot of pageItems that are labled "txtDate" that are *not* textFrames. But I think this would not be the case here. Having said all that, the differences are probably not that great and not really relevant, I just wanted to give a hint, how one can make his script more efficient. :) – mdomino Sep 30 '16 at 11:34
  • 1
    Interesting discussion. I do think allPageItems (which is not actually a collection but an array ;) ) is useful if you can't presume the hierarchy of the object within the document (might this item be part of a group, maybe inside another group, etc). Given the speediness, you are right to point it out. In the snippet I just wanted to introduce the whose implementation by InDiScripts which I use very frequently. – Loic Sep 30 '16 at 13:21
0

I think, you will need to loop through the documents textFrames testing for name being txtDate, when found match, apply your changes and continue with the loop.

#target indesign
var myDoc = app.documents[0];    
var myFrames = myDoc.textFrames;

for (var i=0;  i< myFrames.length ; i++) {
    if (myFrames[i].label == "txtDate"){            
        alert ("match"); // you changes here           
        }   
}
Nicolai Kant
  • 1,391
  • 1
  • 9
  • 23
  • This approach only works, if all the relevant textFrames are on the top level. If they are nested in a group they will not found. So looping through `allPageItems` instead and additionally checking if the pageItems are a textFrame will give you the correct result. – mdomino Sep 29 '16 at 21:47
  • Do you mean a text frame grouped with the other page elements or anchored text frame inside another text frame? – Nicolai Kant Sep 30 '16 at 06:22