Saturday, January 07, 2012

Set ALL the Things


Ironically, the use case for this script is very specific, but, read on...
I wanted a script that would allow me to set a bunch of  differently sized objects to an exact width.


In illustrator it is easy to scale multiple objects at once, but if they are already placed, and I just want to shrink them to an exact size... I draw a blank.

So I wrote this to fix my problem, but while I was at it, I thought it might be fun to make it a little more flexible.  You can use this script to bulk re-size things to specific dimensions. Or, if you're feeling a little more adventurous...

For more advanced (or masochistic) users:
You can also pass functions as values, for instance, assuming you entered:

 fillColor

as your attribute,
The following value will set color of all selected objects to red:

(function(){var c=new RGBColor();c.red=255;c.green=0;c.blue=30;return c;})();

Not super exciting, I know, but it's a start.
Another slightly more interesting example:

For attributes enter:

fillColor,strokeColor


for values enter:

(function(){var c=new RGBColor(); var r = function(n){return Math.floor(Math.random()*n);} c.red=r(255);c.green=r(255);c.blue=r(255);return c;})();

The above will assign a random color to both the fill and stroke of each selected object. (assuming the object had a stroke to begin with.)

Let's try an even more complex example:
To set stroke, then set random stroke thicknessess and a random stroke color, do this:

 attributes:

 stroked,strokeWidth,strokeColor


 values:

true,(function(){return Math.random()*20;})(),(function(){var c=new RGBColor(); var r = function(n){return Math.floor(Math.random()*n);} c.red=r(255);c.green=r(255);c.blue=r(255);return c;})()
 

Granted, this is super messy, very error prone, and way overkill. If you want to do something this advanced, and you have the skills to write function closures in a prompt, you could easily just write your own custom script, but for quick 'one-off' tasks that don't need a script, maybe this 'advanced use' will come in handy for someone. If not, then maybe it's a good learning exercise.

Simple, or complex, let me know how you use it in the comments.  Just remember, the advanced stuff is not required, the script just defaults to 'width,height', so you don't have to do anything complicated to use it.

Update: Script now takes any standard units, so '20mm' and '6ft' are valid values.
Script now also gives you the option to transform objects from their center, rather than defaulting to top,left.

12 comments:

Jode said...

Is there a simple way to use units other than px for the dimensions?

Also, can the resized object be anchored by their center rather than the upper left corner?

John said...

Hello Jode,

Good questions Jode, Thanks for commenting.

Illustrators JS engine uses px regardless of what the document settings are. A quick workaround is to just enter math formulas instead of values.

If you wanted your objects set to a width of 3 inches, at the value prompt, you could just enter 3*72, because(in illustrator, by default) 1 inch == 72ppi.

If you want to know illustrators pixel value for mm, you can just make a 1x1mm rectangle on the stage, and then switch units (cntrl-alt-shift-U)
until pixels are default, and see what that value is. For convenience, it's 2.834646. So, 3mm is 3px * 2.834646. Note this is not an exact conversion, so you may still be a pixel off if you are making really large objects.

In regards to your second question, Illustrator has awesome tools for re-positioning objects, so I didn't feel the need to add it to the script. If you think it would be beneficial, email me a use case, and I'll consider adding it to v2.
cheers,
-J

John said...

OK Jode,

I had some time today, and added both requests to the code.
cheers,
-J

Anonymous said...

hello,

This script is really great et so simple to use.
I just can't find a way to force exemple change the height and keep the proportion of all the elements.

Is there a way?

David

John said...

Brilliant idea. That use case didn't occur to me. I'll either post an example incorporate that as an option in the next release, or post an example of an inline function to do the trick here in the comments.

Thanks for commenting!
-J

John said...

David,
I have reviewed the code and can see no easy way to implement this without hosing existing flexibility.
I will keep looking for an acceptable solution. Can you please email me and let me know what your use case is? It might be worth making into a separate, albeit less flexible script. Thanks
-J

Anonymous said...

Hello,

Thanks for looking :D.
The script already same me a lot of time.

David

Jode said...

John,

This is great - thanks! In the past I've been using Transform Each and busting out the calculator to get the ratio, but this will be much simpler. Since I'm usually working with shapes that will be cut with a laser, I almost always work in physical units, so this is very convenient, too. From my Googling for an alternative to Transform Each, I think many others will be pretty happy with your script as well. Thanks again!

Cristofer Cruz said...

Hi John,

Downloaded this script yesterday and already has a use for it today. I noticed it doesn't support decimals, would be a good addition. I added decimal support myself since I needed it.

Thanks

John said...

Hello Cristofer,
Thanks for posting. Please email me at the address on the top of the script. I've tested the code on my end, and it properly supports decimals for me. How are you using the script, such that decimals aren't working for you?

onirix said...

I was looking like yours for so long !

Thank you so much !!!

kali brayfield said...

Thanks so much resized 500 cards in .2 seconds! You rock!!