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

resize method with Javascript in Illustrator CS4

New Here ,
Dec 13, 2010 Dec 13, 2010

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!

TOPICS
Scripting
4.1K
Translate
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
Adobe
Community Expert ,
Dec 13, 2010 Dec 13, 2010

Where are you running it from? If from the ESTK, make sure yhou have targeted Illustrator in the application dropdown above the script window.

Picture 1.png

Translate
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 ,
Dec 13, 2010 Dec 13, 2010

Yes, running it in ESTK and yes, targeting Illustrator. (This is just part of my script.)

Thanks.

Translate
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 ,
Dec 13, 2010 Dec 13, 2010

var endWidth = 20;

*just a typo when posting...

Translate
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
Guide ,
Dec 13, 2010 Dec 13, 2010

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

Translate
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 ,
Dec 13, 2010 Dec 13, 2010

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?

Translate
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
Guide ,
Dec 13, 2010 Dec 13, 2010

'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…

Translate
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 ,
Dec 13, 2010 Dec 13, 2010

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.

Translate
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
Guide ,
Dec 13, 2010 Dec 13, 2010

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…

Translate
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 ,
Dec 14, 2010 Dec 14, 2010
LATEST

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;

Translate
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