View Full Version : indesign JS command "document.close" renders script useless - need help!

03-20-2012, 07:51 PM
hey guys,

I have a (seemingly) fully working indesign script which opens up documents and indexes the contents.
it puts out the index fine, as long as I do not close the documents.

right now it
- opens
- analyzes
- opens next
- analyzes
- spits out the index of all docs

if I add a close in the loop after the analyze I get an Error 45 / Object is invalid alert on my index list

if (myFile != null) {
try {
// main routine. documents are opened , analyzed, closed, next.
for (x=0;x<myFile.length;x++) {
myOpenFile = myFile[x]
myDocument = app.open(myOpenFile);
mySearchPresets(); //also calls the search function and returns "myFoundList"

app.activeDocument.close(); // if I leave out this line everything works great.

} catch (e) {
alert ("Es ist ein Fehler beim Öffnen von\n" +myFile+ "\naufgetreten!\n\n" + e, "Fehler");

after this it jumps to this function

function myArtNumList(){
mySearchResultList = [];
myFinalList = [];
i = 0;
a = 0;
//converts text into integer
//myArtNum= +myArtNum;
//alert ("ich sortiere");
//alert (myFoundList[0]);
while (i < myFoundList.length) {

myArtNum = myFoundList[i].contents; // this is the part I get the 45 error on ...

// sort and output is omitted here

any thoughts on this? thanks for your help!

Old Pedant
03-20-2012, 09:36 PM
No idea what your myFoundtList contains, but if it contains *REFERENCES* to data in the document then of course the error makes sense.

Possibly you need to clone the objects/text/whatever you find before putting it in myFoundList, so that the list references JS objects in memory instead of objects in the document.

03-20-2012, 10:16 PM
thanks! that just might be it! it searches for different types of text and puts the results into "myFoundList". so I would probably end up with the reference to the text instead of the text itself.

Do you have a quick hint how I could clone the contents so that the reference will get lost?

EDIT: I'm on it. I just might have found a way...

Old Pedant
03-20-2012, 10:29 PM
Just do

var copy = new String( reference_to_contents );

Or you can even simply do

var copy = "" + reference_to_contents;

If the contents are other than a string, you can find ways to clone objects via google query: "javascript clone object"

03-20-2012, 10:49 PM
Thank you so much! That was the hint I needed!