Skip to main content
DuanesSearchForKnowledge
Inspiring
April 9, 2013
Answered

deleting text from text frame

  • April 9, 2013
  • 2 replies
  • 4139 views

my question is does anyone now the best way or anyway to remove text from a text frame?

say if you want to remove anything in () in a text frame or any * symbols.

52(one on both sides) //you have this in a text frame

52 // and you want to change it to this by deleting (.)

or

40* //you have this in a text frame

40 // and you want to change it to this by deleting the *

any help would be great.

This topic has been closed for replies.
Correct answer CarlosCanto

Eureka! Thanks Carlos! I'm going to have to wrap my head around that.

now to get rid of () and everything in them I taught this would work;

myText2 = myText2.replace(/\(\.\)/, '')

I thought a period stood for anything or all. not sure about that now.

I was trying to say (all), replace with nothing.


this should do

myText2 = myText2.replace(/\((.+)\)/, '');

2 replies

pixxxelschubser
Community Expert
Community Expert
April 9, 2013

Try this:

var aDoc = app.activeDocument;

var aTFrame = aDoc.textFrames;

var aString = "50*";

for (i=aTFrame.length-1; i>0; i--) {

    contentString = aTFrame.contents;

    if (contentString.indexOf(aString) != -1) { //if 50* present in the text frame

        //aTFrame.selected = true;

        //alert (contentString);

        aTFrame.contents = contentString.replace (/50\*/g, '50');

        }

    }

CarlosCanto
Community Expert
Community Expert
April 9, 2013

use the replace method, for example to remove all asterisks

var string = '*52* 53 54 *55';

var newstring = string.replace (/\*/g, '');

alert(newstring);

homework topics

- string.replace(what, with)

- javascript regular expressions (/\*/g) (or RegExp Object)

DuanesSearchForKnowledge
Inspiring
April 9, 2013

Thanks Carlos and P.S. I have been able to change the string but for some reason I cant get it to actually change the textframe in the document.

Below is what I had before. TF 0 is 50* and when you read the alerts the first says 50* and the last one says 50, but the text frame in the file does not change. Do you know how to get it to change the text frame?

var myDoc = app.activeDocument;

var myText = myDoc.textFrames[0].contents;

alert(myText);

myText = myText.replace("*", '')

alert(myText);

CarlosCanto
Community Expert
Community Expert
April 9, 2013

now you need to put that string back to the text frame

myDoc.textFrames[0].contents = myText;