Finding multiple words and changing their text attributes: Illustrator

Hello, good evening, awesome.

First of all, much thanks and beers to the contributors in http://bbs.macscripter.net/viewtopic.php?id=13008.
It’s old, yeah, but it told me everything I had been clawing my eyes out over trying to make work for a solid day and a half. Having not programmed anything in well over 2 years now, and never having heard of Applescript before a few weeks ago, things have been a little confusing, but this site has helped far more than the Adobe forums ever have for anything.

Well, almost everything, because now that I’ve found a way to tear through lots of files changing multiple-word sets to other multiple-word sets, I want to change the attributes of those multiple-word sets.

So where I’m at now is looking at a slightly modified version of the example code in the Illustrator CS3 Applescript Reference that tells you a simple way of searching for a word and changing it’s colour. It’s not exactly what I want to do, but my problem is nearly identical to this, only in the script I’m writing I’ve got a few more loops and variables set that aren’t pertinent to the problem.

set searchString to "Many words" as Unicode text

tell application "Adobe Illustrator"
	
	set textArtItemCount to (count text frames in document 1)
	if (textArtItemCount > 0) then
		repeat with itemCounter from 1 to textArtItemCount
			if (((contents of text frame itemCounter of document 1) as Unicode text) contains searchString) then
				
				set the text font of (words of text frame itemCounter of document 1 whose contents = searchString) to "Interstate-Regular"
				
			end if
		end repeat
	end if
end tell

The error I get tells me that it can’t do what the instruction to change the fill colour is telling it to do, and that line is highlighted.

Now this code works if searchString is a single word, but logically something feels wrong with that line I’m getting the error on. I just don’t have nearly enough experience to figure out exactly what it is. I’m also still wrapping my head around delimiters, but from what I do know I don’t think they would help a lot in this case.

If anyone can explain to me what’s going wrong, they’d be much appreciated.

Model: PowerPC G5
AppleScript: 1.10.7
Browser: Firefox 2.0.0.14
Operating System: Mac OS X (10.4)

Not sure exactly what’s going on, but here are some clues:

  1. This works:
set the text font of (every word of text frame itemCounter of document 1) to text font "MyriadPro-Cond"

(I changed the font because I don’t have Interstate). Note that you have to add “text font” before the font name or Illustrator errors “Object expected.”

  1. Look what happens when you do this:
 if (((contents of text frame itemCounter of document 1) as Unicode text) contains searchString) then
				
				
				get (words of text frame itemCounter of document 1 whose contents = searchString)
			end if

You get {} as a result. So Illustrator is trying to change the font of nothing.

Looks like the problem is in the “every.whose” construction. But I don’t know how to fix it at the moment, never having dealt with scripting AI’s Find/Replace before now.

Hi

The Bad news in my opinion is i don’t think this can be done. (I could be wrong!)
just spent an hour tinkering with this and had various results none of them really good.
I struggled to convert the font on one word let alone two.
Here’s the little snippet i got the best results with seemed a bit buggy!

tell application "Adobe Illustrator"
	activate
	tell document 1
		set t to words of text frame 1 whose contents is "Many"
	end tell
	set g to result
	repeat with i in g
		set the text font of text of i to text font "Times-Italic"
	end repeat
end tell

Sometimes it just changed the first 3 characters then sometimes it did them all go figure!! :confused:
sorry can’t be of more help.

Good Luck

GAPS:

Just an FYI, your multiple word search fails because no single word can contain multiple words!

You say, “.words of text frame” which equates to every single word. So you’re essentially saying ‘tell me which word contains 2 words’ (“Many words”) which is, of course, impossible (validated by misterfriendly’s {}).

Jim Neumann
BLUEFROG

Thanks for the fast relies, guys!

misterfriendly: I haven’t got quite the right setup to test this here at home yet, but yes, I did indeed neglect extra “text font”. And cheers for reminding me of “get” for testing stuff… before now I had been pulling results into variables and display dialoging the variables. It really has been a while for me. If you read the statement logically, it makes perfect sense that that is what you would end up with… I just don’t know how to fix it.

pidge1: heh! “I don’t think this can be done”… I’m not willing to give up hope yet. But I don’t know… perhaps I need to turn to javascript? I have nThe code I posted works if you just have one word (as long as you put the extra “text font” in), since that is pretty much the structure Adobe provide in the reference book. Thanks for trying, though.

BLUEFROG: Yeah, after reading up on “word” in a couple reference guides, it was clear to me that might be where the problem lay. Except, everything else seems to have the same problem of defining something too specific”ie. character, text, text item, etc. So just changing that isn’t going to cut it, I figure.
So I considered using text item delimiters the same way one does in find/replace, but instead of replacing, just selecting the text, and applying a change to the selection. But as I understand it, in Illustrator selected text is read only, and so I couldn’t even get select commands to have any impact even in this script, let alone inside a more complex delimiters-based script.

And so I’m at a bit of a loss…