Change a partcular stroke weight

Is there a way to a have a script search all the stroke weights in an Illustrator CS document, and then only change those strokes that equal a particular weight?

For example:
Change all the active document path items whose strokes equalling .25 to strokes equalling .75.

Sounds like it’s doable - basically - you’ll want to set a variable to all page items that have that particular stroke weight. Once you’ve got that - it’s just a matter of setting the stroke to what you want.

ie:

tell application "Adobe InDesign CS2"
	tell document 1
		set x to (every rectangle whose stroke weight > 1)
		repeat with i in x
			set stroke weight of i to 10
		end repeat
	end tell
end tell

This is with InDesign - but I’m sure the same basic concept applies to Illustrator.

Chris,
Your script is very close”thank you. It works if I just use a <, but that increases the strokes of all those non stroked items. For some reason the script doesn’t allow me to set a stroke “equal” to a particular value. Even though I know .216 is the stroke size I want to change throughout my document?
So, I thought I could put in a range, but that isn’t working either? (I am receiving an error)
Can anyone help me target that specific value of “.216” and only have those strokes changed?

I would really appreciate any help on this.

Thanks,
jeff

tell application "Illustrator CS"
	tell document 1
		set x to (every path item whose stroke width < 0.217 and every path item whose stroke width > 0.215)
		repeat with i in x
			set stroke width of i to 0.7
		end repeat
	end tell
end tell

Hey Jeff,

Sometimes AS is confused by compound filter clauses, I’ve noticed that not every application supports even the “whose” filter at all! Here’s a work-around, it tests each member of x. Not as elegant, but gets the job done:

tell application “Illustrator CS”
activate
tell document 1
set x to (every path item whose stroke width < 0.217)
repeat with i in x
if (stroke width of i > 0.215) then
set stroke width of i to 0.7
end if
end repeat
end tell
end tell

Ted

This is Perfect!
Thank you very much. Now, I did realize one flaw in my theory. I need to negate all paths that have a fill color. Could such a parameter be added to the the path item?
such as set x to (every path item whose stroke width <0.217 and whose fill color is none)? I haven’t checked my definitions yet. But I was just wondering if it could even be done?

Once again. Thank you so much for helping me out with this.

jeff

Just as an fyi - the reason you’re not able to identify all the path items with a stroke width of .216 is probably becasue there are no items that have that stroke width. Allow me to explain:

draw an item and set the stroke to .216 manually

then - run this applescript. Note the result - its NOT .216…

tell application "Adobe Illustrator"
	tell document 1
		get stroke width of every path item
	end tell
end tell
--result {0.216000005603}

You’re probably figuring this out - but Applescript and Adobe Apps is just plain weird…actually - I don’t blame AppleScript…blame Adobe

anyway - here’s the fill color modification to Ted’s script:

tell application "Adobe Illustrator"
	activate
	tell document 1
		set X to (every path item whose stroke width < 0.217)
		repeat with i in X
			if (stroke width of i > 0.215) then
				if filled of i is false then
					set stroke width of i to 0.7
				end if
			end if
		end repeat
	end tell
end tell

Thank you Chris.
This is fantastic. And thank you for sharing your knowledge.
Believe it or not, I realized that catch when getting help from someone using javascript and throwing in some alerts. It wasn’t the intention, but when I saw that value of .216000. it all made sense. I really can’t blame Adobe though. I am simply glad one can tap into their programs via scripts and do as much as one can.

I can’t lose this script, I am sure it will come in handy again in the future as a subset of an additional script, etc.

Thanks again,

jeff

You can, BTW, combined all those qualifications into one line.

every path item whose stroke width > 0.215 and stroke width < 0.217 and filled is false

That doesn’t work Joseph - see the beggining of the thread - there’s something weird that goes on with strokes that are decimals in Illustrator. As far as I can tell - the way Ted is doing it is the only way it works.

OK, I just have to jump in here… not only is this possible but it’s an easy one liner as well.


tell application "Illustrator CS" to set properties of every path item of document 1 whose (stroke width < 0.217 or stroke width > 0.215 and filled is false) to {stroke width:0.7}

(You may want to break it out of a one liner and drop in a try block for production’s use. I was just showing off by putting it in one line!:lol:)

A few thoughts about the scripts so far and the one I’ve posted…

#1: If you have a page with several hundred (or thousand objects) on it, “walking the document” (running a repeat loop) can leave you staring at the beach ball for a loong time. With a test document of 1872 rectangles (varying from .216 to .218 in stroke width) the current script being discussed took 61 seconds to finish. The one liner I posted took 5 seconds!

#2: You can change properties of objects globally using this syntax but the order in which you specify them is critical. If you set the properties of objects to be {stroke width: 2.0, stroked:true}, any unstroked objects will have the default stroke applied to them (usually 1pt White). If you want strokes applied to the unstroked objects you must declare the stroke first, then the stroke width. {stroked:true, stroke width:2.0} Otherwise, you’re telling Illustrator to apply a stroke width to a nonexistent property (the stroke) which it won’t understand, but it will understand the {stroked:true} and add a stroke. Since there was no stroke width decalaration after this, it applies the default width.

#3: Whenever possible, I like to break a document down into groups that can be quickly changed by use of the “properties method”. Walking the document may seem OK on simple jobs but I firmly believe in coding for the worst. I even code on a slower G4 so when my scripts hit the faster machines in my department they run like a rocket because they’ve been optimized to run in a less capable environment.

Anyways, sorry to butt in but I see many people fall into the trap of repeat loops and we all know how quickly Illustrator files become very complex. (They never look like there’s that many objects!)

Cheers,
Jim Neumann
BLUEFROG

Thank you Jim,
Even though I need to read your email a few more times to totally grasp what you are talking about :slight_smile:
btw, please don’t ever apologize for butting in. Your input is always welcome and preferred, at least by me it is. After all, it is the only way people like myself can learn this stuff. Thanks for sharing the knowledge.

Btw, I did change an “or” to an “and” in your one line script. It works very nicely.

tell application "Illustrator CS" to set properties of every path item of document 1 whose (stroke width < 0.217 and stroke width > 0.215 and filled is false) to {stroke width:0.7}

Thanks,
jeff

Well damn BlueFrog - we’re you just waiting for us to flounder here? haha - good work…I agree repeat loops are S L O W…I just couldn’t get it to work without one. I bow to your higher knowledge!

And I accept your worship with the sacrifice of a beautiful virgin, Chris :smiley:

I am at that point in my scripting where speed and brevity is EVERYTHING. Most of what I am working on is high end Electronic Prepress that needs to cover a lot of bases and be extremely nimble and flexible. In fact most of what I am working on appears to be uncharted territory as far as Illustrator is concerned! (I actually started teaching myself to read hex for one project!)

Glad I could be of service to anyone who gets something out of my rambling post.

Best Regards,
Jim Neumann
BLUEFROG