Loop issues. not complete

I’m creating a function to help me with album layout and I’m having problems with the loop in app.batchPlay.
Could someone give me a light?

function criarQuadro() {
    try {
        // Obter o documento ativo
        const activeDocument = app.activeDocument;
        // Verificar se há uma seleção ativa
        if (activeDocument.selection) {
            // Obter as coordenadas da seleção
            const selectionBounds = activeDocument.selection.bounds;
            // Calcular as dimensões da seleção
            const width = selectionBounds.right - selectionBounds.left;
            const height = selectionBounds.bottom - selectionBounds.top;
            const horizontalDivisions = 4;
            const verticalDivisions = 4;
            const spaceBetweenColumns = 30; // Espaço entre as seleções na horizontal
            const spaceBetweenRows = 30; // Espaço entre as seleções na vertical

            const newWidth = (width - (spaceBetweenColumns * (horizontalDivisions - 1))) / horizontalDivisions;
            const newHeight = (height - (spaceBetweenRows * (verticalDivisions - 1))) / verticalDivisions;

            for (let h = 0; h < horizontalDivisions; h++) {
                for (let v = 0; v < verticalDivisions; v++) {
                    const left = selectionBounds.left + (h * (newWidth + spaceBetweenColumns));
                    const top = selectionBounds.top + (v * (newHeight + spaceBetweenRows));
                    const right = left + newWidth;
                    const bottom = top + newHeight;

                    // Aplicar quadro nas partes
                    // Mostrar as coordenadas em um alerta
                    core.executeAsModal(async () => {    
                      app.batchPlay([{
                        _obj: "make", _target: [{_ref: "framedGroupSection"}], framedGroupRect: { _obj: "classFloatRect",top: top,left: left, bottom: bottom, right: right}, PreferredResolution: 300,                    
                       
                    }], {});
                    app.batchPlay([{"_obj":"selectAllLayers","_target":[{"_enum":"ordinal","_ref":"layer"}]}], {});
                    });
                }
            }

            // Exibir as dimensões em uma alerta
            activeDocument.selection.deselect();

        } else {
            alert('Nenhuma seleção ativa no documento.');
        }
    } catch (error) {
        alert(`Você precisa ter uma seleção`);
    }
}

criarQuadro();
![2024-01-14 19-24-18|video](upload://nQ5S4qCE2EQvy08m6V8RF9f2Fbi.mp4)

Just off the top of my head, would using await on the executeAsModal and batchPlay lines help?

1 Like

Thanks for the tip. In fact, I had already noticed that the error was due to the lack of the asynchronous function.