InDesign findWhat not working, if called from an ScriptUI dialog
Short version:
I’ve written a function, that performs a Grep search.
When I run the function from the main script, it works fine.
When I run the function from an ScriptUI dialog, it fails silently, when it reaches the grep search.
Does anyone have any idea, why this is happening? – And how to make it work?
Full story:
I’m using Indesign CC 2022 on macOS 12.6 (Monterey).
The search function looks for anchored frames.
I put it in a separate file and load it as an include: //@include "doc_name.jsx"
Found items are returned. The search part looks something like this:
// my_functions.jsx
function find_items(param){
app.findChangeGrepOptions.includeFootnotes = true;
// ...
app.findGrepPreferences.findWhat = "~a"; // ~a is the anchor mark
var results = app.documents[0].this_doc.findGrep();
// ... filter results
return results_filtered;
}
When I call the function from the main script, it works as expected:
// main.jsx
//@include "my_functions.jsx"
var res = find_items(param);
alert(res);My results are displayed as expected.
But I want to run the function with some user defined parameters from an ScriptUI dialog.
When I run the function from a dialoge, it stops right at the grep search (after clicking the OK-button, of course).
The strange thing is, that it doesn’t display any error message.
// main.jsx
//@include "my_functions.jsx"
var dialog = new Window("dialog");
// ...
var ok = dialog.add("button", undefined, undefined, {name: "ok"});
ok.text = "OK";
ok.onClick = function(){
ok_clicked(param);
};
dialog.show();
function ok_clicked(param){
var res = find_items();
dialog.close();
alert(res);
}
The script will stop when it reaches this line:
app.findChangeGrepOptions.includeFootnotes = true;
Strangely, when I put the options in properties, it doesn’t stop:
app.findChangeGrepOptions.properties = {
includeFootnotes: true,
// ... other properties
};
Unfortunately, this doesn’t solve my problem. In this case the script stops at findWhat:
app.findGrepPreferences.findWhat = "~a";
If I run the dialog first and my function afterwards (independent of the dialog), everything works.
---
So for now, I will try to write the parameters from the dialog into a global variable.
I can then run my function with the values from this variable. I think this will work, but it’s super ugly.
I would prefer, to keep the dialog open until my function is done.
The function will open multiple documents. With the dialog open, I could show the name of the active document.
Also, it would be clear to the user, that the script isn’t finished, as long as the dialog is visible.
If anyone has an idea for a cleaner solution, I’d be grateful.
Regards, Martin
