Skip to main content
Participating Frequently
August 19, 2021
Answered

Script Copy Object or Artboard dimensions (text) to clipboard

  • August 19, 2021
  • 1 reply
  • 2427 views

Super novice to scripting, as in the extent of my knowlodge is copy, pasting and tinkering with sampled scripts.

 

Request:

But what I'm trying to achieve is a script that will copy the dimensions of an object or artboard as text (wxh) into my clipboard. A nice window to confirm the dimensions would be great as well.

 

Reason: 

I work with a lot of files where I need to include the dimensions at the end of the filename. It can get kinda tideous, especially when I have to type out a long string like 2.5inx4.875in. It especially sucks when I have my file correctly named and then I need to type the dimensions, only to notice I haven't selected my object, which helps by having the content appear in the transform panel.

 

Thank you advance and I know I'm asking for two scripts essentially. I guess the object dimension would be the most important at this time. If someone can help me figure this out, it would help me out greatly with my workflow and time.

This topic has been closed for replies.
Correct answer femkeblanco

I did this in a rush.  If an item is selected, the item's dimensions will be copied, if no items is selected, the artboards dimensions will be copied:

 

var doc = app.activeDocument;
if (doc.selection.length > 0) {
    var w = (doc.selection[0].width / 72).toFixed(3);
    var h = (doc.selection[0].height / 72).toFixed(3);
} else {
    var w = (doc.width / 72).toFixed(3);
    var h = (doc.height / 72).toFixed(3);
}
var dimensions = w + "inx" + h + "in";
alert(dimensions);
var AR = doc.artboards[0].artboardRect;
var rect1 = doc.pathItems.rectangle(0, AR[2]-100, 100, 100);
var text1 = doc.textFrames.areaText(rect1);
text1.contents = dimensions;
app.selection = null;
doc.pageItems[0].selected = true;
app.cut();

 

1 reply

femkeblanco
femkeblancoCorrect answer
Legend
August 19, 2021

I did this in a rush.  If an item is selected, the item's dimensions will be copied, if no items is selected, the artboards dimensions will be copied:

 

var doc = app.activeDocument;
if (doc.selection.length > 0) {
    var w = (doc.selection[0].width / 72).toFixed(3);
    var h = (doc.selection[0].height / 72).toFixed(3);
} else {
    var w = (doc.width / 72).toFixed(3);
    var h = (doc.height / 72).toFixed(3);
}
var dimensions = w + "inx" + h + "in";
alert(dimensions);
var AR = doc.artboards[0].artboardRect;
var rect1 = doc.pathItems.rectangle(0, AR[2]-100, 100, 100);
var text1 = doc.textFrames.areaText(rect1);
text1.contents = dimensions;
app.selection = null;
doc.pageItems[0].selected = true;
app.cut();

 

ajabon grinsmith
Community Expert
Community Expert
August 19, 2021
Participating Frequently
August 19, 2021

Tried that one and didn't work for me.... but now just realized I've been uploading/using javascript incorrectly. So that explains why theirs didn't work *facepalm.

But @femkeblanco script is so much sweeter anyways. So thankful I posted and they answered.