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

Press Escape to stop the Script

Guest
Feb 21, 2020 Feb 21, 2020

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
4.7K
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

correct answers 1 Correct answer

Community Expert , 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
...
Translate
Guest
Mar 10, 2020 Mar 10, 2020

Hi again,

Does anyone have any ideas on this?

Thanks, Bren

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

 

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

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
}

 

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

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

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

Hi JustOneBren,

 

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

 

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

 

Regards,

Mike

 

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

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

 

 

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 ,
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 script.\t";
    tf.recompose();
}

 

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

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

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 ,
Mar 30, 2020 Mar 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);
    }
//======================

 

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 ,
Mar 30, 2020 Mar 30, 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++
    }
//======================

 

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

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

Try to add myProgress.close(); at the end of 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
Enthusiast ,
Mar 30, 2020 Mar 30, 2020
LATEST

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

The ESC key is not working... =(

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