0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

JXA(JavaScript で書いた AppleScript)で window.confirm(); を使いたい

Posted at
const CONFIRM_RESULT_YES = 0;
const CONFIRM_RESULT_NO = 1;
const CONFIRM_RESULT_CANCEL = -1;

/**
 * JavaScript の window.confirm() のように振る舞う。
 * @param {string} msg ユーザに表示するメッセージ内容。'\n' で改行できます。
 * @param {string} yesButton Yes の意味となるボタン名称(既定は "Yes")。
 * @param {string} defaultBtn No の意味となるボタン名称(既定はなし)。
 * @param {string} noButton デフォルトにしたいボタン名(既定は Yes ボタン)。
 * @param {string} cancelButton キャンセルボタンにしたいボタン名(既定はなし)。
 * @returns 0: Yes、1: No、-1: Cancel
 */
function confirm(msg,
    yesButton = "Yes",
    noButton = null,
    cancelButton = null,
    defaultBtn = null) {

    let app = Application.currentApplication();
    app.includeStandardAdditions = true;

    let params = {};
    let buttons = [yesButton];

    if (defaultBtn === null) {
        defaultBtn = yesButton;
    }

    if (noButton !== null && noButton !== "") {
        buttons.push(noButton);
    }

    if (cancelButton !== null && cancelButton !== "") {
        if (!buttons.includes(cancelButton)) {
            buttons.push(cancelButton);
        }
        params.cancelButton = cancelButton;
    }

    params.buttons = buttons;

    if (buttons.includes(defaultBtn)) {
        params.defaultButton = defaultBtn;
    }

    let result = {};
    try {
        result = app.displayAlert(msg, params);
    } catch (e) {
        if (e.errorNumber === -128) {
            return CONFIRM_RESULT_CANCEL;
        }
        throw e;
    }

    if (result.buttonReturned === yesButton) {
        return CONFIRM_RESULT_YES;
    }
    else {
        return CONFIRM_RESULT_NO;
    }
}

confirm("続行しますか?", "Yes", "No", "No");
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?