Skip to main content
maxham12
Known Participant
August 31, 2017
Answered

Scripts that copy the width/height of the object to the clipboard

  • August 31, 2017
  • 3 replies
  • 6683 views

Could you create two scripts that copy the width/height (only a fractional number without "px") of the selected object (or group of objects) to the clipboard?

This topic has been closed for replies.
Correct answer Ten A

I rewrite your modifications.

var myDoc = app.activeDocument;

var myLayer = myDoc.layers.add();  //in this case, You can keep your layer in ths variable.

var sel = app.selection; //sel as selections array.

var str = "";

for (var i=0;i<sel.length;i++) { //loop and check each selected objects

  str += sel.width.toFixed(3) + "/" + sel.height.toFixed(3) + "\n";

  }

var tx = myLayer.textFrames.add();

tx.contents = str;

app.executeMenuCommand("deselectall"); //deselect all abjects

tx.selected = true;

app.cut();

for (i=0;i<sel.length;i++) sel.selected = true;  //select all of last selections

myLayer.remove();

Here is some references you can read and try.

https://www.w3schools.com/js/default.asp

ex:how to make loop

https://www.w3schools.com/js/js_loop_for.asp

Have you aleady install Extendscript Toolkit CC?

If you aleady install it, You can read references under the Help menu.

3 replies

Ten A
Community Expert
Community Expert
September 7, 2017

Yes, you can do it.

maxham12
maxham12Author
Known Participant
September 7, 2017

I managed:

var layerName = LayerOrderType;

var myDoc = app.activeDocument;

var layerName = "scriptWork";  var myLayer = myDoc.layers.add(); myLayer.name = layerName;

var sel = app.selection[0];   

var str = sel.width.toFixed(3) + "/" + app.selection[0].height.toFixed(3);     

var tx = app.activeDocument.layers.getByName("scriptWork").textFrames.add(); //tx as textFrame object 

app.executeMenuCommand("deselectall");   

tx.selected = true;     

tx.contents = str;     

app.cut();   

sel.selected = true; 

app.activeDocument.layers.getByName('scriptWork').remove();

But I can not implement this in the script:

the selected shape is grouped, and after copying the value to the clipboard is ungrouped. I do not know where to insert a function.

Can you help?

Ten A
Community Expert
Ten ACommunity ExpertCorrect answer
Community Expert
September 8, 2017

I rewrite your modifications.

var myDoc = app.activeDocument;

var myLayer = myDoc.layers.add();  //in this case, You can keep your layer in ths variable.

var sel = app.selection; //sel as selections array.

var str = "";

for (var i=0;i<sel.length;i++) { //loop and check each selected objects

  str += sel.width.toFixed(3) + "/" + sel.height.toFixed(3) + "\n";

  }

var tx = myLayer.textFrames.add();

tx.contents = str;

app.executeMenuCommand("deselectall"); //deselect all abjects

tx.selected = true;

app.cut();

for (i=0;i<sel.length;i++) sel.selected = true;  //select all of last selections

myLayer.remove();

Here is some references you can read and try.

https://www.w3schools.com/js/default.asp

ex:how to make loop

https://www.w3schools.com/js/js_loop_for.asp

Have you aleady install Extendscript Toolkit CC?

If you aleady install it, You can read references under the Help menu.

Ten A
Community Expert
Community Expert
September 7, 2017

You have to make "for" loop and put your code into it.

maxham12
maxham12Author
Known Participant
September 7, 2017

Unfortunately, I do not yet know how to do this.

Is it possible to create and delete the scriptWork layer automatically?

Ten A
Community Expert
Community Expert
September 1, 2017

If you want to get only text and paste it in text editor, you can use below code.

var str = app.selection[0].width + "/" + app.selection[0].height;

var tx = app.activeDocument.textFrames.add();

app.selection[0].selected = false;

tx.selected = true;

tx.contents = str;

app.cut();

maxham12
maxham12Author
Known Participant
September 1, 2017

Great! You helped me a lot!

How to make the script round to a value of three digits after the decimal point?

1.24682468246824  →  1.247

Silly-V
Legend
September 1, 2017

Make the following edit to this line:

var str = app.selection[0].width.toFixed(3) + "/" + app.selection[0].height.toFixed(3);