shape dimensions to text script

New Here ,
Aug 06, 2022 Aug 06, 2022

Copy link to clipboard

Copied

Hi am looking for a script that will translate the shape dimensions in (centimetres) into a text layer, even if the script copies the data, I could then select the text tool and paste the data or the script just makes the text itself.

For example below

Untitlerrrrrd-1.jpg

This would be a great help. I must say I'm very impressed with the adobe forum, very helpful and knowledgable people here. Cheers and thank you for your help

TOPICS
Actions and scripting , Windows

Views

75

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Aug 06, 2022 Aug 06, 2022

Copy link to clipboard

Copied

@nig25542238q2e0 – How do you wish to handle results as below:

 

15.0029333333333 cm x 8.001 cm

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 06, 2022 Aug 06, 2022

Copy link to clipboard

Copied

Ok thats alot of decimal places, can that not be fixed or have less numbers? if not I would just delete the numbers I don't need in the text field. Also the word width and height are important I think.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Aug 06, 2022 Aug 06, 2022

Copy link to clipboard

Copied


@nig25542238q2e0 wrote:

Ok thats alot of decimal places, can that not be fixed or have less numbers? 


 

Yes there are and yes it can be "fixed". Pixels and CM don't always play nice.

 

This is why I asked what you required. It depends on your required level of precision. I originally rounded to the nearest integer, however, I think that Tom is right in going for 2 decimal places.

 

#target photoshop
// Set the rulers
var origRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.CM;
// Layer bounds
var layerBounds = activeDocument.activeLayer.bounds;
var layerWidth = layerBounds[2].value - layerBounds[0].value;
var layerHeight = layerBounds[3].value - layerBounds[1].value;
// Dimensions to 2 decimals
layerWidth = layerWidth.toFixed(2);
layerHeight = layerHeight.toFixed(2);
var dims = "width = " + layerWidth.toString() + " cm x height = " + layerHeight.toString() + " cm";
// Copy dimensions to clipboard
var d = new ActionDescriptor();
d.putString(stringIDToTypeID('textData'), dims);
executeAction(stringIDToTypeID('textToClipboard'), d, DialogModes.NO);
// Restore the original rulers
app.preferences.rulerUnits = origRulerUnits;
// End of script
alert(dims + "\r" + "(copied to clipboard)");

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 06, 2022 Aug 06, 2022

Copy link to clipboard

Copied

Thank you soo very much Stephen, fantastic work.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Aug 06, 2022 Aug 06, 2022

Copy link to clipboard

Copied

rundimentary script with rounded results...

 

#target photoshop

Dimensions();

function Dimensions(){
    var layer = activeDocument.activeLayer;

    var width = Math.round((layer.bounds[2]-layer.bounds[0]).as("cm"));
    var height = Math.round((layer.bounds[3]-layer.bounds[1]).as("cm"));
    var textLayer = activeDocument.artLayers.add();
    textLayer.kind = LayerKind.TEXT;
    textLayer.name = "Dimensions";
    var textReference = textLayer.textItem;
    textReference.contents = "width = " + width + " cm x height = " + height + " cm";
}

 

...2 digits...

// Current layer name as text layer

#target photoshop

Dimensions();

function Dimensions(){
    var layer = activeDocument.activeLayer;

    var width = (layer.bounds[2]-layer.bounds[0]).as("cm").toFixed(2);
    var height = (layer.bounds[3]-layer.bounds[1]).as("cm").toFixed(2);
    var textLayer = activeDocument.artLayers.add();
    textLayer.kind = LayerKind.TEXT;
    textLayer.name = "Dimensions";
    var textReference = textLayer.textItem;
    textReference.contents = "width = " + width + " cm x height = " + height + " cm";
}

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 06, 2022 Aug 06, 2022

Copy link to clipboard

Copied

Thank you soo very much Tom, fantastic work.

 

I've been reading on how to round it up or down to the nearest half(.5) or whole number, but cant seem to implement it.

Exampe the shape size is 9.99 I'd like it to be a 10 and 3.7 would be 3.5 sample as in excel

width = 10cm x height = 3.5cm 

Thank you anythow you have been a great help.

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Aug 06, 2022 Aug 06, 2022

Copy link to clipboard

Copied

I see other's already gave you some code. I wrote this last night before you posted this thread, but pretty much does the same thing to two decimial places. I wasn't sure how big you wanted the text, so I made it so it will be adjusted to 1/4 of the document width.

#target photoshop
var doc = activeDocument;
var lay = doc.activeLayer;
var width = lay.bounds[2]-lay.bounds[0];
var wNum = width.value.toFixed (2)
var wUnit = width.toString().split(' ')[1]
var height = lay.bounds[3]-lay.bounds[1];
var hNum = height.value.toFixed (2)
var hUnit = height.toString().split(' ')[1]
var artLayerRef = doc.artLayers.add()
artLayerRef.kind = LayerKind.TEXT;
var textItemRef = artLayerRef.textItem;
textItemRef.size = 30;
textItemRef.contents = 'width = '+ wNum + ' ' +wUnit +' X height = ' + hNum + ' ' + hUnit;
var tLayer = doc.activeLayer
var txtW = tLayer.bounds[2].value-tLayer.bounds[0].value;
var tLimit = .25
var sFactor = (doc.width.value * .25)/txtW
tLayer.resize (sFactor*100, sFactor*100, AnchorPosition.BOTTOMCENTER)

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 06, 2022 Aug 06, 2022

Copy link to clipboard

Copied

Thank you soo very much Chuck, fantastic work, and thank you for a pleasant 1st-time introduction to this forum.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 06, 2022 Aug 06, 2022

Copy link to clipboard

Copied

LATEST

Also thank you for the output text size hack as you probably noticed the text layer was too small, being able to adjust that has really helped a lot.

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines