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

ScripUI window updates only after callback function is executed

Participant ,
Nov 24, 2023 Nov 24, 2023

Copy link to clipboard

Copied

I built a ScriptUI palette, that calls a function, which takes some time to finnish.

While the function is running, I want to show, whats going on. 

 

To archive this, I tried to change staticText in the palette, before running the function

Unfortunately, the updated text only is shown after the function has finished. Here’s my code:

var dialog = new Window("palette");

var st_processing = dialog.add("statictext", undefined, undefined, {name: "st_processing", truncate: "end"}); 
    st_processing.text = "";
    st_processing.preferredSize.width = 240; 

var ok = dialog.add("button", undefined, undefined, {name: "ok"}); 
    ok.text = "OK"; 
    ok.onClick = function(){
        st_processing.text = "Doing something …";
        function_one();
        // st_processing.text = "";
        // function_two();
        // dialog.close();
    };

dialog.show();

function function_one(){
    // do sth.
}

 

If I un-comment the last three lines in the onClick function, I don’t see anything. 

The way it is now, the function is run and the text is updated only after the function has finnished

 

I tried changing the size of staticText (similar to what Peter Karel is describing in »Fixing display problems in listbox controls« on p 40 in his ScriptUI tutorial).

        st_processing.text = "Doing something …";
        var s = st_processing.size;
        st_processing.size = [s[0] + 1, s[1] + 1];
        st_processing.size = s;

Unfortunately this doesn’t help.

 

I also tried this:

        st_processing.text = "Doing something …";
        st_processing.refresh();

This does actually show the message immediately.

But this way the function is never run. 

 

Is there a way, to show the updated text immediately?

 

Thanks for any suggestions, 

Martin 

TOPICS
Scripting

Views

340

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

Guide , Nov 24, 2023 Nov 24, 2023

Try with a different button.

The ok and close buttons are detected by name and handled differently.

 

Have you already used the function update() ? I don't know whether it still works.

Below a screenshot from the Javascript Tools Guide.

DirkBecker_0-1700837665426.png

 

Note also the third paragraph of show(), for your modal dialog experiments.

You'd enter some values in the dialog and perform your actual changes after show() returns.

 

Votes

Translate

Translate
Community Expert ,
Nov 24, 2023 Nov 24, 2023

Copy link to clipboard

Copied

If you want the prompt to appear before you click OK, you should make it part of the dialog, not of the callback.

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
Participant ,
Nov 24, 2023 Nov 24, 2023

Copy link to clipboard

Copied

Thanks for your feedback. Maybe I didn’t explain it well. I want this to happen:

 

  • When you open the dialog, the staticText shows nothing.
  • When you click OK, the staticText says something like »processing file 1«.
    The function could process all files in a book, so I would like to update the staticText for each file.
  • After processing is done, the window is closed. 

 

So adding the text to the dialog doesn’t do what I want. 

 

For now, I tried to change the text before the function is run (as shown in my example code). 

I also tried to change it from within "function_one()", but this didn’t change the behaviour.

- Martin 

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 ,
Nov 24, 2023 Nov 24, 2023

Copy link to clipboard

Copied

Aha, ok. Then make the prompt part of the called function, not of the callback:

 

ok.onClick = function(){
  function_one();
  // function_two();
  // dialog.close();
};

dialog.show();

function function_one(){
  st_processing.text = "Doing something …";
  // Do something
  st_processing.text = "";
}

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
Participant ,
Nov 24, 2023 Nov 24, 2023

Copy link to clipboard

Copied

HI @Peter Kahrel! I tried this before, without success.
Just to be sure, I made another attempt. Still doesn’t work. 

 

To see what’s going on, I added a bunch of alerts: 

 

ok.onClick = function(){
  alert("1 " + st_processing.text);
  function_one();
  alert("4 " + st_processing.text);
};

dialog.show();

function function_one(){
  st_processing.text = "Doing something …";
  alert("2 " + st_processing.text);
  // Do something
  st_processing.text = "";
  alert("3 " + st_processing.text);}

 

With the alerts it does exactly what one would expect:

  • alerts are shown in the correct order
  • they always display the expected text
  • text in the palette is updated properly

 

As soon as I remove the alerts, it’s going back to the way it behaved before. 

 

This is in noy way critical, so I think I will just leave it as it is (without any feedback on progress).

 

Thanks, Martin 

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 ,
Nov 24, 2023 Nov 24, 2023

Copy link to clipboard

Copied

Ok, one last shot then. It works ok for me (if I understand  it correctly). Open the Pages panel, then use the below amendments. I added sleeps so that you can actually see what happens. When I run this, I can see the prompts change when the functions are called.

 

ok.onClick = function(){
  function_one();
  function_two();
};


function function_one(){
  st_processing.text = "Doing something …";
  var d = app.documents.add();
  for (var i = 0; i < 10; i++) {
    d.pages.add();
    $.sleep(140);
  }
  st_processing.text = "";
}

function function_two(){
  st_processing.text = "Doing something else …";
  var p = app.documents[0].pages.everyItem().getElements();
  for (var i = p.length-1; i > 0; i--) {
    p[i].remove();
    $.sleep(140);
  }
  st_processing.text = "";
}

 

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
Participant ,
Nov 24, 2023 Nov 24, 2023

Copy link to clipboard

Copied

I’m pretty sure, when I tried, I did exactly what you do in function_one(), but It didn’t work at all, when I tried it.

 

With your script, it immediately shows »Doing something …«. 

But it never shows »Doing something else …«.

 

I did keep my dialog code, removed everything else and added your two functions. 

Besides that, I did restart InDesign, to make sure, there’s nothing in memory that could interfere. 

 

However, when I change your function_one() to app.documents[0], it doesn’t work either:

// var d = app.documents.add();
var d = app.documents[0];

 

It seems to me, like InDesign only updates if something changes in the UI (like an alert or a new doc). For my script this could actually do it:

For now, I’m only working on the active document. But the main goal of my script is, to open all documents of a book (seperately) and find some information. In a second step, I will open the documents again and modify said information. 

With what I found out with your help, I think it’s very likely, I get an visible update of my window for each opened document. 

 

I still don’t understand, why you see »Doing something else …«, and I don’t. 

 

Thank’s again, 

Martin 

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
Guide ,
Nov 24, 2023 Nov 24, 2023

Copy link to clipboard

Copied

Try with a different button.

The ok and close buttons are detected by name and handled differently.

 

Have you already used the function update() ? I don't know whether it still works.

Below a screenshot from the Javascript Tools Guide.

DirkBecker_0-1700837665426.png

 

Note also the third paragraph of show(), for your modal dialog experiments.

You'd enter some values in the dialog and perform your actual changes after show() returns.

 

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
Participant ,
Nov 24, 2023 Nov 24, 2023

Copy link to clipboard

Copied

LATEST

Thanks a lot!

window.update() did the trick.

It is so much nicer, if the panel can give some feedback.

Martin

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