Skip to main content
Inspiring
December 18, 2023
Answered

Can’t open the same InDesign document twice

  • December 18, 2023
  • 2 replies
  • 1208 views

Short version: 

I’m trying to open the same indesign file twice with the same function.
It works fine the first time, but fails without error message the second time.

 

I’m using InDesign CC 2022 on macOS. 

 

// 1
var res = open_docs(["file.indd","~/Desktop/file.indd], true);
var the_doc = res[0];
// Read my IDs
the_doc.close(SaveOptions.NO);

// 2
var res = open_docs(["file.indd","~/Desktop/file.indd], false);
var the_doc = res[0];
// Update my IDs
the_doc.close(SaveOptions.YES);


function open_doc(docs_i, open_hidden) {

	var this_doc_name       = docs_i[0];
	var this_doc_fullName   = docs_i[1];

	// Check if docs_i is open
	var this_doc = app.documents.itemByName(this_doc_name);
	if(	this_doc.isValid && 							
			// ^ A document with the correct name is open, and ...
			(this_doc.fullName.toString() == this_doc_fullName.toString())
			// ^ the document has the correct path.
		){
		doc_open = true;
	} else {
		doc_open = false;
		// open the document
		this_doc = app.open(this_doc_fullName, true);
		// this_doc = app.open(this_doc_fullName, !open_hidden);
		// doesn’t get here, the second time this is run.
		app.activeDocument = this_doc;
	}
	return [this_doc, doc_open];
}

 

Full story:

In a first step, I open all documents of a book and search for custom IDs (= Text in a text frame). 

Each document is processed after the other. Before I open a document I check if it is already open.
Open documents stay open, all others are closed after processing.

 

In the second step I want to open the documents again and update the IDs. 

I’m using the exact same function to open, but this time it fails before the document is opened.

The script doesn’t do anything after the line with open(), but I don’t get any error message. 

 

In my test scenario (two small docs in a book) everything runs quite fast. 
So I wonder, if it might have something to do with the file system. 

Could it help, to open the document as a copy in step one?

 

Also: I’m running the function from a dialog (palette)

 

Thanks for any help / idea. 

 

Regards, Martin 

 

 

This topic has been closed for replies.
Correct answer C330 S

I’m sorry for wasting everybodys time!

The simplified code example I posted, doesn’t even show the actual problem

 

This is what happened: 

 

In step one, I open every file like this: 

 

var this_doc_fullName = app.books[0].bookContents[i].fullName;
app.open(this_doc_fullName, true);

 

 

this_doc_fullName is a file object (or at least behaves like one). 

I then add this_doc_fullName to an object. 

 

In step two, I tried to open this_doc_fullName from the object. 

I didn’t realize, that the saved item was no longer an File object, but a string:

 

app.open(document_path_as_string, true);        // doesn’t work

 

app.open() expects a File object, not a string, so it doesn’t work.

 

To make it work, I only needed to create a File object from the path:

 

app.open(File(document_path_as_string), true);  // works

 

 
Regards, Martin

2 replies

Peter Kahrel
Community Expert
Community Expert
December 20, 2023

@Robert at ID-Tasker 

>app.books[0].bookContents[i].fullName is a String 

 

It isn't: it's a File object. In a console a file name is printed, but it is a File object, which can be verified by

 

app.books[0].bookContents[i].fullName.constructor.name

 

P.

C330 SAuthor
Inspiring
December 20, 2023

@Peter Kahrel Thank’s! This explains the behaviour perfectly. 

Regards, Martin 

C330 SAuthorCorrect answer
Inspiring
December 18, 2023

I’m sorry for wasting everybodys time!

The simplified code example I posted, doesn’t even show the actual problem

 

This is what happened: 

 

In step one, I open every file like this: 

 

var this_doc_fullName = app.books[0].bookContents[i].fullName;
app.open(this_doc_fullName, true);

 

 

this_doc_fullName is a file object (or at least behaves like one). 

I then add this_doc_fullName to an object. 

 

In step two, I tried to open this_doc_fullName from the object. 

I didn’t realize, that the saved item was no longer an File object, but a string:

 

app.open(document_path_as_string, true);        // doesn’t work

 

app.open() expects a File object, not a string, so it doesn’t work.

 

To make it work, I only needed to create a File object from the path:

 

app.open(File(document_path_as_string), true);  // works

 

 
Regards, Martin
Robert at ID-Tasker
Legend
December 18, 2023
quote
var this_doc_fullName = app.books[0].bookContents[i].fullName;
app.open(this_doc_fullName, true);

 

this_doc_fullName is a file object (or at least behaves like one). 

I then add this_doc_fullName to an object. 

 

In step two, I tried to open this_doc_fullName from the object. 

I didn’t realize, that the saved item was no longer an File object, but a string:

 

By @C330 S

 

app.books[0].bookContents[i].fullName is a String - where did you get that it would be an Object?

 

C330 SAuthor
Inspiring
December 19, 2023
Hi Robert, 
to be honest: I have no idea, why it behaves like that. 

 

I’m buliding an array of book documents like this:

 

var sel_book = app.books[0];
var docs = [];

for(i=0; i<sel_book.bookContents.length; i++){
    docs.push([
        sel_book.bookContents[i].name,
        sel_book.bookContents[i].fullName
    ]);
}

 

doc_array should look like this:

 

[
    ["file_1.indd", "~/documents/file_1.indd"],
    ["file_2.indd", "~/documents/file_2.indd"]
]

 

I then use the function open_doc() from my initial post to open each document:

 

var r = open_doc(docs[i],open_hidden);

 

I altered the else part of open_doc() like this:

 

    } else {
        doc_open = false;

        alert(typeof(this_doc_fullName));

        // Convert to File object, if only string is given:
        if(typeof(this_doc_fullName) == "string"){
            this_doc_fullName = File(this_doc_fullName);
        }

        this_doc = app.open(this_doc_fullName, !open_hidden);
        // ...
    }

 

 

When I run my script, the alert shows object in step one and string in step two.
(Only) with the added lines after ›//Convert to File object ...‹ the document does also open in step two.

 

I agree with you that this_doc_fullName should be a string (that’s why i wrote ›… behaves like an object‹) . 
If you have any idea, whats going on there, I’d be interested to hear. 

 

Regards, Martin