Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


ID2021 scripting: Idle task falls silent

Explorer ,
Apr 19, 2021 Apr 19, 2021

Copy link to clipboard

Copied

Hi all!

I am trying to implement IdleTask in a startup script to automatically run short scripts after checking some DOM properties.
This decision was made because events on page items do not work.


The still empty ON_IDLE event handler function fires several times and falls silent after a while ... (event handler contains only an alert ).

 

I've tried changing the restart time between .5 --- 10 seconds, and the lower the number was set, the longer the script stayed working. If the sleep time is set to 10 seconds, the event appears 3---5 times.

 

As a result, IdleTask freezes after about 2---3 minutes, even if the user is not working. IdleTask remains valid, but there are no more ON_IDLE events.

 

I did not launch any other IdleTask or scripts, InDesign remains fully functional.

 

I would like the event handler script to be restarted without interruption (except for the user work time).

Why is this happening and is it possible to somehow customize it?

 

Another question is: If several IdleTasks are running, will they be executed sequentially or concurrently?

TOPICS
Scripting

Views

243

Likes

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
Adobe Community Professional ,
Apr 19, 2021 Apr 19, 2021

Copy link to clipboard

Copied

Can you post the code that isn’t working?

 

This example creates a new task named iTask and sends an alert when a page is added:

 

var doc = app.activeDocument;
var pc = doc.pages.length;
//make a new idleTask. The task name and the sleep time
var iTask = makeIdleTask("onAddingPage", 100)
iTask.addEventListener(IdleEvent.ON_IDLE, onAddPage);


/**
* The idle task event function
* @Param the event 
* @Return void 
*/

function onAddPage(e){
    if ( doc.pages.length>pc ) {
        alert("Page Added, do something")
    }
    pc = doc.pages.length;
}


/**
* Makes a new named idleTask 
* @Param the task name 
* @Param the sleep time 
* @Return the named idleTask
*/
function makeIdleTask(n, s){
    var it;
    try {
        app.idleTasks.add({name:n, sleep:s})
    }catch(e) {
        it = app.idleTasks.item (n);
    } 
    return app.idleTasks.item (n);
}

 

Likes

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
Adobe Community Professional ,
Apr 19, 2021 Apr 19, 2021

Copy link to clipboard

Copied

Hi Rob,

does the script still work if you leave your InDesign alone for some minutes?

 

Regards,
Uwe Laubender

( ACP )

Likes

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
Adobe Community Professional ,
Apr 19, 2021 Apr 19, 2021

Copy link to clipboard

Copied

Seems to be—I waited 20 min.

Likes

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
Adobe Community Professional ,
Apr 19, 2021 Apr 19, 2021

Copy link to clipboard

Copied

The 100 millisec sleep time seemed delay when I first tried after 30 min. The alert took a few seconds to show when I added the first page, but then it happened normally after adding another page.

Likes

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
Adobe Community Professional ,
Apr 19, 2021 Apr 19, 2021

Copy link to clipboard

Copied

Hi vhh68,

I've seen this issue as well. Though it needs some minutes that the problem shows up. I have no good solution to this.

Hm. Maybe a workaround is to not allow the user and InDesign to be idle so long?

For the second question: Sorry, I have no idea.

 

Regards,
Uwe Laubender

( ACP )

Likes

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
Explorer ,
Apr 23, 2021 Apr 23, 2021

Copy link to clipboard

Copied

Hello rob day, hello Uwe Laubender!

Here is just part of the code responsible for working with idleTask:

 

app.addEventListener('afterOpen', function(event){
        if (event.parent instanceof LayoutWindow) {
            
            var docName = event.parent.parent.name,
                docTask = app.idleTasks.itemByName(docName);
            
            docTask && docTask.isValid && docTask.remove();
            
            docTask = app.idleTasks.add({name: docName, sleep: 1000});
            docTask.addEventListener('onIdle', function(event){
                    event.parent.sleep = 5000; 
                    alert('DOCTASK');
                    //event.parent.sleep = 1000;
            });

        }
    });

 

The script worked only 2-4 times in a row, then events 'onIdle' do not appear. I tried to run this code as a simple script and startup script. The result is absolutely the same...

(Win 10, RAM 32Gb)

 

For each document open, I want to start the task that tracks changes in the document and starts the desired scripts depending on the changes in the document.

At the input, I increase sleep time so that the script can be executed without overlapping, I restore sleep time at the output, since it is possible that only a short-term check will be executed in the next iteration.

 

Likes

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
Adobe Community Professional ,
Apr 24, 2021 Apr 24, 2021

Copy link to clipboard

Copied

LATEST

It looks like the alerts interfere with themselves, try a writeIn. You are adding the idleTask to the application, so when the task is triggered the docName might not be the active doc, and the tasks keep running after the documents are closed. Here I’m writing the number of application tasks and the triggered task’s name

 

#targetengine "session"
app.addEventListener('afterOpen', function(event){
        if (event.parent instanceof LayoutWindow) {
            
            var docName = event.parent.parent.name.replace(/\.[^\.]+$/, ''),
            docTask = app.idleTasks.itemByName(docName);
            
            docTask && docTask.isValid && docTask.remove();
            
            docTask = app.idleTasks.add({name: docName, sleep: 5000});
            docTask.addEventListener('onIdle', function(event){
                    $.writeln("There are currently " + app.idleTasks.length + " application idle tasks. Triggered task’s name is " + docTask.name);     
            });
        }
    });

 

 

Screen Shot 19.png

 

Likes

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