Replace Text in Illustrator

Hi Folks-

I’m attempting to replace a selected string in a text box in Illustrator CS2. I have figured out how to cut text out but not paste new text in. I am guessing the steps must be selecting an insertion point, and then pasting.

Here’s the code I have so far:

set selection to characters FirstChar thru LastChar of text frame current_text_frame of document 1 --sets selection to a range previously calculated

delete the selection of text frame current_text_frame of document 1

thanks!

Ralph

if you are simply replacing the selection why don’t you just set the selection to the replacement text like this


tell application "Adobe Illustrator"
	set selection to characters FirstChar thru LastChar of text frame current_text_frame of document 1 --sets selection to a range previously calculated
	set contents of selection to Replacement_Test
end tell

Edit: lets not select anything there is no need


tell application "Adobe Illustrator"
	set contents of  characters FirstChar thru LastChar of text frame current_text_frame of document 1 to Replacement_Test
end tell


Hi Ralph

Just point to the text frame in your script and change its contents:

tell application "Adobe Illustrator"
	set t to "hello"
	set contents of text frame 1 of document 1 to t
end tell

work, and the script now work, except in the case of a date replacement with a “/”- as in “12/31/06” (which is what I built this for :-).
I code the enter/replace strings as:

set temp to display dialog “Enter the string to be FOUND:” default answer “”
set FindText to text returned of temp as string
set temp to display dialog “Enter the string to INSERT:” default answer “”
set RepText to text returned of temp as string

and

tell application “Adobe Illustrator”
set contents of characters FirstChar thru LastChar of text frame current_text_frame of document 1 to RepText

end tell

any ideas?

thanks,

Ralph

I’m not sure what the problem is but this is i nice bit of string replacement works well if you want to replace the same word in multiple boxes


set temp to display dialog "Enter the string to be FOUND:" default answer ""
set FindText to text returned of temp as string
set temp to display dialog "Enter the string to INSERT:" default answer ""
set RepText to text returned of temp as string
tell application "Adobe Illustrator"
	set (contents of words of every line of every text frame of document 1 whose contents contains FindText) to RepText
end tell

not too sure what you mean here ralph.
Does your date look like this just for example (“12.31.06”)
and you want to change it to ths (“12/31/06”)

illustrator and text is a strange combination you would be best creating your date string how you want it to be outside the illustrator script, and then use one of the above mentioned ways of placing that content back in, otherwise you will be messing about with illustrators insertion points
theres loads of info on the forum about creating different sorts date string!!

Figured it out- Applescript sees a “/” as a text item delimiter, and so it was seeing 2/22/02 as ONE word when counting in Illustrator, but THREE when I entered it as a string in Applescript. By creating a word counter that bypassed Applescript’s native counter, checking for " " spaces, it’s possible to count actual words and ignore Applescript’s default delimiters, “,” and “/”

on CountWords(text_to_count)
set WordCounter to 1

if (text_to_count ≠ "") then
	set number_of_chars to count text_to_count
	set counter to 1
	
	repeat while (counter ≤ number_of_chars)
		if (character counter of text_to_count = " ") then
			set WordCounter to WordCounter + 1
		end if
		set counter to counter + 1
	end repeat
else
	set WordCounter to 0
end if
return WordCounter

end CountWords

if anyone’s interested, I’ll post the entire code.

thanks,

Ralph

Ralph,

what is it you are trying to accomplish I mean what is the ultimate goal… I don’t see how word counts have anything to do with dates. the code I posted can replace single words in any text anywhere on the layout you can narrow it down from there.

mm

what i was trying to do is replace character strings in illustrator.

these strings are usually dates, in the format of 12/12/01

applescript’s text item delimiter considered “12/12/01” to be three words, not one. i forced applescript to count only spaces as delimiters instead of using its native word counting. Below is the final, working script:

– This script finds all occurrences of a string and replaces them.

on CountWordsInFrame(text_frame)
activate application “Adobe Illustrator”
tell application “Adobe Illustrator”
set number_of_words to count words of text frame text_frame of document 1
end tell
return number_of_words
end CountWordsInFrame

on CountWords(text_to_count)
set WordCounter to 1

if (text_to_count ≠ "") then
	set number_of_chars to count text_to_count
	set counter to 1
	
	repeat while (counter ≤ number_of_chars)
		if (character counter of text_to_count = " ") then
			set WordCounter to WordCounter + 1
		end if
		set counter to counter + 1
	end repeat
else
	set WordCounter to 0
end if
return WordCounter

end CountWords

– get strings to be found and inserted from user input
set temp to display dialog “Enter the string to be FOUND:” default answer “”
set FindText to text returned of temp
set temp to display dialog “Enter the string to INSERT:” default answer “”
set RepText to text returned of temp

–make sure there is at least 1 word to find before executing rest of script
if (FindText ≠“”) then

set num_of_words_to_find to CountWords(FindText) --number of words in the string to be found
set FoundIt to false --initialize variable, assume no match found

tell application "Adobe Illustrator"
	activate
	set strings_changed to 0
	
	--make sure there is a text art in the document. if so, then repeat for each text art object
	set num_of_text_frames to count text frames of document 1
	if num_of_text_frames > 0 then
		repeat with current_text_frame from 1 to num_of_text_frames
			
			set current_word to 1
			set num_of_words_in_text_frame to my CountWordsInFrame(current_text_frame)
			
			--repeat as long as there are still words in the text art to evaluate and the 1st word to be found occurs in current text art
			repeat while ((current_word ≤ (num_of_words_in_text_frame - num_of_words_to_find + 1)) and ¬
				(contents of the text of the story of text frame current_text_frame of document 1) contains FindText)
				-- if first word matches, compare remaining words to be found
				if contents of word current_word of text frame current_text_frame of document 1 = FindText then
					set FoundIt to true
					set LoopVar to 1
					repeat while ((LoopVar < (num_of_words_to_find)) and FoundIt)
						if (contents of word (current_word + LoopVar) of text frame current_text_frame) ≠ (contents of word (1 + LoopVar) of FindText) then
							set FoundIt to false --if at any time one of the words doesn't match, there is no match
						end if
						set LoopVar to LoopVar + 1
					end repeat
					
					--if string was found, find 1st and last character of string, delete and replace string
					if FoundIt then
						set FirstChar to character offset of 1st character of word current_word of text frame current_text_frame of document 1
						set LastChar to character offset of last character of word (current_word + num_of_words_to_find - 1) of text frame current_text_frame of document 1
						set contents of characters FirstChar thru LastChar of text frame current_text_frame of document 1 to RepText
					end if
				end if
				
				set FoundIt to false --initialize variable for next iteration through loop
				set current_word to current_word + 1
			end repeat -- within text art
		end repeat --for each text art
		save current document
		close current document
	end if
end tell

end if

Ralph

you said your script does this…

that you wrote a huge block of code for but my previous post does the same thing in 10 lines and could probably be further condesned here it is again …


set temp to display dialog "Enter the string to be FOUND:" default answer ""
set FindText to text returned of temp as string
set temp to display dialog "Enter the string to INSERT:" default answer ""
set RepText to text returned of temp as string
tell application "Adobe Illustrator"
   set (contents of words of every line of every text frame of document 1 whose contents contains FindText) to RepText
end tell

the only way that code would not work would be if someone put multiple words in the display dialog box then it would look for that entire string to replace
mm

wow. that’s pretty amazing. I pulled the code I used from adobe’s ‘extras’ disk that came with the adobe creative suite, and added code here and there.

as an applescript (and programming) newbie I have no idea what’s possible and not possible. sometimes the syntax seems limiting, otherwise (as in your line 6) simply brilliant.

thank you for your patient input!

-Ralph

Your welcome I am glad I could help :smiley:

For anyone who’s interested- here’s the completed script. Put this on your desktop and drag eps files onto it, it will ask you for a string to find and replace, then replace and save back to the original location.

on open of vdropList

set temp to display dialog "Enter the string to be FOUND:" default answer ""
set FindText to text returned of temp as string
set temp to display dialog "Enter the string to INSERT:" default answer ""
set RepText to text returned of temp as string
activate application "Adobe Illustrator"
repeat with vtheFile in vdropList --repeats with each item in the droplist
	tell application "Adobe Illustrator"
		open vtheFile
		try
			set (contents of words of every line of every text frame of document 1 whose contents contains FindText) to RepText
			save document 1 as eps
		end try
		close document 1
	end tell
end repeat

end open