resize method with Javascript in Illustrator CS4
Copy link to clipboard
Copied
var objectBounds = docRef.visibleBounds;
var W = objectBounds[2] - objectBounds[0]; //figures the width
var H = objectBounds[1] - objectBounds[3]; //figures the height
// W & H are reporting the size in points ... convert to picas
var picaW = W / 12;
var picaH = H / 12;
// do the math for the correct column width: settings / width = percentage in decimal; multiply by 100 to get the decimal place in the right spot.
var endWidth
var percentageX = Math.round((endWidth / picaW) * 100);
var percentageY = percentageX;
for (var a=0; a<docRef.layers.length; a++){
var layerRef = docRef.layers;
layerRef.hasSelectedArtwork=true; // select all
// now to resize based on the percentage
layerRef.pageItem.resize(percentageX, percentageY);
}
The width and height are coming out fine. But in this last line, that resize is just not working. When I run it I get an error that says: undefined is not an object.
The syntax must be wrong ... any ideas?
Thanks in advance!
Explore related tutorials & articles
Copy link to clipboard
Copied
Where are you running it from? If from the ESTK, make sure yhou have targeted Illustrator in the application dropdown above the script window.
Copy link to clipboard
Copied
Yes, running it in ESTK and yes, targeting Illustrator. (This is just part of my script.)
Thanks.
Copy link to clipboard
Copied
var endWidth = 20;
*just a typo when posting...
Copy link to clipboard
Copied
Im not sure I understand what you are doing here. I think if you are going to select items then you should process the document selection. (Im no fan of selecting in script). Else just target your items without selecting. Are you hoping 'pageItem' is going to resize all the layer objects? Or is that a typo and should be 'pageItems[0]' plural… by index
Copy link to clipboard
Copied
Basically I just need to resize an image. If selecting isn't the way to go then that's fine, I'm just trying things...
Do you know how to use .resize?
Copy link to clipboard
Copied
'pageItems' is a collection of all the different types of objects on your layer. There is NO need to select objects in script in order to work with them you just target them by index or getByName() etc. then pass the object to resize();
Using 'pageItems[0].resize();' will get you the first item on that layer or you may be able to use 'placedItems[0].resize();' pass your % as arguments to either should work…
Copy link to clipboard
Copied
Ok, now I'm getting somewhere! It is resizing and its using the percentage. But now the problem is that things aren't staying in the same location proportional to where they were. Its as if the reference point is centered on each object instead of the image as a whole.
for (var s=0; s<layerRef.pageItems.length; s++){
layerRef.pageItems.resize(percentageX, percentageY);
}
This for loop is making sure all the pageItems are being accounted for. I took out the 'select all' line.
Copy link to clipboard
Copied
This is correct when using script… If you require scaling of all a layers content at once relative to the one location you need to move all the items into a temporary group… scale the single group object then ungroup it… You should find examples here in previous posts…
Copy link to clipboard
Copied
Great. Grouping was exactly what I needed to do. This works perfectly now (unless there are a few clipping masks already in the image, then it gets a little weird). Here's what I ended up with:
var objectBounds = docRef.visibleBounds;
var W = objectBounds[2] - objectBounds[0]; // figures the width
var H = objectBounds[1] - objectBounds[3]; // figures the height
// W & H are reporting the size in points ... convert to picas
var picaW = W / 12;
var picaH = H / 12;
// do the math for the correct column width: settings / width = percentage in decimal; multiply by 100 to get the decimal place in the right spot.
var endWidth = 20;
var percentageX = Math.round((endWidth / picaW) * 100);
var percentageY = percentageX;
// creates group on a new layer and omits guides
var theLayer = docRef.layers.add();
theLayer.move(docRef, ElementPlacement.PLACEATBEGINNING );
var theGroup = docRef.groupItems.add();
theGroup.name = "theGroup";
theGroup.move(theLayer, ElementPlacement.PLACEATBEGINNING );
for (var b = docRef.layers.length - 1; b > 0; b--) {
if (docRef.layers.visible == true) {
docRef.layers.locked = false;
// move the items into the group if they are not guides;
for (var a = docRef.layers.pageItems.length - 1; a >= 0; a--) {
if (docRef.layers.pageItems.guides != true) {
docRef.layers.pageItems.locked = false;
docRef.layers.pageItems.move(theGroup, ElementPlacement.PLACEATBEGINNING)
}
}
}
}
// now to resize based on the percentage
for (var s=0; s<docRef.groupItems.length; s++){
docRef.groupItems.resize(percentageX, percentageY);
}
var newPicaW = (objectBounds[2] - objectBounds[0]) / 12;
var newPicaH = (objectBounds[1] - objectBounds[3])/ 12;

