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

[ExtendScript] Possible bug with Document.findObject()

Community Expert ,
Jul 14, 2023 Jul 14, 2023

Copy link to clipboard

Copied

Hi scripters, I ran into a possible bug with Document.findObject() where the document reference (ie. the `doc` in doc.findObject) seems to be ignored, and the activeDocument used instead. Here is a script that shows the issue. I'm seeing it in Indesign 18.4 on MacOS 13.4.1.

/**
 * Testing for possible Document.findObject() bug.
 */
function main() {

    var docs = app.documents;

    /* doesn't matter which way this loops */
    // for (var d = docs.length - 1; d >= 0; d--) {
    for (var d = 0; d < docs.length; d++) {

        /* doesn't matter if the document reference is resolved */
        // var doc = docs[d];
        var doc = resolve(docs[d].toSpecifier());

        app.findChangeObjectOptions.objectType = ObjectTypes.ALL_FRAMES_TYPE;
        app.findObjectPreferences = NothingEnum.NOTHING;
        app.findObjectPreferences.fillTint = -1;

        /*
          uncomment this line to see that
          the find occurs in the active document
          regardless of `doc` reference
        */
        // app.activeDocument = doc;

        var found = doc.findObject();

        $.writeln('\nFound ' + found.length + ' objects in "' + doc.name + '" (' + doc.toSpecifier() + ')');

        /* doesn't matter which way this loops */
        // for (var i = found.length - 1; i >= 0; i--) {
        for (var i = 0; i < found.length; i++) {

            $.writeln('specifier: ' + found[i].toSpecifier());
            $.writeln('label: ' + found[i].label);

        }

    }

};

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Test for findObject Bug');

 

To follow my test, open the two attached demo documents, then run the script and look at the console.

 

Here is what I see when I have demo1.indd active:

Screenshot 2023-07-15 at 10.35.50.png

 

Here is what I see when I have demo2.indd active:

Screenshot 2023-07-15 at 10.45.36.png

 

The problem seems to be that doc.findObject() is performed on the activeDocument, not `doc`. You can see that the specifier for the found object always points to the active document, not `doc`.

 

Of course the result I expect is to iterate over all three frames—one from demo1 and two from demo2.

 

Am I missing anything here? Can anyone reproduce this? I couldn't find a bug report on uservoice, so if no-one corrects me here I'll lodge one.

- Mark

 

P.S. I'm assuming that UXP scripting will inherit problems like this from the DOM so it's still worth fixing these. Let me know if that's wrong, because if so, let's not waste much time.

TOPICS
Bug , Scripting

Views

678

Translate

Translate

Report

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

correct answers 1 Correct answer

Community Expert , Jul 15, 2023 Jul 15, 2023

Update: it seems that this is a bug, and is reproducable. I have lodged a bug report. Please vote.

- Mark

Votes

Translate

Translate
Community Expert ,
Jul 15, 2023 Jul 15, 2023

Copy link to clipboard

Copied

Hi Mark,

I have reproduced this bug while I was testing for another discussion linked below

https://community.adobe.com/t5/indesign-discussions/make-script-work-across-multiple-docs/m-p/139357...

You seem to be right, the findObject method is indeed targetting the activeDocument irrespective of the document object used to call it.

We should file a bug. I am not sure if UXP borrows all the implementation from ES, maybe someone following UXP more closely could pitch in. @Peter Kahrel @Laubender @rob day can you guys give this a try?

My config is InDesign 18.4, MAC Os 12.6.7. If someone can test it on Win as well that would be great.

-Manan

Votes

Translate

Translate

Report

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 ,
Jul 15, 2023 Jul 15, 2023

Copy link to clipboard

Copied

Hi Mark and Manan,

will test this later in the day.

Hm. This issue reminds on a now fixed bug in ExtendScript with packaging InDesign documents we had a few years back.

 

Regards,
Uwe Laubender
( Adobe Community Expert )

Votes

Translate

Translate

Report

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 ,
Jul 15, 2023 Jul 15, 2023

Copy link to clipboard

Copied

Hi Mark,  I’m seeing the same behavior with CC2021. I’m not sure it‘s a bug? In the findChangeObjectOptions there’s no property for searching all documents, so I think that’s why activating the document in your documents loop works:

 

Screen Shot.png

 

I don’t think you need to loop through the documents—you could set found to app.findObject(), which would return the three objects. Try this:

 

 

/**
 * Testing for possible Document.findObject() bug.
 */
function main() {
    var s = "Number of items found in open documents "; 
    app.findObjectPreferences = app.changeObjectPreferences = app.findChangeObjectOptions = null;
    app.findChangeObjectOptions.objectType = ObjectTypes.ALL_FRAMES_TYPE;
    app.findObjectPreferences.fillTint = -1;
    
    var found = app.findObject();
    s += found.length + "\r";
    
    for (var i = 0; i < found.length; i++) {
        s+= "Item " + (i+1) + ": " + found[i] + " in document named: " + found[i].parent.parent.name + "\r"
    }
    $.writeln(s)
    /* 
    Returns:
    Number of items found in open documents 3
    Item 1: [object Rectangle] in document named: demo1.indd
    Item 2: [object Rectangle] in document named: demo2.indd
    Item 3: [object Rectangle] in document named: demo2.indd 
    */

}; 

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Test for findObject Bug');

 

 

 

 

Votes

Translate

Translate

Report

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 ,
Jul 15, 2023 Jul 15, 2023

Copy link to clipboard

Copied

Hi Rob,

I do think this behavior is a bug.

 

As a workaround:

When looping all documents, and you have to do a loop for whatever reason for filtering certain ones for example, it should work to make every visited document the active one and then proceed with findObject() . Though, I did not test this yet.

 

Regards,
Uwe Laubender
( Adobe Community Expert )

Votes

Translate

Translate

Report

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 ,
Jul 15, 2023 Jul 15, 2023

Copy link to clipboard

Copied

it should work to make every visited document the active one and then proceed with findObject()

 

Right, if you uncomment Mark’s app.activeDocument=doc line the document gets activated and it works as expected.

 

Find Object would always return page items, so I think with no document loop you could safely use .parentPage.parent.parent to get the found item’s document?

Votes

Translate

Translate

Report

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 ,
Jul 16, 2023 Jul 16, 2023

Copy link to clipboard

Copied

Hi Uwe,

Yes it seems to work when we set the activedocument with the doc object of the loop iteration. So as a workaround this is perfect.

-Manan

Votes

Translate

Translate

Report

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 ,
Jul 15, 2023 Jul 15, 2023

Copy link to clipboard

Copied

Hi @rob day, yes I think that's why I've never come across this bug previously—because I would normally use app.findObject(). However the issue arose because of this post where Rogerio needed to do the findObject based on each document's swatches. So the findObjectPreferences.fillColor param was different for each document and therefore the findObject() call had to be done on the document, not the app.

- Mark

Votes

Translate

Translate

Report

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 ,
Jul 15, 2023 Jul 15, 2023

Copy link to clipboard

Copied

Update: it seems that this is a bug, and is reproducable. I have lodged a bug report. Please vote.

- Mark

Votes

Translate

Translate

Report

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 ,
Jul 15, 2023 Jul 15, 2023

Copy link to clipboard

Copied

Voted.

-Manan

Votes

Translate

Translate

Report

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 ,
Jul 16, 2023 Jul 16, 2023

Copy link to clipboard

Copied

Likewise.

 

P.

Votes

Translate

Translate

Report

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 ,
Jul 16, 2023 Jul 16, 2023

Copy link to clipboard

Copied

Voted!

 

Regards,
Uwe Laubender
( Adobe Community Expert )

Votes

Translate

Translate

Report

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 ,
Jul 16, 2023 Jul 16, 2023

Copy link to clipboard

Copied

I wonder if it can be fixed without changing the UI? With a UI search there isn’t an option to search a single document that isn’t the active doc—the only choice in the dropdown is the active document or all of the documents.

Votes

Translate

Translate

Report

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 ,
Jul 16, 2023 Jul 16, 2023

Copy link to clipboard

Copied

HI @rob day,

Aren't these the same options for other search options like Grep? However, there we are able to search based on the document object calling the findGrep method. 

Screenshot 2023-07-17 at 8.36.50 AM.png

-Manan

Votes

Translate

Translate

Report

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 ,
Jul 17, 2023 Jul 17, 2023

Copy link to clipboard

Copied

LATEST

Hi @rob day, I think I see what you are wondering, but I don't think we need to bring the UI into it. The Document.findText and Document.findGrep functions work as expected with multiple documents, but the Document.findObject function does not. I think it is as simple as that.

- Mark

Votes

Translate

Translate

Report

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