• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How can I get preceding and sibling characters for an inserting point ?

Explorer ,
Apr 28, 2020 Apr 28, 2020

Copy link to clipboard

Copied

Hi, 

I work on an InDesign script doing some work depending on an insertion point.

My goal is : if my insertion point is directly preceded or directly followed by a given character (for example /), I would like to insert a space between both.

 

For example if I have this text : 

 

"Example 1/Example 2 for test"

 

And the given insertion point is between "1" and "/" ; I would like to obtain this :

 

"Example 1 /Example 2 for test"

 

Or if it's between '/' and "E", this :

"Example 1/ Example 2 for test"

 

Any idea please ?

 

Thanks

TOPICS
How to , Scripting

Views

711

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 28, 2020 Apr 28, 2020

Copy link to clipboard

Copied

Hi Emilie,

 

Try the following code with cursor placed inside a text, so that app.selection[0] would denote a insertionPoint. The alert should give you the character that is after the insertionPoint

var ins = app.selection[0]
var index = ins.index
var c = ins.parent
c.characters[index].isValid && alert(c.characters[index].contents)

-Manan

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 28, 2020 Apr 28, 2020

Copy link to clipboard

Copied

I would probably just use Find/Replace. It's scriptable, too.  For instance:

 

//reset your preferences
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;
//set the find and change
app.findGrepPreferences.findWhat = "(/)(E)";
app.changeGrepPreferences.changeTo = "$1 $2";
//change it. can also change on certain objects, ie. myDoc.changeGrep();
app.changeGrep();
//reset preferences
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;

 

You can also do finds, and then loop through the finds to get Text objects. From there, you can get the insertion point of the find.  

 

app.findGrepPreferences.findWhat = "/E";
var myFinds = app.findGrep();
for (var i = 0; i < myFinds.length; i++) {
    var middleIP = myFinds[i].insertionPoints[1];
}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 28, 2020 Apr 28, 2020

Copy link to clipboard

Copied

Thanks for both responses.

I couldn't use find and replace because I don't want to do this at all places in the document. Only for some insertion points.

Manan's answer seems to help me see what's the next character. I've to work on it then.

If I gave you the context, you will understand why I can't use find and replace or Grep. 
The whole goal of the main script is to add some index references on a document. And it works in many cases. 
We get the insertion point we want and add it to page references  :

topic.pageReferences.add(ip)

But sometimes when I have this type of content :

Example 1/Example 2 for test

the calculated insertion point for index is between "1" and "/". 
You will probably say "not a problem". But that's a big one for me because InDesign inserts that reference index character and my text/title is now really long and non-breaking which causes many problems in cascads with overset text and so on.

What I want here is : if I'm in this case, inserts a space to allow InDesign to break my line at this insertion point (where I've inserted index character).

Not sure that is really clear ; it's also difficult for me

Thanks

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 29, 2020 Apr 29, 2020

Copy link to clipboard

Copied

LATEST

If your text is too long and nonbreakting think about inserting a zero width nonjoiner. With a script this character is named:

SpecialCharacters.ZERO_WIDTH_NONJOINER

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 28, 2020 Apr 28, 2020

Copy link to clipboard

Copied

I see you needed the preceding character as well, for that you will have to decrement the index by 1, i suppose you would have figured that out

 

-Manan

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines