Skip to main content
Known Participant
January 8, 2024
Answered

Change scale of image (content) without scaling the frame.

  • January 8, 2024
  • 1 reply
  • 390 views

Hello, I've built this script with the help of chatGPT (!) and it almost works! It find items with a specific object style and scale it.

 

My problem is that I would like the scale (percentage) to be directly apply to the images and not the frames.

 

Ex. : If I apply 50%, I would like every images percentage to be set at 50% x 50%, but for now, if an image is already at 80%, it scale it at 40%. If an image is distorted (30% x 60%) it stay distorted (15%x30%).

 

Any suggestions ? Thanks !

 

// Prompt the user for the object style name
var myObjectStyleName = prompt("Enter the object style name:", "Global Scale");

// Get the object style by name
var myObjectStyle = app.activeDocument.objectStyles.itemByName(myObjectStyleName);

// Check if the object style exists
if (myObjectStyle.isValid) {
// Prompt the user for the scale factor
var myScaleFactorInput = prompt("Enter the scale factor (e.g., 55):", "55");

// Convert the input to a number
var myScaleFactor = parseFloat(myScaleFactorInput);

// Check if the input is a valid number
if (isNaN(myScaleFactor)) {
alert("Invalid scale factor. Please enter a valid number.");
} else {
// Function to scale an individual page item
function scalePageItem(pageItem) {
// Check if the page item has the specified object style
if (pageItem.appliedObjectStyle == myObjectStyle) {
// Reset the image scaling to 100%
if (pageItem instanceof Image) {
pageItem.redefineScaling();
}

// Set the horizontal and vertical scale
pageItem.horizontalScale = pageItem.verticalScale = myScaleFactor;

// Fit the frame to content
pageItem.fit(FitOptions.FRAME_TO_CONTENT);
}

// If the item is a group, recursively scale its contents
if (pageItem.allPageItems) {
for (var i = 0; i < pageItem.allPageItems.length; i++) {
scalePageItem(pageItem.allPageItems[i]);
}
}
}

// Loop through all page items
for (var i = 0; i < app.activeDocument.pageItems.length; i++) {
scalePageItem(app.activeDocument.pageItems[i]);
}
}
} else {
// Handle the case where the object style doesn't exist
alert("Object style '" + myObjectStyleName + "' not found.");
}

 

This topic has been closed for replies.
Correct answer rob day

Hi @Zaphod , Does this work?

 

 

 

 

// Prompt the user for the object style name
var myObjectStyleName = prompt("Enter the object style name:", "Global Scale");

// Get the object style by name
var os = app.activeDocument.objectStyles.itemByName(myObjectStyleName);

// Check if the object style exists
if (os.isValid) {
    var s = prompt("Enter the scale factor (e.g., 55):", "55");
    var api = app.activeDocument.allPageItems
    var ag;
    for (var i = 0; i < api.length; i++){
        if (api[i].appliedObjectStyle == os) {
            ag = api[i].allGraphics
            for (var j = 0; j < ag.length; j++){
                if (ag[j].constructor.name == "Image" && ag[j].horizontalScale != Number(s)) {
                    ag[j].horizontalScale = ag[j].verticalScale = Number(s);
                    ag[j].parent.fit(FitOptions.FRAME_TO_CONTENT)
                } 
            };   
        } 
    };   
}else{alert("Object Style named " + myObjectStyleName + " does not exist")}

 

 

 

1 reply

rob day
Community Expert
rob dayCommunity ExpertCorrect answer
Community Expert
January 8, 2024

Hi @Zaphod , Does this work?

 

 

 

 

// Prompt the user for the object style name
var myObjectStyleName = prompt("Enter the object style name:", "Global Scale");

// Get the object style by name
var os = app.activeDocument.objectStyles.itemByName(myObjectStyleName);

// Check if the object style exists
if (os.isValid) {
    var s = prompt("Enter the scale factor (e.g., 55):", "55");
    var api = app.activeDocument.allPageItems
    var ag;
    for (var i = 0; i < api.length; i++){
        if (api[i].appliedObjectStyle == os) {
            ag = api[i].allGraphics
            for (var j = 0; j < ag.length; j++){
                if (ag[j].constructor.name == "Image" && ag[j].horizontalScale != Number(s)) {
                    ag[j].horizontalScale = ag[j].verticalScale = Number(s);
                    ag[j].parent.fit(FitOptions.FRAME_TO_CONTENT)
                } 
            };   
        } 
    };   
}else{alert("Object Style named " + myObjectStyleName + " does not exist")}

 

 

 

ZaphodAuthor
Known Participant
January 8, 2024

Yes, that's perfect, thank you so much for your time. This will impact my work greatly !

 

Have a nive day !