Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

[CS3][JS] Uniquely identifying documents with common names - possible?

Enthusiast ,
Jun 24, 2008 Jun 24, 2008
Hi!

Is there no way to refer directly to a document in app.documents when there are several documents with the same name open at the same time (located in different folders)?

I have tried to assign unique labels to the documents when opening them, but have not succeeded in referring to documents by their labels, only by name - and the name not being unique, I get just one document reference (the first).

I also tried using the documents.itemByRange function, supposedly returning an array of all documents in the range defined by the parameters sent to the function. However, sending in the same name in the "from" and "to" parameters, returned only one document:

app.documents.itemByRange('test.indd', 'test.indd').getElements().length
Result: 1
//(I would have expected to get both of them)

// Referring directly by the name:
app.documents.item('test.indd').getElements().length
Result: 1
// Checking the name of the documents by their indexes:
app.documents.item(0).name
Result: test.indd
app.documents.item(1).name
Result: test.indd

Do you really have to loop through all open documents and check their fullName to be able to uniquely identify a document if the names are not unique?

Kind regards,
Andreas Jansson
TOPICS
Scripting
912
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 24, 2008 Jun 24, 2008
Try this:

app.documents.everyItem('test.indd').getElements()

Peter
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jun 24, 2008 Jun 24, 2008
Sorry, that doesn't seem to work either. I do get an array with 2 in length, but the fullName returns the same value for both documents.

app.documents.everyItem('test.indd').getElements()[0].fullName
Result: //..... Data/Indesign-test2/test.indd
app.documents.everyItem('test.indd').getElements()[1].fullName
Result: //..... Data/Indesign-test2/test.indd

(The two open documents are stored in "Indesign-test" and "Indesign-test2" respectively.)

Just to make sure I checked the fullName by document index, and it still returns the correct path for the other document:

app.documents[1].fullName
Result: //..... Data/Indesign-test/test.indd

Andreas
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jun 24, 2008 Jun 24, 2008
This is a limitation -- I'd call it a bug, but this was done deliberately and works as intended.

Having more than one document open with the same name is very difficult to deal with -- just about all the staples don't work. I posted an alert about this here:

http://www.niemannross.com/developer/wiki/index.php?title=InDesign_JS_Object_Model_Documents

The only way to work, I think, is to build an array of full names and use that to index into the documents. But every time you change the order of the documents, you'll need to re-build the index.

Dave
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jun 24, 2008 Jun 24, 2008
BTW, one justification for the InDesign team to take this position: only two people have run into this in all the time that scripting InDesign has been possible, and we're talking to each other in this topic.

Dave
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 24, 2008 Jun 24, 2008
Hi Dave,
Make that three or four... ;)

Kris at Rorohiko created a method to get unique references to two
different documents with the same name. It's usable if you have the free
APID ToolAssistant version 1.0.44 or higher. (it doesn't need to be
licensed...)

Here's an excerpt from the reference manual:

The get doc GUID function is used as follows:
guid =
app.callExtension(0x90B6C,10008,document);
This function returns a GUID (Globally Unique Identifier) for a
document. This is a string of the form
{nnnnnnnn-nnnn-nnnnnnnnnnnnnnnnnnnn}
where all 'n' stand for a hexadecimal digit.
These GUID allow you to work with same-name documents that
are concurrently open without getting tangled in the confusion
that occurs when using resolve calls.
As long as a document is not moved from its original location on
disk, it will keep the same GUID. Two documents with the same
name but different paths will have different GUID.
Opening, closing, (re)saving to the same location will NOT
change the GUID.
Moving a document to another location will change the GUID.
The find open doc by GUID function is used as follows:
doc = app.callExtension(0x90B6C,10009,guid);
This function retrieves a reference to an already open document
based on its GUID string.
These GUID strings are unique and persistent and allow
persistent references to same-name documents without resolvelike
issues (but the documents are meant to be 'unmovable' i.e.
they cannot be renamed or moved to another folder). Once the
document is open, it can be retrieved via its GUID.

HTH,

Harbs
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jun 24, 2008 Jun 24, 2008
On 24/6/08 9:44 PM, "Dave Saunders" <member@adobeforums.com> wrote:<br /><br />> only two people have run into this in all the time that scripting InDesign has<br />> been possible<br /><br />I suspect you mean only two have posted here about it. FWIW, you can get<br />around it in AppleScript using a whose clause, although even that method<br />sometimes has to be a bit indirect because of the use of file references for<br />full path and full name.<br /><br /><br />-- <br />Shane Stanley <sstanley@myriad-com.com.au>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jun 24, 2008 Jun 24, 2008
Aha! I've forced people to out themselves! Maybe we can start a movement!

Dave
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 24, 2008 Jun 24, 2008
> Maybe we can start a movement!
>
You lead, I'll follow.

I've found it's just easier to just ask Kris to create a solution to
work around any shortcomings in the scripting DOM, though! 🙂 (He's very
good at that...)

Harbs
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jun 25, 2008 Jun 25, 2008
Thanks for all information and ideas.<br /><br />I ended up with a small function to get an open document by its fullPath.<br /><br />function getDocumentByPath(fullPath){<br /> var myFile = new File(fullPath);<br /> var numberOfDocuments = app.documents.length;<br /> var documentIndex = undefined;<br /> <br /> if (numberOfDocuments == 1){<br /> // Only one document (always index 0) open.<br /> documentIndex = 0;<br /> }else if (numberOfDocuments != 0){<br /> // Loop through all open documents and get the index of the one with the path requested.<br /> var fileFullPath = myFile.absoluteURI;<br /> for (var i = 0; i<app.documents.length; i++){<br /> if (File(app.documents.fullName).absoluteURI == fileFullPath){<br /> documentIndex = i;<br /> break;<br /> }<br /> }<br /> }<br /><br /> return app.documents[documentIndex];<br />}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 26, 2008 Jun 26, 2008
LATEST
Very nice solution.

This will only work if you know the full path. The advantage of the
method added by APID, is that it will work even if you don't know the
file path. If you come up with a way to do that with scripting alone,
I'd love to hear it!

Harbs
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines