如果取消警报,如何使 javascript 部分重复?

How to make a javascript section repeat if an alert is canceled?

抱歉,如果这是一个愚蠢的问题,我真的是编码新手。我有一个脚本,我目前正在 运行 通过 Photoshop 在 Mac 上对文件夹中的图像进行排序并将它们保存到其他几个文件夹之一。它工作正常,但我每次都必须手动输入文件路径并重新保存脚本,这很麻烦。

相反,我想在开始时提示输入文件夹,然后提供确认选择或重新开始的选项。文件夹提示正在工作,但我不知道如果用户按下取消按钮如何使脚本重复。

我试过在网上搜索答案,但因为我真的不知道我在找什么,所以我不会走得太远。我最好的猜测是,也许我需要使用一个针对按钮的 if/else 语句,或者一个重复的循环,直到按下确认按钮。

这是我目前的情况:

var app = Application.currentApplication()
app.includeStandardAdditions = true


var sourceFolder = app.chooseFolder({
    withPrompt: "Please select a source folder:"
})

var pathFolder = app.chooseFolder({
    withPrompt: "Please select an output folder for images with correctly named clipping paths:"
})

var unnamedFolder = app.chooseFolder({
    withPrompt: "Please select an output folder for images with unnamed clipping paths:"
})

var noneFolder = app.chooseFolder({
    withPrompt: "Please select an output folder for images with no clipping paths:"
})


var alertText = "Confirm folder locations?"
var alertMessage = "Originals folder: \n" + sourceFolder + "\n\n" + 
    "Path 1 folder: \n" + pathFolder + "\n\n" + 
    "Unnamed Paths folder: \n" + unnamedFolder + "\n\n" + 
    "No Paths folder: \n" + noneFolder
app.displayAlert(alertText, {
    message: alertMessage,
    buttons: ["Change", "Confirm"],
    defaultButton: "Confirm",
    cancelButton: "Change"
})

如果按下 "Confirm" 按钮,我希望脚本继续到下一部分,它将开始将图像分类到所选文件夹中。如果按下 "Change" ,我希望这段代码重新开始。我怎样才能做到这一点?

我想我可能已经弄明白了(感谢 Medet Tleukabiluly 提到函数,它给了我一个新的探索方向):

var app = Application.currentApplication()
app.includeStandardAdditions = true

var sourceFolder
var pathFolder
var unnamedFolder
var noneFolder

function selectFolders() {
    sourceFolder = app.chooseFolder({
        withPrompt: "Please select a source folder:"
    })
    pathFolder = app.chooseFolder({
        withPrompt: "Please select an output folder for images with correctly named clipping paths:"
    })
    unnamedFolder = app.chooseFolder({
        withPrompt: "Please select an output folder for images with unnamed clipping paths:"
    })
    noneFolder = app.chooseFolder({
        withPrompt: "Please select an output folder for images with no clipping paths:"
    })

    var alertText = "Confirm folder locations?"
    var alertMessage = "Originals folder: \n" + sourceFolder + "\n\n" + 
        "Path 1 folder: \n" + pathFolder + "\n\n" + 
        "Unnamed Paths folder: \n" + unnamedFolder + "\n\n" + 
        "No Paths folder: \n" + noneFolder
    var x = app.displayAlert(alertText, {
        message: alertMessage,
        buttons: ["Change", "Confirm"],
        defaultButton: "Confirm",
    })

    if (x.buttonReturned === "Change") {
        selectFolders()
    }   
}

selectFolders()

app.displayDialog("Confirmed")

不确定我是否以尽可能最好的方式完成了它,但这似乎有效 - 它现在重复 selectFolders 函数直到按下 "Confirm",然后继续执行脚本。