Skip to main content
Known Participant
July 3, 2022
Question

Scaling

  • July 3, 2022
  • 2 replies
  • 7211 views

Hi all, can anyone please help me to retrieve a scaling value of  image in illustrator. How can we identify which scaling has been applied to the image using script. I would like to know how to check whether the image has been applied horizontal scale or vertical scale. Thank you. 

This topic has been closed for replies.

2 replies

m1b
Community Expert
Community Expert
July 3, 2022

Hi @Abpujar, please try this. - Mark

Edit 1: improved script.

Edit 2: updated script to better handle RasterItems which have flipped scale Y values. Still more work to fix fully.

Edit 3: now handles RasterItems and PlacedItems correctly (I think!).

 

 

/**
 * Get PlacedItem Scale And Rotation
 * for Adobe Illustrator
 *
 * by m1b
 * discussion: https://community.adobe.com/t5/illustrator-discussions/scaling/m-p/13050842
 *
 */


var doc = app.activeDocument,
    item = doc.selection[0],
    result = getLinkScaleAndRotation(item);

if (result)
    alert('Results:\nHorizontal scale: ' + result[0] + '%\nVertical scale: ' + result[1] + '%\nRotation: ' + result[2] + '°');



/**
 * Return a linked item's scale.
 * @author m1b
 * @param {PlacedItem|RasterItem} item - an Illustrator item
 * @returns {Array} [scaleX%, scaleY%, rotation°]
 */
function getLinkScaleAndRotation(item) {
    if (item == undefined)
        return;

    var m = item.matrix,
        rotatedAmount,
        unrotatedMatrix,
        scaledAmount;

    var flipPlacedItem = (item.typename == 'PlacedItem')
        ? 1
        : -1;

    try {
        rotatedAmount = item.tags.getByName('BBAccumRotation').value * 180 / Math.PI;
    } catch (error) {
        rotatedAmount = 0;
    }
    unrotatedMatrix = app.concatenateRotationMatrix(m, rotatedAmount * flipPlacedItem);
    scaledAmount = [unrotatedMatrix.mValueA * 100, unrotatedMatrix.mValueD * -100 * flipPlacedItem];

    return [round(scaledAmount[0]), round(scaledAmount[1]), round(rotatedAmount)];
}



/**
 * Rounds `n` to `places` decimal places.
 * @param {Number} n - the number to round
 * @param {Number} places - number of decimal places, can be negative
 * @returns {Number}
 */
function round(n, places) {
    var m = Math.pow(10, places || 4);
    return Math.round(n * m) / m;
}

 

 

 

AbpujarAuthor
Known Participant
July 4, 2022

Hi @m1b @thank you for the reply, I’m getting the scaling value but I need -ve or +ve value, for example if the image has been applied horizontal scale then the scaling value should be -ve or if the image has a vertical scale then the vertical scale value should be in -ve. If the image has not been applied any scaling then the scaling value should be in +ve. Thank you. 

m1b
Community Expert
Community Expert
July 14, 2022

Hi, I have an issue while retrieving RasterItems(raster image), how can I retrieve the raster image file details? Is there any file property for RasterItems?


Hi @SA8888, yes. If the rasterItem has a file property, you can use that. Sometimes a rasterItem does not have a file property, for example when it is pasted into Illustrator, or came from some source that did not have a file.

 

You can get a rasterItem's file (if it has one) like this:

var doc = app.activeDocument,
    item = doc.selection[0];

if (
    item.hasOwnProperty('file')
    && item.file.constructor.name === 'File'
    && item.file.exists
) {
    // file is good
    var f = item.file;
    $.writeln('file is good');
    $.writeln(f.fsName);
}

 

-Mark

Ton Frederiks
Community Expert
Community Expert
July 3, 2022

The Links panel gives scaling info.