Skip to main content
Participating Frequently
March 8, 2022
Answered

Script to export all smart objects layer names with scale %?

  • March 8, 2022
  • 1 reply
  • 557 views

I'm hoping someone can help me work out how to extract the scale percentage of smart objects into a text document somehow?  I'm finding other scripts that are kind of doing things I would like, but my limited scripting skills mean I can't hack things together to get what I need. (We are working with files we didn't create, so we are stuck with this mess)

 

I have files with many smart objects scattered throughout adjusted to different scales. I was wondering if it is possible at all to automatically create a csv file with a list of the layer names of all the smart objects, with their scale %'s next to them? 

For example:

Smart Object A - 30%

Smart Object B - 50%

Smart Object C - 120%

...etc. (ignoring non-smart object layers in the file)

The percentage scale data is available to play with if we can see it when we transform the smart object, surely? 

Any help will be greatly appreciated.

This topic has been closed for replies.
Correct answer jazz-y

Scale value that you see on the panel is calculated based on the transformation parameters.  If we are talking about simple transformations (proportional scaling and rotation), then the percentage can be obtained by calculating, for example, the ratio of the width of the object before and after the transformation (checked on CC 23.2.1):

 

#target photoshop;
var s2t = stringIDToTypeID;

(r = new ActionReference()).putProperty(s2t('property'), p = s2t('numberOfLayers'));
r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
var len = executeActionGet(r).getInteger(p),
    div = ';'
csv = ['name' + div + 'scale']
for (var i = 1; i <= len; i++) {
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('layerKind'));
    r.putIndex(s2t('layer'), i);
    if (executeActionGet(r).getInteger(p) == 5) {
        (r = new ActionReference()).putProperty(s2t('property'), p = s2t('name'));
        r.putIndex(s2t('layer'), i);
        var n = executeActionGet(r).getString(p) + div;
        (r = new ActionReference()).putProperty(s2t('property'), p = s2t('smartObjectMore'));
        r.putIndex(s2t('layer'), i);
        var t = executeActionGet(r).getObjectValue(p).getList(s2t('transform'));
        csv.push(n + (Math.round((Math.sqrt(Math.pow(t.getDouble(0) - t.getDouble(2), 2) + Math.pow(t.getDouble(1) - t.getDouble(3), 2))) /
            executeActionGet(r).getObjectValue(p).getObjectValue(s2t('size')).getDouble(s2t('width')) * 10000) / 100)+'%')
    }
}
var f = (new File(Folder.desktop + '/scale')).saveDlg('Save file', '*.csv');
if (f) {
    if (f.open('w', 'TEXT')) {
        f.write(csv.join('\n'))
        f.close()
    }
}

 

 

You can also check out the topic Get Layer Transform X Y Top Left Coordinate Position to understand that transforming a smart object can be quite tricky. 

1 reply

jazz-yCorrect answer
Legend
March 8, 2022

Scale value that you see on the panel is calculated based on the transformation parameters.  If we are talking about simple transformations (proportional scaling and rotation), then the percentage can be obtained by calculating, for example, the ratio of the width of the object before and after the transformation (checked on CC 23.2.1):

 

#target photoshop;
var s2t = stringIDToTypeID;

(r = new ActionReference()).putProperty(s2t('property'), p = s2t('numberOfLayers'));
r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
var len = executeActionGet(r).getInteger(p),
    div = ';'
csv = ['name' + div + 'scale']
for (var i = 1; i <= len; i++) {
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('layerKind'));
    r.putIndex(s2t('layer'), i);
    if (executeActionGet(r).getInteger(p) == 5) {
        (r = new ActionReference()).putProperty(s2t('property'), p = s2t('name'));
        r.putIndex(s2t('layer'), i);
        var n = executeActionGet(r).getString(p) + div;
        (r = new ActionReference()).putProperty(s2t('property'), p = s2t('smartObjectMore'));
        r.putIndex(s2t('layer'), i);
        var t = executeActionGet(r).getObjectValue(p).getList(s2t('transform'));
        csv.push(n + (Math.round((Math.sqrt(Math.pow(t.getDouble(0) - t.getDouble(2), 2) + Math.pow(t.getDouble(1) - t.getDouble(3), 2))) /
            executeActionGet(r).getObjectValue(p).getObjectValue(s2t('size')).getDouble(s2t('width')) * 10000) / 100)+'%')
    }
}
var f = (new File(Folder.desktop + '/scale')).saveDlg('Save file', '*.csv');
if (f) {
    if (f.open('w', 'TEXT')) {
        f.write(csv.join('\n'))
        f.close()
    }
}

 

 

You can also check out the topic Get Layer Transform X Y Top Left Coordinate Position to understand that transforming a smart object can be quite tricky.