Search code examples
javascriptmacosphotoshop-script

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


Apologies if this is a silly question, I'm really new to coding. I have a script that I'm currently running through Photoshop on a Mac, which sorts through images in a folder and saves them to one of several other folders. It works fine, but I have to manually type in the filepaths and re-save the script each time, which is a nuisance.

Instead I want to prompt for the folders at the start, and then give the option to confirm the selection, or start again. The folder prompts are working, but I have no idea how to make the script repeat if the user presses the cancel button.

I've tried searching online for answers, but since I don't really know what I'm looking for I'm not getting very far. My best guess is that maybe I need to use an if/else statement targeting the buttons, or a loop that reiterates until the confirm button is pressed.

Here's what I have so far:

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"
})

If the "Confirm" button is pressed I want the script to continue on to the next section where it will start sorting the images into the chosen folders. If "Change" is pressed, I want this section of code to start again. How can I make this happen?


Solution

  • I think I may have figured it out (thanks Medet Tleukabiluly for mentioning functions, it gave me a new line of exploration):

    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")
    

    Not sure if I've done it in the best way possible, but this seems to work - it now repeats the selectFolders function until "Confirm" is pressed, then continues through the script.