Copy link to clipboard
Copied
I would like to find a script which will take the dimensions of an object or several grouped objects and make a line of text in the illustrator file with the dimensions. Anyone know how this could be done?
here you go
...#target Illustrator
// script by grizzlyburr?
myDoc = app.activeDocument;
findDims(app.selection);
function findDims(objs) {
for (i=objs.length-1; i>=0; i--) { //loop through your collection of objects
var vb = objs.geometricBounds; // left, top, right, bottom // * changed to geometricBounds
var w = (vb[2] - vb[0]);
var h = (vb[1] - vb[3]);
if (objs.parent.typename != "GroupItem") { // Check if parent is a groupItem
descFrame = myDoc.tex
Copy link to clipboard
Copied
Wow, totally snubed. I'm new to scripting and need help but I don't see any responce. Is this an extremely difficult script to learn, or write, or find?
Copy link to clipboard
Copied
Can you be a little more specific about what you're attempting?
Do you just want a text frame that has the same dimensions as the page item, or are you wanting the your text to be typed along a path with the same path points as your reference object (see the "Type on a Path Tool" in your tools palette).
Cheers.
Copy link to clipboard
Copied
I want to have several shapes which I have grouped. So I need the dimensions of all these shapes when grouped (the parameter or foot print) to a single stand alone line of text. I can cut and paste these dimensions from the W and H in the transform panel to a line of text to indicate the foot print of all the shapes selected. In essence I need to get the W and H to magically appear as a line of text in the illustrator file. Then I could add the script to the current set of actions I use for this process and automate the process. Does this make more sense?
Copy link to clipboard
Copied
Okay, I think I follow now. Something like this might work:
myDoc = app.activeDocument;
findDims(myDoc.groupItems);
findDims(myDoc.pathItems);
function findDims(objs) {
for (i=objs.length-1;i>=0;i--) { //loop through your collection of objects
if (objs.parent.constructor != GroupItem) { // Check if parent is a groupItem and skip if it is
descFrame = myDoc.textFrames.add(); // Create the text frame
descFrame.contents = "W: "+objs.width + "pts\rH: " + objs.height + "pts"; // Adds contents to frame
descFrame.position = [objs.left,objs.top - objs.height - 5]; // Positions the frame
}
}
}
Cheers.
Copy link to clipboard
Copied
Works fine even for grouped objects, but remember that a script added to an action does not survive a restart. The action must be remade each time.
Copy link to clipboard
Copied
myDoc = app.activeDocument;
findDims(myDoc.groupItems);
findDims(myDoc.pathItems);
function findDims(objs) {
for (i=objs.length-1;i>=0;i--) { //loop through your collection of objects
if (objs.parent.constructor != GroupItem) { // Check if parent is a groupItem and skip if it is
descFrame = myDoc.textFrames.add(); // Create the text frame
descFrame.contents = "W: "+objs.width + "pts\rH: " + objs.height + "pts"; // Adds contents to frame
descFrame.position = [objs.left,objs.top - objs.height - 5]; // Positions the frame
}
}
}
Seeing as I'm so new to the scripting process I need to get more information. I have saved the text I'm quoting into a text file with a .scpt extension. Then I run this in Illustrator under File>Script>Other Script. Currently all I get is a blank dialog box with an OK button. Can you help clue me into what i might do to get this to run.
Copy link to clipboard
Copied
The script provided is a cross-platform JavaScript. You will need to paste it into a text editing program (BBEdit, TextEdit or the like) and save it with a .jsx extension. Put that file in AICS3 (or CS4)>Presets>Scripts folder and restart AI. The script will appear in File>Scripts and can be selected from there. A couple of other points. If you want inches, just add a /72 behind the objs.width and objs.height. You might also want to add a space in front of the units in both places.
Copy link to clipboard
Copied
Larry G. That's not good news that I can't have the action work after restart. Perhaps I can use the Insert Menu Item… as a work around. Thanks for the heads up on this being a javascript and not a applescript, as you can see I don't know a script from a hole in the ground. I will try running it as a java.
Copy link to clipboard
Copied
It doesn't matter how you make the Action. It won't survive the restart. It's be the same since AI10 so don't expect is to change anytime soon.
Copy link to clipboard
Copied
Once again, excuse my cluelessness. Here is the syntax error I get when I run the script. I figure there is some line, probably line one which I need to edit to conform to my computer set-up.
Copy link to clipboard
Copied
From the Format menu in TextEdit, choose Make Plain Text and then save. Make sure you override the .txt and call it a.jsx file.
Copy link to clipboard
Copied
The script as written uses the geometric bounds of the items. If you want to use the visible bounds, you can do something like the following
myDoc = app.activeDocument;
findDims(myDoc.groupItems);
findDims(myDoc.pathItems);
function findDims(objs) {
for (i=objs.length-1; i>=0; i--) { //loop through your collection of objects
var vb = objs.visibleBounds; // left, top, right, bottom
var w = (vb[2] - vb[0]);
var h = (vb[1] - vb[3]);
if (objs.parent.constructor != GroupItem) { // Check if parent is a groupItem
descFrame = myDoc.textFrames.add(); // Create the text frame
descFrame.contents = "W: "+ w/72 + " in\rH: " + h/72 + " in"; // Adds contents to frame
descFrame.position = [objs.left, objs.top - objs.height - 5]; // Positions the frame
}
}
}
Copy link to clipboard
Copied
This script is giving me the dimensions of all the object in the document. Is there a way to edit this script so it only gives the geometric dimensions for the currently selected objects, in inches, with only four decimal places? I know, I'm asking alot more then the orignal question.
Copy link to clipboard
Copied
If you comment out the line
findDims(myDoc.pathItems)
with two slashes (//findDims(myDoc.pathItems)) it will only do the grouped items in the file. I haven't had any luck figuring out how to set it to the selection yet.
Copy link to clipboard
Copied
You should be able to target the currently selected items by changing the function's parameter.
findDims(app.selection);
If you want the width and height rounded to four decimals you can multiply your w and h values by 10000, use the round() method, and then divide the result by 10000.
var w = (vb[2] - vb[0]);
var w = Math.round(w*10000);
var w = w/10000;
or
var w = Math.round((vb[2]-vb[0])*10000)/10000;
var h = Math.round((vb[1]-vb[3])*10000)/10000;
Copy link to clipboard
Copied
I'm just getting back into this project and finding the current java script has the habbit of adding dimensions to all objects in the document and not just the ones selected. Any way to get this to run on the objects which are current selected or objects which are not hidden. The first script will spit out dimensions even for the items which are currently hidden.
Copy link to clipboard
Copied
Here is my current script which you told me to add finddim(app.selection); to and it worked to give the dimentions of only the selected group of items. I can't seem to get the four decimal lines to work. Not sure where they go or if they replace other lines. The attempts I have made to add the math round and devide lines has not yielded any results. I was also trying to figure out how the text is placed and if I can make the script place the resulting text to the top center of the selected objects as well as making the objects dimentions of the actual vectors and not the visiable object (vectors + stroke).
myDoc = app.activeDocument;
findDims(app.selection);
function findDims(objs) {
for (i=objs.length-1; i>=0; i--) { //loop through your collection of objects
var vb = objs.visibleBounds; // left, top, right, bottom
var w = (vb[2] - vb[0]);
var h = (vb[1] - vb[3]);
if (objs.parent.constructor != GroupItem) { // Check if parent is a groupItem
descFrame = myDoc.textFrames.add(); // Create the text frame
descFrame.contents = "W: "+ w/72 + " in\rH: " + h/72 + " in"; // Adds contents to frame
descFrame.position = [objs.left, objs.top - objs.height - 5]; // Positions the frame
}
}
}
Copy link to clipboard
Copied
Does this help
#target illustrator
if (app.documents.length > 0)
{
var idoc = app.activeDocument;
sel = idoc.selection;
if (sel.length==1)
{
//alert("selection = 2")
var vb = sel[0].visibleBounds; // left, top, right, bottom
var w = (vb[2] - vb[0])/72;
var h = (vb[1] - vb[3])/72;
descFrame = myDoc.textFrames.add(); // Create the text frame
descFrame.contents = "W: "+ w.toFixed (4) + " in\rH: " + h.toFixed (4) + " in"; // Adds contents to frame
descFrame.position = [sel[0].left, sel[0].top + 25]; // Positions the frame
}
}
Copy link to clipboard
Copied
Very nice, I had to tweek the start lines to get it to run. Now the dimensions appear as text at the upper left of the object/group of objects selected. The dimensions are still showing the appearance size and not the actual vector size, but now there're down to the four decimal places I was looking for. So I will review the previous scripts and see if I can figure out how the visibleBounds line might be changed to affect the size results I'm getting. What line relates to the placement of the text position? I'm guessing it's descFrame.position. How would I change this line to arrive at the text being centered and 1.125" away from the object? Here is what I ended up with after changing your last post…
#target illustrator
myDoc = app.activeDocument;
if (app.documents.length > 0)
 {
 
 var idoc = app.activeDocument;
 sel = idoc.selection;
 if (sel.length==1)
 {
 //alert("selection = 2")
 var vb = sel[0].visibleBounds; // left, top, right, bottom

 var w = (vb[2] - vb[0])/72;
 var h = (vb[1] - vb[3])/72;

 
descFrame = myDoc.textFrames.add(); // Create the text frame
descFrame.contents = "W: "+ w.toFixed (4) + " in\rH: " + h.toFixed (4) + " in"; // Adds contents to frame
descFrame.position = [sel[0].left, sel[0].top + 25]; // Positions the frame

}

}
Copy link to clipboard
Copied
I took the liberty to modify grizzlyburr script
#target Illustrator
// script by grizzlyburr?
myDoc = app.activeDocument;
findDims(app.selection);
function findDims(objs) {
for (i=objs.length-1; i>=0; i--) { //loop through your collection of objects
var vb = objs.geometricBounds; // left, top, right, bottom
var w = (vb[2] - vb[0]);
var h = (vb[1] - vb[3]);
if (objs.parent.typename != "GroupItem") { // Check if parent is a groupItem
descFrame = myDoc.textFrames.add(); // Create the text frame
var W = w/72;
var H = h/72;
W = W.toFixed(4);
H = H.toFixed(4);
descFrame.contents = "W: "+ W + " in\rH: " + H + " in"; // Adds contents to frame
descFrame.position = [objs.left, objs.top + descFrame.height + 5]; // Positions the frame
}
}
}
Copy link to clipboard
Copied
Carlos, yours seems to be working out the best so far. Have to leave for the day. hopefully I will be able to work on this tomorrow. Thanks all.
Copy link to clipboard
Copied
here you go
#target Illustrator
// script by grizzlyburr?
myDoc = app.activeDocument;
findDims(app.selection);
function findDims(objs) {
for (i=objs.length-1; i>=0; i--) { //loop through your collection of objects
var vb = objs.geometricBounds; // left, top, right, bottom // * changed to geometricBounds
var w = (vb[2] - vb[0]);
var h = (vb[1] - vb[3]);
if (objs.parent.typename != "GroupItem") { // Check if parent is a groupItem
descFrame = myDoc.textFrames.add(); // Create the text frame
var W = w/72; // *
var H = h/72; // *
W = W.toFixed(4); // *
H = H.toFixed(4); // *
descFrame.contents = "W: "+ W + " in\rH: " + H + " in"; // Adds contents to frame
var distanceFromTop = 1.125*72; // *
var x = objs.left+w/2-descFrame.width/2; // *
var y = objs.top + descFrame.height + distanceFromTop; // *
descFrame.position = [x,y]; // *
//descFrame.position = [objs.left, objs.top + descFrame.height + 1.125*72]; // Positions the frame
}
}
}
Copy link to clipboard
Copied
That did it. This worked Great.
Copy link to clipboard
Copied
I think I should rail on Adobe for not allowing me to use Java Script in an Action. This menu item in Illustrator is still broken, does anyone think Adobe will ever fix this known BUG?!? Any way to set up a keyboard shortcut in Illustrator to run Java Scripts? The only Keyboard shortcut I could find only allows you to navigate to a Script, and not to run a specific Script.
Anyway, thanks to all who contributed to this Script.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now