Copy link to clipboard
Copied
Hi
Firstly, I am a noob when it comes to scripting.
I would like to be able to automate several search and replace text commands in an open text document. I have tried recording find and replace actions but the action simple repeats the same search each time.
I wish to automate the replacement of a single space with a double space. Also replace x with space x space (make sense?).
I thought that this would be a simple case of modifying an existing script but I can find nothing for find and replace.
Is this something a noob could create with relative ease?
Copy link to clipboard
Copied
not really
important is the Illustrator Text Object-model, which is not very easy to understand.
And the Core JS String and regExp Objekts.
Have a look at this:
http://www.ogebab.de/home/skripte/text/replace
The dialog is poor (because of compatibility) and it works simply on TextFrames.contents, i don't know if this is good i all cases.
But i use it not very often so i don't want to rewrite it.
Maybe it is a starting-point for you.
But maybe it is more then you need.
try this:
//select a text
var text = app.activeDocument.selection[0];
var string = text.contents;
string = string.replace(/ /g," ");
text.contents= string;
/ /g is a regular expression
.replace() is a method of the String Object
both explained at
https://developer.mozilla.org/en/JavaScript/Reference
Copy link to clipboard
Copied
The simple script you posted in the thread is looking like what I need.
I have copied and pasted the code several times in the script so that I can perform a list of changes (took me a while to realise I had to change variable count...I am that much of a noob).
Any idea what i can use to force a line break? I have tried "\n" and "\^" but neither appear to work.
Thanks for your awesome help.
Copy link to clipboard
Copied
thats one of the funny things in Illustrator and the Text model.
contents seems to be not really a string?
you have to split the String (read fom contents) with JScore "things".
(depending on what you want, this can get really frustrating whitout knowledge of rexExp.)
then add paragraphs to the TextFrame.
a simple example:
textframe:
this is a simple test string
var text = app.activeDocument.selection[0];
var string = text.contents;
string = string.replace(/simple/g,"simple xxx");
string = string.split("xxx");// string is now an Array of 2, xxx is removed
text.paragraphs.removeAll();//wipe out
for(var i=0;i<string.length;i++){
text.paragraphs.add(string);//write new
}
this is a simple
test string
Copy link to clipboard
Copied
Well that's a litle more complicated than I was expecting. I think I will just insert a xxx where I need the break and then manually do a find and replace in Illustrator for that one.
One other (last) point. The script works if I have one text box selected, but not if I have them all selected. Is it possible to make it work on multiple selections?
Copy link to clipboard
Copied
try using "\r" instead of \n for line breaks
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more