Sg2.9
Working with Many Items at Once
Recent RSS
Working with Many Items at Once
From:  Chris Malven
Date:  5. October 2010, 00:28

Anybody have any suggestions on working with LOTS of Illustrator items at once? It seems like even the simplest scripts take many minutes to execute when working with hundreds of objects. Take the following, for instance:

if(document.selectedItems.length > 1) {

var rotationAmount = (90).toRadians();

for (var i = 0; i 0.5)
{
guy.rotate(rotationAmount);
}
}

} else {
Dialog.alert('Please select more than one item');
}

Anything I could be doing more efficiently here? Thanks!

Re: Working with Many Items at Once
Date:  6. October 2010, 10:09

You could try passing the document.selectedItems value to a variable. This way, it caches the value - when you use document.selectedItems directly, you're asking Illustrator to look in the document and find the selected items each time.

var selectedItems = document.selectedItems;

if(document.selectedItems.length > 1) {

	var rotationAmount = 90;

	for (var i = 0; i < selectedItems.length; i++) {
		var item = selectedItems[i];

		if (Math.random() > 0.5)  {
			item.rotate(rotationAmount);
		}
	}

} else {
	Dialog.alert('Please select more than one item');
}

Also, I changed rotationAmount to use degrees now - since in the new version of Scriptographer we use degrees for angles.

Does it run faster now?

Scripts
08.08.14, 15:24
15.05.14, 14:23
02.03.14, 19:16
18.11.13, 14:48
22.03.13, 03:05
22.02.13, 15:45
Posts
10.01.17, 16:37
19.02.16, 06:03
19.02.16, 06:00
17.01.16, 11:00
12.01.16, 13:10
25.11.15, 08:19
Script of the Moment
Hexagonal Grid Generator 16.11.11