• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Explorer ,
Aug 31, 2017 Aug 31, 2017

Copy link to clipboard

Copied

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?

TOPICS
Scripting

Views

5.2K

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

correct answers 1 Correct answer

Community Expert , Sep 07, 2017 Sep 07, 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();

fo

...

Votes

Translate

Translate
Adobe
Community Expert ,
Aug 31, 2017 Aug 31, 2017

Copy link to clipboard

Copied

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();

Votes

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
Explorer ,
Sep 01, 2017 Sep 01, 2017

Copy link to clipboard

Copied

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

Votes

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
Valorous Hero ,
Sep 01, 2017 Sep 01, 2017

Copy link to clipboard

Copied

Make the following edit to this line:

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

Votes

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
Explorer ,
Sep 01, 2017 Sep 01, 2017

Copy link to clipboard

Copied

Cool! Thank you very much!

Votes

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
Explorer ,
Sep 02, 2017 Sep 02, 2017

Copy link to clipboard

Copied

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?

Votes

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
Community Expert ,
Sep 03, 2017 Sep 03, 2017

Copy link to clipboard

Copied

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;

Votes

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
Explorer ,
Sep 03, 2017 Sep 03, 2017

Copy link to clipboard

Copied

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

3.png

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.

Votes

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
Explorer ,
Sep 07, 2017 Sep 07, 2017

Copy link to clipboard

Copied

Ten A, is it possible to implement a script so that it takes into account the total length / width of the selected objects?

I assume that the script cuts objects from double selection because of the app.cut() function.

Can it be better if a separate layer is created for the text?

Or so that the height / width values are copied from the text, and the text itself is deleted with the help of a step backward (Ctrl + Z)?

Votes

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
Community Expert ,
Sep 07, 2017 Sep 07, 2017

Copy link to clipboard

Copied

You can make a layer that can be add textFrames to cut values.

If you make a layer named "scriptWork", You can access like below

app.activeDocument.layers.getByName("scriptWork");

therefor previouse code rewrite like below.

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

If you select some object, replace "sel.selected = false;" to

app.executeMenuCommand("deselectall");

Please note that I do not have test environment now and codes not tested. However, these may work.

Votes

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
Explorer ,
Sep 07, 2017 Sep 07, 2017

Copy link to clipboard

Copied

As you suggested, I changed the code:

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; 

Indeed, the script no longer cuts out objects. However, it still copies the values of only one shape of the two.

Votes

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 ,
Feb 20, 2020 Feb 20, 2020

Copy link to clipboard

Copied

LATEST

Is there a way to get width & height in meters instead of pixels? (And with only 3 decimals : 0,111m x 0,222m).

Thanks.

Votes

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
Community Expert ,
Sep 07, 2017 Sep 07, 2017

Copy link to clipboard

Copied

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

Votes

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
Explorer ,
Sep 07, 2017 Sep 07, 2017

Copy link to clipboard

Copied

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

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

Votes

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
Community Expert ,
Sep 07, 2017 Sep 07, 2017

Copy link to clipboard

Copied

Yes, you can do it.

Votes

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
Explorer ,
Sep 07, 2017 Sep 07, 2017

Copy link to clipboard

Copied

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?

Votes

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
Community Expert ,
Sep 07, 2017 Sep 07, 2017

Copy link to clipboard

Copied

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.

Votes

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
Explorer ,
Sep 08, 2017 Sep 08, 2017

Copy link to clipboard

Copied

Thank you very much! You helped me a lot. Links are very useful.

Votes

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
Community Expert ,
Sep 08, 2017 Sep 08, 2017

Copy link to clipboard

Copied

You're welcome.

Please keep studying about it will helps your work.

Votes

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