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 2, 2017

How to make the script not remove the selection from the current object?

The script creates the text and cuts the value. Is it possible that after these operations he would select my last object?

maxham12
maxham12Author
Known Participant
September 4, 2017

How about this?

var sel = app.selection[0];

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

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

sel.selected = false; 

tx.selected = true; 

tx.contents = str; 

app.cut();

sel.selected = true;


Works great! But I noticed one bug of all versions of the script - it does not work if the top layer is blocked.

By the way, if you select two or more shapes (which are not grouped), then in the first run of the script, it will remove one of the shapes, and in the second run of the script, the Illustrator closes.