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

49 Views Asked by At

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

There are 2 best solutions below

2
user1754036 On BEST ANSWER

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]);
    }
}
0
Yuri Khristich On

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