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?
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
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();
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
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);
Copy link to clipboard
Copied
Cool! Thank you very much!
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?
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;
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.
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.
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)?
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.
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.
Copy link to clipboard
Copied
Is there a way to get width & height in meters instead of pixels? (And with only 3 decimals : 0,111m x 0,222m).
Thanks.
Copy link to clipboard
Copied
You have to make "for" loop and put your code into it.
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?
Copy link to clipboard
Copied
Yes, you can do it.
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?
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.
Copy link to clipboard
Copied
Thank you very much! You helped me a lot. Links are very useful.
Copy link to clipboard
Copied
You're welcome.
Please keep studying about it will helps your work.