[ExtendScript] Possible bug with Document.findObject()
- July 15, 2023
- 4 replies
- 1326 views
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:

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

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.

