javascriptgraphicsadobe-indesign

I have been working on an InDesign script that will select the actual image inside of a graphic frame ... is this possible


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


Solution

  • 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]);
        }
    }