Skip to main content
Known Participant
July 23, 2023
Answered

Script to Scale selected content graphic frame Data merge

  • July 23, 2023
  • 4 replies
  • 2993 views

Hi

I am preparing a date merge process , I want to scale X and Y For each content graphic frame of 5 graphic frames by a certain presentage different from other graphic frames

Is there are a script to do that Automatically 

This topic has been closed for replies.
Correct answer m1b

Hi @M.Hasanin, thank you for doing this, very well done. Your scripting is going very well. As it happened (one minute later than you!) I was about to post a version of my same script that handled scaling also. I will post it here so you may see if I did anything differently.

- Mark

/**
 * Apply scale and rotation to graphics according to their script label.
 * eg. if label is "scale: 110, rotate: 15" then the graphic will be
 * scaled to 110% and rotated to 15° counter-clockwise.
 * @author m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/script-to-rotate-selected-image-inside-frame-data-merge/m-p/13955151
 */
function main() {

    var doc = app.activeDocument,
        images = doc.allGraphics,
        rotationMatcher = /\brotate:\s*(-?\d+\.?\d*)/i,
        scaleMatcher = /\bscale:\s*(\d+\.?\d*)/i,
        label,
        scale,
        rotation;

    for (var i = 0; i < images.length; i++) {

        label = images[i].label || images[i].parent.label;

        if (!label)
            continue;

        rotation = label.match(rotationMatcher);
        scale = label.match(scaleMatcher);

        if (
            !rotation
            || rotation.length < 2
            || isNaN(rotation = Number(rotation[1]))
        )
            rotation = undefined;
        else
            rotation -= images[i].absoluteRotationAngle;

        if (
            !scale
            || scale.length < 2
            || isNaN(scale = Number(scale[1]))
        )
            scale = undefined;
        else
            scale = scale / images[i].absoluteHorizontalScale;

        if (!rotation && !scale)
            continue;

        var tm = app.transformationMatrices.add({
            counterclockwiseRotationAngle: rotation,
            horizontalScaleFactor: scale,
            verticalScaleFactor: scale,
        });

        images[i].transform(
            CoordinateSpaces.pasteboardCoordinates,
            AnchorPoint.centerAnchor,
            tm,
        );

    }

};

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Transform Graphics');

4 replies

M.Hasanin
Inspiring
July 23, 2023

hi @r28071715i111 , here is another version as you asked me in private message that will scale and rotate in same time :

/**
 * Apply Scale and Rotation to graphics script labels
 * eg. if label is "r:30 s:10", then the graphic will be rotated to 30 degree and scaled to 10%
 * @Originally authored by m1b for rotation version 
 * Updated Mixed Rotation and Scale Version By M.Hasanin
 * @discussion https://community.adobe.com/t5/indesign-discussions/script-to-rotate-selected-image-inside-frame-data-merge/m-p/13955151
 */

function main() {

    var doc = app.activeDocument,
        tm = app.transformationMatrices.add({ counterclockwiseRotationAngle: 0 }),
        images = doc.allGraphics,
        matcher = /^r:(-?\d+\.?\d*)\s+s:(-?\d+\.?\d*)/i, // match both values in one regex (Rotation and Scale with Space)
        label,
        value1,
        value2;

    for (var i = 0; i < images.length; i++) {

        label = images[i].label || images[i].parent.label;

        if (!label)
            continue;

        var matches = label.match(matcher);

        if (!matches || matches.length < 3) // check for both values
            continue;

        value1 = Number(matches[1]); // set value1 to the first match Rotate
        value2 = Number(matches[2]); // set value2 to the second match Scale Percentage


        images[i].transform( //Roatation
            CoordinateSpaces.pasteboardCoordinates,
            AnchorPoint.centerAnchor,
            tm.rotateMatrix(-images[i].absoluteRotationAngle + value1),
        );
        
        images[i].horizontalScale = images[i].verticalScale = value2; //Scale Percentage

    }

};

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Scale and Rotate Graphics');
Mohammad Hasanin
Known Participant
July 23, 2023

Thanks For Great effort Mr Mohammad Hasanin

I also tried it and worked like a charm

That script that I need definitly

Thanks

M.Hasanin
Inspiring
July 24, 2023

@r28071715i111  

You are welcome

Mohammad Hasanin
M.Hasanin
Inspiring
July 23, 2023

Hi @r28071715i111 , here is a version that will scale in percentage :

/**
 * Apply Scale to graphics with a "Scale" script label.
 * eg. if label is "s:10", then the graphic will be scaled to 10%
 * @Originally authored by m1b for roation version 
 * Updated Scale Version By M.Hasanin
 * @discussion https://community.adobe.com/t5/indesign-discussions/script-to-rotate-selected-image-inside-frame-data-merge/m-p/13955151
 */
function main() {

    var doc = app.activeDocument,
        images = doc.allGraphics,
        matcher = /^s:(-?\d+\.?\d*)/i,
        label,
        value;

    for (var i = 0; i < images.length; i++) {

        label = images[i].label || images[i].parent.label;

        if (!label)
            continue;

        value = label.match(matcher);

        if (!value
            || value.length < 2
            || isNaN(value = Number(value[1]))
        )
            continue;

        images[i].horizontalScale =  images[i].verticalScale = value;

    }

};

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Scale Graphics Percent');
Mohammad Hasanin
Known Participant
July 23, 2023

Thanks For Great effort Mr Mohammad Hasanin

I tried it and worked like a charm

very very Thanks

Participant
July 23, 2023

Hello,

Yes, you can automate the process of scaling X and Y for each content graphic frame using a script. However, the exact script you need will depend on the software or website programming language you are using to work with these graphic frames.

If you are working with a graphic design software like Adobe Illustrator or Photoshop, you might be able to use their built-in scripting capabilities. Both applications support scripting using JavaScript. You can write a script that iterates through the graphic frames and applies the desired scaling percentage to each one.

Here's a general outline of how the script might work in JavaScript for Adobe Illustrator:

  1. Get a reference to the graphic frames you want to scale.
  2. Iterate through each graphic frame.
  3. Apply the scaling percentage to the X and Y properties of the frame.

For Adobe Photoshop, the scripting process would be similar, but you would use Photoshop's JavaScript API to manipulate the graphic frames.

If you are working with a different software or a programming language, the approach might vary. Some graphic design software might have their own scripting languages or APIs to achieve similar results.

If you provide more details about the software you are using or the specific programming language you want to work with, I can try to help you with a more specific script example.

Known Participant
July 23, 2023

Thanks For Your great reply

I want script for Adobe indesign only

Not illustrator and photoshop

Known Participant
July 23, 2023

Any Help here

Robert at ID-Tasker
Legend
July 23, 2023

It's a simple modification of the other script you've already received on the other thread about rotating objects  - I'm not JS guy so can't help you but please ask there.

 

Known Participant
July 23, 2023

Thanks for your reply

I hope one one of js can help me in this script