Skip to main content
Inspiring
November 23, 2023
Answered

InDesign findWhat not working, if called from an ScriptUI dialog

  • November 23, 2023
  • 2 replies
  • 674 views

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 

This topic has been closed for replies.
Correct answer Peter Kahrel

Instead of 

new Window("dialog")

use

new Window("palette")

 

2 replies

m1b
Community Expert
November 23, 2023

Hi @C330 S, when you execute the function in the first case (that works) you do

var res = find_items(param);

with `param`. But when you execute it in the UI code:

var res = find_items();

you don't give it `param`.

 

Maybe that is the problem? Also be sure that the the `param` variable is accessible from your ui code.

- Mark

 

C330 SAuthor
Inspiring
November 24, 2023

Hi @m1b, thanks for your suggestion.
Unfortunately the thing, you spotted is more of a typo in the code in this ticket.
When I tried to break the code down to the minimum necessary to show my problem, I just missed that attribute.
- Martin

m1b
Community Expert
November 24, 2023

Ah I see. No problem.

 

By the way, if you do need a modal dialog, you can do it, but you need to close the window ( dialog.close(1) ) before you do your find_items().

 

This means a few things:

1. that the call to find_items() cannot be inside your button event handler ( ok_clicked ).

2. a good place to update your script's settings (eg. your param variable which could be in an Object shared by the script and the UI) is in your ok_clicked handler before doing dialog.close(1). (By the way, 1 means success. 2 means user cancelled dialog.)

3. so following this approach, instead of dialog.show(), you can do:

var result = dialog.show();

if (result == 2)
    return; // user cancelled dialog

var res = find_items(param);
alert(res);​

 

My usual approach is to create a settings object with all the properties I want (even if they are undefined at the start) and send them to the UI function, eg.  ui(settings) which shows the dialog, and the ok button handler function inside the ui function updates the settings object. That way the script can easily be run with or without the UI showing—you can just comment out the ui(settings) line.

- Mark

Peter KahrelCorrect answer
Community Expert
November 23, 2023

Instead of 

new Window("dialog")

use

new Window("palette")

 

C330 SAuthor
Inspiring
November 24, 2023

Thank you very much. That did it.

Now, that I have the solution, I think, I have already read about this different behaviour of "dialog" and "palette".

A modal window ("dialog") would be perfect for what I wanted to do, so I didn’t even think about using "palette". 

But in the end "palette" does it as well.

– Martin