Skip to main content
February 21, 2020
Answered

Press Escape to stop the Script

  • February 21, 2020
  • 2 replies
  • 4864 views

Hi everyone,

I have a script that loops through multiple documents doing it's thing but sometimes I want to stop it, but I can't without force quitting inDesign.

Is there a piece of javascript I can insert into a few places in the script that looks for the escape button being pressed, and, if it is it stops the script at that point.

Thanks, Bren

This topic has been closed for replies.
Correct answer ajabon grinsmith

JustOneBren, 

It is wrong. Implement the code you want to do in the while block.
Try to understand it because it shows more concrete code.

 

var n = 0;
var doc = app.documents.add();
var tf = doc.textFrames.add();
tf.geometricBounds = doc.pages[0].bounds;
tf.parentStory.pointSize = 3;

//Continue processing the contents of the while block until you press the shift key
while (ScriptUI.environment.keyboardState.shiftKey == false) {
    tf.contents += "press shift key to stop the script.\t";
    tf.recompose();
}

 

 


I was also wrong, and my Mac was able to abort with the esc key. The final form of the code.

At least ' keyCode == "esc" ' didn't work in my environment.

var n = 0;
var doc = app.documents.add();
var tf = doc.textFrames.add();
tf.geometricBounds = doc.pages[0].bounds;
tf.parentStory.pointSize = "6pt";

//Continue processing the contents of the while block until you press the esc key
while (ScriptUI.environment.keyboardState.keyName != "Escape") {
    tf.contents += "press esc key to stop the script.\t";
    tf.recompose();
}

 

2 replies

lfcorullon13651490
Legend
March 30, 2020

Hello, guys. Any idea why this is not working on my progressbar?

 

//======================
var found = new Array (150);
var myProgress = new Window ("palette" , "Script by LFCorullón" , undefined , {closeButton: false});
myProgress.pbar = myProgress.add("progressbar", undefined, 0, found.length);
myProgress.pbar.preferredSize.width = 300;
myProgress.str = myProgress.add("statictext" , [0,0,300,24]);
myProgress.str.text = "Start processing...";

var btn = myProgress.add("group");
btn.alignment = "center";
btn.add("statictext" , undefined , "Press ESC to cancel");

myProgress.show();

//======================
for (var i = 0; i < found.length; i++){
    myProgress.pbar.value = i+1;
    myProgress.str.text = "Processing " + (i+1);
    $.sleep(20);
    }
//======================

 

Ten A
Community Expert
Community Expert
March 31, 2020

You need to watch ESC keys states like below.

//======================
var i = 0
while (ScriptUI.environment.keyboardState.keyName!="Escape"){
    if (i>found.length) break;
    myProgress.pbar.value = i+1;
    myProgress.str.text = "Processing " + (i+1);
    $.sleep(20);
    i++
    }
//======================

 

lfcorullon13651490
Legend
March 31, 2020

Thank you. But doesn't work.

Changing the for loop to this code you sent, the dialog counts to 151 and doesn't close in the end. So, I changed the if statement to 'i > found.length -1 and then the count goes to 150. But the dialog still doesn't close in the end.

The ESC key press does nothing in any case...

March 10, 2020

Hi again,

Does anyone have any ideas on this?

Thanks, Bren

Legend
March 10, 2020

You can check the existence of some file or folder and stop on change …

There are also scripts around that display a progress bar. I don't know whether they have provisions to interrupt.