Press Escape to stop the Script

Contributor ,
Feb 21, 2020 Feb 21, 2020

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

2.0K

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

correct answers 1 Correct answer

Adobe Community Professional , Mar 12, 2020 Mar 12, 2020

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 scrip
...

Likes

Translate

Translate
Contributor ,
Mar 10, 2020 Mar 10, 2020

Copy link to clipboard

Copied

Hi again,

Does anyone have any ideas on this?

Thanks, Bren

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
Guide ,
Mar 10, 2020 Mar 10, 2020

Copy link to clipboard

Copied

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.

 

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 ,
Mar 10, 2020 Mar 10, 2020

Copy link to clipboard

Copied

Canceling with the esc key may be possible only on Windows.
I think that it is possible on Mac and Windows if you can use the shift key.

while (ScriptUI.environment.keyboardState.shiftKey == false) {
    // do somethings
}

 

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
Contributor ,
Mar 12, 2020 Mar 12, 2020

Copy link to clipboard

Copied

Dirk, will have to look into the progress bar and see, thanks for the idea.

 

Hi Ajabon, I will look into seeing if there is a way around making it the escape key instead of the shift key but changing yours to:

while (ScriptUI.environment.keyboardState.shiftKey == true) { exit() } // If shift is pressed
worked to exit the script.

 

Thanks, Bren

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
Advocate ,
Mar 12, 2020 Mar 12, 2020

Copy link to clipboard

Copied

Hi JustOneBren,

 

Try the code below to have the escape key exit the script.

 

while (ScriptUI.environment.keyboardState.keyName == "esc"){ exit()}

 

Regards,

Mike

 

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 ,
Mar 12, 2020 Mar 12, 2020

Copy link to clipboard

Copied

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();
}

 

 

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 ,
Mar 12, 2020 Mar 12, 2020

Copy link to clipboard

Copied

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();
}

 

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
Contributor ,
Mar 13, 2020 Mar 13, 2020

Copy link to clipboard

Copied

Ajabon, That works perfectly and will work with the script I have just as I need it to 👍

Mike, couldn't get yours to work directly but it looks like it helped in changing Ajabons code to work with the 'escape' key.

 

Thank you both for your help, Bren

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
Enthusiast ,
Mar 30, 2020 Mar 30, 2020

Copy link to clipboard

Copied

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);
    }
//======================

 

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 ,
Mar 30, 2020 Mar 30, 2020

Copy link to clipboard

Copied

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++
    }
//======================

 

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
Enthusiast ,
Mar 30, 2020 Mar 30, 2020

Copy link to clipboard

Copied

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...

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 ,
Mar 30, 2020 Mar 30, 2020

Copy link to clipboard

Copied

Try to add myProgress.close(); at the end of script.

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
Enthusiast ,
Mar 30, 2020 Mar 30, 2020

Copy link to clipboard

Copied

LATEST

Yes, this works. But, the main question is how to "cancel/interrupt" the script.

The ESC key is not working... =(

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