Skip to main content
zeRafio
Inspiring
August 21, 2023
Answered

Change button shortcut

  • August 21, 2023
  • 2 replies
  • 706 views

Is there any way to change the shortcut attached to the "Don't save" button in the dialog displayed after you select the "File>Close" menu item?

----

MacOS 12.6.7 (21G651) French localization InDesign 18.4.0.56 (18400)

This topic has been closed for replies.
Correct answer Dirk Becker

The event handler/listener (your doClose) should have an argument.

That argument will be an event object.

As per https://www.indesignjs.de/extendscriptAPI/indesign-latest/#MenuAction.html, the BEFORE_INVOKE is cancelable, if that works as documented, use the event's .preventDefault() method.

 

2 replies

Peter Kahrel
Community Expert
Community Expert
August 22, 2023

Aha, I see.

 

You can do a shortcut to the Annuler button by adding & before the letter you want to use, e.g. "&Annuler". Then immediately after defining b2, set the shortcut:

 

var b2 = g.add("button", . . .
b2.shortcutKey = "A";

 

Now when the dialog appears and you hold down the Alt key, the shortcut is underlined. Press Alt+A to "press" the button. (I seem to remember that shortcut key don't work or don't work well on Macs, not sure.)

 

As to the problem of the original window appearing after you dismiss your own, maybe you can prevent that by removing the original Close item, the inserting your own, then adding your event listener to your own menu item.

zeRafio
zeRafioAuthor
Inspiring
August 22, 2023

Hi Peter,

Adding a "&" in a menu title does not work on Mac.

And for the original window display, following Dirk's advise solved the problem.

Anyway,  I want to thank you fort the help I could find on your site.

Peter Kahrel
Community Expert
Community Expert
August 22, 2023

You can do that with a script and create a shortcut to the script. Here's the script, it's just one line (not counting the description):

//DESCRIPTION: Close the active document without saving it
app.activeDocument.close (SaveOptions.NO);

Place the script in InDesign's Scripts folder, then create a shortcut to it in the Keyboard Shortcuts window.

zeRafio
zeRafioAuthor
Inspiring
August 22, 2023

Thanks Peter, 

I know how to close a document by script. My problem is not there.

I'm a disabled person and some key combinations are getting difficult for me to perform.

I've tried to rebuild the dialog using ScriptUI (thanks to your PDF!) but I encounter some difficulties.

With the following script, the dialog behaves normally except when you hit 'Escape' or click on the 'Cancel' button: the "native" dialog appears after the scriptUI one is dismissed.

----

MacOS 12.6.7 (21G651) French localization

InDesign 18.4.0.56 (18400)

----

 

#targetengine "closeDocument";
 
var myAction = app.menuActions.itemByID(263);
// alert (myAction.eventListeners.length);
if (myAction.eventListeners.length > 0) {
    for (var myCounter = myAction.eventListeners.length; myCounter > 0 ; myCounter--) {
        myAction.eventListeners.item(0).remove();
        // myAction.removeEventListener("beforeInvoke", doClose)
    }
}

myAction.eventListeners.add("beforeInvoke", doClose)

function doClose() {
    // declare global variables
    var myReturnCode = 0;
    var myDoc = app.documents[0]

    // close immediatly the active doc if it's not modified
    if (myDoc.modified == false) {
        myDoc.close(SaveOptions.no); exit;
    }
    else {
        // get document name
        var myName = myDoc.name;

        // build the message string
        var myMess = "Enregistrer les modifications apportées au document Adobe InDesign \"" + myName + "\" avant de fermer ?"

        // declare the indesign icon 42x32
        var myIcon = undefined
        
        // create the dialog
        w = new Window('dialog', "Adobe InDesign", undefined); // default orientation is 'column'

        // create first dialog row for icon and message
        var g = w.add('group'); // default orientation is 'row'
        g.alignment = ['left', 'top'];

        // add the icon
        var i = g.add('image', undefined, myIcon);
        i.alignment = ['left', 'top'];
        i.size = [42, 32];

        // add the message
        var m = g.add('statictext', undefined, myMess, { multiline: true });
        m.alignment = ['left', 'top'];
        var labelSize = (m.graphics.measureString(myMess, undefined, 410));
        if (labelSize.height < 30) { var labelSize = (m.graphics.measureString(myMess, undefined, undefined)); };
        m.preferredSize = labelSize;

        // make a second row to display buttons at bottom of the dialog from right to left
        var g = w.add('group'); // default orientation is 'row'
        g.alignment = ['right', 'top'];

        // add an invisible active button before all other to catch keyboard shortcut
        var z = g.add('iconbutton', [0, 0, 0, 0], undefined);
        z.active = true;

        // add the 3rd button
        var b3 = g.add('button', undefined, "Ne pas enregistrer", { name: 'third' });
        b3.onClick = function () { myReturnCode = 2; w.close(); $.sleep(200) };

        // add the cancel button
        var b2 = g.add("button", undefined, "Annuler", { name: "cancel" });
        b2.onClick = function () { myReturnCode = -1; w.close(); $.sleep(200) };

        // attach keyboard shortcut to the window for 3rd button
        w.addEventListener('keydown', function (myEvent) {
            if (myEvent.metaKey && myEvent.keyName === 'D') { myReturnCode = 2; w.close(); $.sleep(200) }
        });

        // add default button
        var b1 = g.add('button', undefined, 'Enregistrer', { name: 'ok' });
        b1.onClick = function () { myReturnCode = 1; w.close(); $.sleep(200) };

        // get indesign layout window dimensions
        var wY = app.windows.item(0).bounds[0];
        var wX = app.windows.item(0).bounds[1];
        var wB = app.windows.item(0).bounds[2];
        var wR = app.windows.item(0).bounds[3];

        // calculate dialog position centered on indesign window
        w.onShow = function () {
            var winW = w.size[0];
            var winH = w.size[1];
            var screenH = $.screens[1].bottom;
            w.frameLocation = [wX + ((wR - wX) / 2) - (winW / 2), screenH - ((wY + ((wB - wY) / 2) + (winH / 2)) - 22)];
            // w.update();
        };

        // display the dialog
        w.show();

        // execute the user choice
        if (myReturnCode === 1) { myDoc.close(SaveOptions.yes); }
        if (myReturnCode === 2) { myDoc.close(SaveOptions.no); }
        if (myReturnCode === -1) { exit(); }
        myReturnCode;
    }
}

 

Dirk BeckerCorrect answer
Legend
August 22, 2023

The event handler/listener (your doClose) should have an argument.

That argument will be an event object.

As per https://www.indesignjs.de/extendscriptAPI/indesign-latest/#MenuAction.html, the BEFORE_INVOKE is cancelable, if that works as documented, use the event's .preventDefault() method.