skip to Main Content

Here is where I’m at:

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

    // Check if any frame is selected
    if (doc.selection.length > 0 && doc.selection[0].constructor.name == "Rectangle") {
        var selectedFrame = doc.selection[0];

        // Check if the selected frame contains any graphics
        if (selectedFrame.allGraphics.length > 0) {
            // Select the first graphic inside the frame (assuming it's an image)
            var graphic = selectedFrame.allGraphics[0];
            graphic.select();
        } else {
            alert("The selected frame does not contain any graphics.");
        }
    } else {
        alert("Please select a frame to proceed.");
    }
} else {
    alert("No document is open.");
}

when I have the frame selected and run the script, nothing happens. I do get an alert if I don’t select a frame. I have tried many variations of this but get nowhere. I AM VERY NEW TO CODING but learning a lot on here. thank you

2

Answers


  1. Try changing graphic.select() to app.select(graphic);

    Here’s a more flexible approach that simply checks to see if your selection has a graphics property. If so, it selects the first graphic associated with it.

    // Check if there is a selection
    if (app.selection.length > 0) {
        // Get the first item of the selection
        var selectedItem = app.selection[0];
        
        // Check if the selected item has a 'graphics' property and it contains graphics
        if (selectedItem.hasOwnProperty('graphics') && selectedItem.graphics.length > 0) {
            // Select the first graphic in the container
            app.select(selectedItem.graphics[0]);
        }
    }
    ``
    
    Login or Signup to reply.
  2. I’m used to get a selected graphic this way:

    // get selection or exit if nothing is selected
    var img = (app.selection.length > 0) ? app.selection[0] : exit(); 
    
    // if the selection is a container then try to select its graphic
    try { img = img.graphics[0] } catch(e) {};
    
    // do stuff with the 'img'
    // probably it makes sense to check here if the 'img' is an image
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search