Sg2.9
Color all paths
Recent RSS
Color all paths
From:  Stef
Date:  26. April 2010, 23:06

Hi,

I've never scripted before and I've become completely stuck now so would appreciate your help.

What I'm trying to do should be really simple. I've got a document where I've traced an image so have paths of different stroke sizes (hence can't change all of them together) and I'm trying to color them all red.

Looking at these pages I've got as far as:

var allPaths = activeDocument.pathItems;

for (var i = 0; i < pathItems.length; i++)
{
var item = allPaths[i];
item.strokeColor = '#ff0000';
}

but this doesn't seem to work. Could anyone please show me the way? Thanks!

Re: Color all paths
Date:  27. April 2010, 14:46

To find all items in your document of a certain type you can use document.getItems(attributes):

var allPaths = document.getItems({
	type: Path
});

for (var i = 0; i < allPaths.length; i++) { 
	var item = allPaths[i];
	item.strokeColor = '#ff0000'; 
}
Re: Color all paths
From:  Stef
Date:  27. April 2010, 22:06

Thanks Jonathan. However now it just gets stuck on the first line, saying document is undefined. Is there anything else I should put before that code?

Thanks

Re: Color all paths
From:  Jürg Lehni
Date:  28. April 2010, 13:08

That sounds like you don't have an open document when you execute the code. The global document variable only returns a valid document if there actually is one.

Re: Color all paths
From:  Stef
Date:  30. April 2010, 00:41

There's definitely a document open when I run the code. As a test I created a new document, drew a few lines and ran the code from the ExtendedScript Toolkit and get the error. See attached screenshot. Thanks.

Re: Color all paths
Date:  30. April 2010, 16:30

I'm not entirely sure but you should probably write your script in an other script editor...

Re: Color all paths
From:  Stef
Date:  1. May 2010, 16:53

Thanks all for your help. By looking through the Illustrator Javascript reference I've found that this code works:

// Define the new color
var newRGBColor = new RGBColor();
newRGBColor.red = 255;
newRGBColor.green = 0;
newRGBColor.blue = 0;

//Color all paths
var allPaths = app.activeDocument.pathItems
 
for (var i = 0; i < allPaths.length; i++) { 
    var item = allPaths[i]; 
    item.strokeColor = newRGBColor; 
}
Re: Color all paths
From:  Jürg Lehni
Date:  1. May 2010, 17:06

Stef, you are using Adobe's ExtendScript engine. This site here is dedicated to the Scriptographer plugin that you can download here. They are two completely different things, and code written for one does not work on the other.

We will not be able to help with scripts for Adobe ExtendScript, but there are plenty of other sites that offer help, for example on the Adobe Forums.

But if you are curious to learn thing the Scriptographer way, you may want to start by looking at the Tutorials, and we are happy to answer your questions in this forum.

Re: Color all paths
From:  Stef
Date:  1. May 2010, 17:27

Hi Jürg, thanks for clarifying that, that explains it now. I obviously didn't look around the site enough before posting. However thanks for your help and I'll look into using scriptographer for any future scripting needs.

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 Rasterizer 02.03.14