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

Timing Issues with Scripts

Enthusiast ,
May 08, 2025 May 08, 2025

@frameexpert - Adding a mention since you don't necessarily follow the InDesign boards.

 

First off, I know this isn't only an Adobe issue. I've run into similar problems with Visual Basic (and similar solutions).

 

Anyway - My scripts tend to run through a lot of steps and function calls. I've noticed that sometimes some of the steps are "missed" - i.e., I'll run the script and the expected action does not happen. I'll run the script again and it works as expected. I'm "assuming" that the script moves on to the next action before the previous one completes and that causes the issue.

 

I've found often putting a 0.5-second pause before and after the action usually resolves the issue:

$.sleep(500);

but I wasn't sure if this was commonplace (I haven't seen it often in scripts others posted online), or if there was a better solution ...

 

Issues with this solution (applies to VBA also):

 

  • Since the script works (sometimes) without the sleep statements, it's hard to know if they are required. It's only a 0.5 second pause, but if I have to add 40 of them, I've added 20 seconds to a script that might have run fine with only 2 seconds added.

 

  • Similarly, since the script works (sometimes) without the sleep statements, it's hard to know when the pause time is sufficient - i.e. if the script failed 50% of the time with no delay statements, did the 0.5-second delay fix it, or did it just mean that in only fails 10% of the time, but not the time that I tested and it needs to be 1-second or 2-seconds. And if I set it to 5-seconds to be sure it waits long enough, I'm back to added extra unnecessary delay to the script.

 

Thanks in advance!

TOPICS
Scripting
860
Translate
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
LEGEND ,
May 08, 2025 May 08, 2025

What kind of operations are you talking about? 

 

I know at least one specific situation - but maybe it's not the same. 

 

Translate
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 ,
May 08, 2025 May 08, 2025

Hi @Marshall_Brooks a pause might help when you are dealing with an asynchronous call to some other process, but it is not a comfortable situation. Can you identify the "actions" that might be causing the subsequent missing operations? Sometimes there are more robust approaches that avoid them without hacks like $.sleep.

 

Putting a delay *before* an operation suggests that it is actually *after* a previous operation that has not completed. Otherwise there is no reason to sleep before an operation because nothing has happened yet.

 

Perhaps you could insert an assertion into the code after the "action" that checks if the action is finished (this could be a while loop with a short $.sleep(100) and a timeout. Just an idea.

- Mark

Translate
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
Enthusiast ,
May 12, 2025 May 12, 2025

Great suggestions. The script does multiple things. The specific action that was being skipped was a find and replace character formats. The action before that step was a save document (to a network drive). The action before that were other find and replace text statements. The script also sets and hides conditional formats, opens a template on the network drive and updates master pages, etc.

 

Also, there seem to be two types of intermittent errors:

  • Sometimes the script seems to run fine, but it just "skips over" a step like the find/replace above. Sleep statements seem to fix that. Not the most elegant way to handle it.
  • The script opens a pop-up window that stays open and allows you to click buttons to run other actions. Sometimes the scripts seems to freeze - i.e. I thought it was skipping on of the conditional format apply actions, but I tried to do something else and that did not work either. Closing the pop-up window and re-running the script, it worked again.

 

Translate
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
LEGEND ,
May 13, 2025 May 13, 2025

@Marshall_Brooks

 

I don't think F&C is asynchronous?

 

Unless, your scripts are UXP scripts? 

 

Translate
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 ,
May 13, 2025 May 13, 2025

Hi @Marshall_Brooks , Can you show an example your find and change code?

Translate
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
Enthusiast ,
May 13, 2025 May 13, 2025

Find and Change (there are a couple of them) is here: https://community.adobe.com/t5/framemaker-discussions/script-to-change-red-text-to-black/m-p/1530539...

 

I have a couple of them in the script (one is just text, but is similar).

 

I have most of the issues with the script worked out. I added about eight 0.5-second sleep statements. Crude and ugly, but the script seems to be working.

 

The issue with the window freezing seemed to happen when I had a revert to save step and closed and re-opened the document and the script lost the active document. I had to add code to close and re-open the pop-up window and redefine the "doc" variable, but that seemed to fix the issues.

Translate
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
LEGEND ,
May 13, 2025 May 13, 2025

@Marshall_Brooks

 

But that's for FRAMEMAKER?

 

Here - is InDesign forum 😉 

 

Translate
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
Enthusiast ,
May 13, 2025 May 13, 2025

Understood. On the FrameMaker forum, I was previously referred here for ScriptUI questions, which is basically what this is.

Translate
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
LEGEND ,
May 13, 2025 May 13, 2025
quote

Understood. On the FrameMaker forum, I was previously referred here for ScriptUI questions, which is basically what this is.


By @Marshall_Brooks

 

But those are completely different applications 🙁 so problems are most likely not related.

 

Translate
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 ,
May 13, 2025 May 13, 2025

Not sure I’m understanding your code, but it doesn’t look like you are using the built-in ExtendScript F&C properties and methods—is there a reason for that?

 

A simple text search example:

 

//the Find/Change result as an array
var res = getTextSearch("hello")

//loop the returned array
for (var i = 0; i < res.length; i++){
    $.writeln(res[i].contents)
};  

/**
* Gets results of a text search as an array 
* @ param text to search for 
* @ return result array 
*/
function getTextSearch(fp){
    app.findTextPreferences = app.changeTextPreferences = app.findChangeTextOptions = null;
    app.findChangeTextOptions.properties = {includeHiddenLayers:true, 
        includeLockedLayersForFind:true, 
        includeLockedStoriesForFind:true, 
        includeMasterPages:true} 
    app.findTextPreferences.findWhat = fp;
    return app.activeDocument.findText()
}

 

Translate
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
LEGEND ,
May 13, 2025 May 13, 2025

@rob day

 

The script was for FrameMaker 😉

 

Translate
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
Enthusiast ,
May 13, 2025 May 13, 2025

Correct, the script is for FrameMaker.

 

The original script was posted here: https://community.adobe.com/t5/framemaker-discussions/find-replace/td-p/3581679#4032650 by @Ian Proudfoot as a modification of @Russ Ward code.

 

I needed it to search for font changes and @frameexpert helped me with that in the previously linked thread.

 

There are minor issues with the script (it seems to wrap to the beginning even though the code implies it shouldn't, but otherwise, it seems to work well.

Translate
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
LEGEND ,
May 13, 2025 May 13, 2025
LATEST

@Marshall_Brooks

 

But it has nothing to do with InDesign...

 

Maybe in the FrameMaker there is a possibility of "skipping" F&C request - but not in the InDesign. 

 

Not in the ExtendScript nor in VBS - maybe in the UXP version of the script. 

 

Translate
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 ,
May 13, 2025 May 13, 2025

Hi @Marshall_Brooks ,

with very large InDesign documents I would work with exportFile() instead of asynchronousExportFile().

You could determine this e.g. by the number of pages that are involved in the export process.

 

Notes:

Perhaps it would also help if your script has only one document open at the same time going through a lot of documents in a folder or documents from a book file.

 

Regards,
Uwe Laubender
( Adobe Community Expert )

Translate
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