Copy link to clipboard
Copied
I am writing script for Adobe indesign server 2023 to access the links which code is better.
can anyone tell me the methods count() and property length difference or which is better for server version script .
below code with length property
var documentLinks = app.documents[0].links;
for (stLnks = 0; stLnks < documentLinks.length; stLnks++){
documentLinks[stLnks].unlink();
}
below code with count property
var documentLinks = myDoc.links;
for (stLnks = 0; stLnks < documentLinks.count(); stLnks++){
documentLinks[stLnks].unlink();
}
Copy link to clipboard
Copied
.length or .count() -- it makes no difference. What does make a difference is using
var documentLinks = app.documents[0].links.everyItem().getElements();
(instead of var documentLinks = app.documents[0].links).
With var documentLinks = app.documents[0].links; you use a collection, while .everyItem().getElements(); converts the collection to an array, which almost always processes much quicker.
Copy link to clipboard
Copied
Thanks @Peter Kahrel
"Some time when I am using the .count() method in InDesign Server, it's not embedding links properly. However, when I am using .length one by one, all the links embed properly.
So, I have a doubt: count() is a method and length is a property. When can I use count() and when can I use length?"
Copy link to clipboard
Copied
> I have a doubt: count() is a method and length is a property. When can I use count() and when can I use length?
Well, I always use length, on bith Desktop and Server, and I've never seen a problem. So I would say use length and you can't go wrong. Why count() sometimes gives a problem I've no idea.
Copy link to clipboard
Copied
Hi samy@123 As @Peter Kahrel explained links are a collection and not an arrayācollections and arrays have different methods. You can find the links collectionās methods here:
https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Links.html
Also as Peter notes you can force a collection into an Array via getElements()āin that case you would not be able to use the collection methods
https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Array.html
In your example using a collection would let you avoid the for loopāthis single line would embed all of the document links:
var documentLinks = app.documents[0].links.everyItem().unlink()