Skip to main content
Participant
May 22, 2024
Answered

is it possible to select a random area with scripting?

  • May 22, 2024
  • 3 replies
  • 365 views

Sorry if this is basic, is there a way to script the selection of a random area in photoshop?

This topic has been closed for replies.
Correct answer Stephen Marsh
quote

Sorry if this is basic, is there a way to script the selection of a random area in photoshop?


By @Mark5C6C

 

I wouldn't say it's basic, but it's possible.

 

I presume that you are referring to both the size and position. I would expect that there should be both a minimum and maximum size for the selection compared to the canvas size. What other criteria, such as the canvas edges, is it OK for the selection to go to the edge of the canvas, or should it always have a "safe zone" where it has to keep away from the canvas edges? Is this a rectangular selection or something else?

 

What is the use case?

 

Edit: This is what ChatGPT regurgitated:

 

// Ensure a document is open
if (app.documents.length > 0) {
    var doc = app.activeDocument;

    // Get the canvas dimensions
    var canvasWidth = doc.width.as("px");
    var canvasHeight = doc.height.as("px");

    // Generate random values for the selection
    var randomWidth = Math.floor(Math.random() * canvasWidth);
    var randomHeight = Math.floor(Math.random() * canvasHeight);
    var randomX = Math.floor(Math.random() * (canvasWidth - randomWidth));
    var randomY = Math.floor(Math.random() * (canvasHeight - randomHeight));

    // Define the selection coordinates
    var selectionCoords = [
        [randomX, randomY],
        [randomX + randomWidth, randomY],
        [randomX + randomWidth, randomY + randomHeight],
        [randomX, randomY + randomHeight]
    ];

    // Make the selection
    doc.selection.select(selectionCoords);

    // Inform the user
    alert("Random selection created at (" + randomX + ", " + randomY + ") with dimensions " + randomWidth + "x" + randomHeight + " pixels.");
} else {
    alert("No document is open. Please open a document and try again.");
}

 

3 replies

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
May 22, 2024
quote

Sorry if this is basic, is there a way to script the selection of a random area in photoshop?


By @Mark5C6C

 

I wouldn't say it's basic, but it's possible.

 

I presume that you are referring to both the size and position. I would expect that there should be both a minimum and maximum size for the selection compared to the canvas size. What other criteria, such as the canvas edges, is it OK for the selection to go to the edge of the canvas, or should it always have a "safe zone" where it has to keep away from the canvas edges? Is this a rectangular selection or something else?

 

What is the use case?

 

Edit: This is what ChatGPT regurgitated:

 

// Ensure a document is open
if (app.documents.length > 0) {
    var doc = app.activeDocument;

    // Get the canvas dimensions
    var canvasWidth = doc.width.as("px");
    var canvasHeight = doc.height.as("px");

    // Generate random values for the selection
    var randomWidth = Math.floor(Math.random() * canvasWidth);
    var randomHeight = Math.floor(Math.random() * canvasHeight);
    var randomX = Math.floor(Math.random() * (canvasWidth - randomWidth));
    var randomY = Math.floor(Math.random() * (canvasHeight - randomHeight));

    // Define the selection coordinates
    var selectionCoords = [
        [randomX, randomY],
        [randomX + randomWidth, randomY],
        [randomX + randomWidth, randomY + randomHeight],
        [randomX, randomY + randomHeight]
    ];

    // Make the selection
    doc.selection.select(selectionCoords);

    // Inform the user
    alert("Random selection created at (" + randomX + ", " + randomY + ") with dimensions " + randomWidth + "x" + randomHeight + " pixels.");
} else {
    alert("No document is open. Please open a document and try again.");
}

 

Legend
May 22, 2024

Truly random? As in whatever area the script decides randomly?

May 22, 2024

you can use a selection tool for example the square and generate an image inside that box.