Copy link to clipboard
Copied
Can anyone point me in the righ direction in the api reference as to how i capture a text string?
I have a text string like so "<b> my text <b>"
I want to be able to capture the text between the two '<b>' and change it's style to bold.
Copy link to clipboard
Copied
var myString = "<b>my text<b>";
var mysubString = myString.slice(3,myString.length-3);
alert(mysubString);
alert(myString);
Copy link to clipboard
Copied
Thanks Carlos thats a great help i have not used the 'slice' before.
I have another question, how would i capture the text string between the two "<b>" if it is variable?
In other words any text found between two "<b>" anywhere in my document is to be selected and font style changed to bold?
Copy link to clipboard
Copied
same way, same code. I'll work regardless of the string, regardless of the tag <?>, it is based on character count.
slice is saying "give me a string starting at the 4th character, stop after the 4th character before the end"
now if you want to evaluate for <b> before you do something with it, slice the first 3 character to see if it is equal to "<b>" then do your thing accordingly.
Copy link to clipboard
Copied
So would i be correct in assuming that i need to do something like this to capture anything between the first 3 and the last 3 characters?
string = text.contents
var myString = "<b>string<b>";
etc
Copy link to clipboard
Copied
something more like this
var idoc = app.activeDocument;
var text = idoc.textFrames[0];
var myString = text.contents; // "<b>my text<b>";
var mysubString = myString.slice(3,myString.length-3);
alert(mysubString);
alert(myString);
Copy link to clipboard
Copied
Thanks again CarlosCanto,
I think i am not making much sense as your sample captures everything in the text frame! I really only want what is between the two "<B>"s.
I think i may need ot look at this differently, maybe capture text between 1st and 2nd "<B>", then loop through job and capture 3rd and 4th pair and so on.
Thanks for your help.
Copy link to clipboard
Copied
I don't understand
alert(myString); // returns everything in the thextFrame --> "<b>my text<b>"
alert(mysubString); // returns only the text between the <b> tags --> "my text"
Copy link to clipboard
Copied
Where is this text coming from it looks like XML of which I know very little (well almost nothing actually)… But the ESTK does have an XML object that will allow better handling of this…
var mystring = '<b> my text <b>'; var myXML = new XML(mystring); alert(myXML.elements().length()); alert(myXML.child(0).toString());
Copy link to clipboard
Copied
Sorry MM i was busy replying to CC.
You are correct i am modifying xml data using a combination of applescript and javascript. Cracking this part of the javasscript will save time later on.
Thanks for the heads up on the Xml object i'll check it out.
Copy link to clipboard
Copied
The script you gave me works fine for for a text frame that has only "<b>my text<b>"
But my text frame may be over 20 sentences and I need to be able to find any text between say <?> and <?>. Here is an example of what i am trying to get a handle on..
"This text <b>needs<b> to be bold. This text <b>also<b> needs to be bold. This <i>text<i> needs to be italic, and finally this <u>text<u> needs to be underlined."
So as you can see in the text above I want to capture, the words "needs", "also", "text" and "text" so that i can then change there styles.
Hope this makes more sense.
Copy link to clipboard
Copied
Are you aware that your examples are not properly formed XML?
Copy link to clipboard
Copied
Yes i am, the tags i used where just an example loosely based on some i've seen.
The text im trying to catch could be between anything that is unique ie "<b>" could be "-->".
It can be done using applescript but I'm already running a javascript on each text frame and thought it made sense to do this in javascript at the same time.
I hadn't anticipated that a question of grabbing random text between two unique place holders (one at the front and one at the back) would create such a debate!
Thanks for the interest [Jongware]
Copy link to clipboard
Copied
oh, ok, this will do
var myString = "This text <b>needs<b> to be bold. This text <b>also<b> needs to be bold. This <i>text<i> needs to be italic, and finally this <u>text<u> needs to be underlined.";
var firstIndex = 0;
var nextIndex = 0;
var lastIndex = myString.lastIndexOf ("<");
var mysubString = "";
var start = 0;
while (nextIndex!=lastIndex)
{
firstIndex = myString.indexOf (">", start);
nextIndex = myString.indexOf ("<", firstIndex);
mysubString = myString.substring(firstIndex+1, nextIndex);
alert (mysubString);
start = nextIndex+3;
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now