Skip to main content
gert verrept
Inspiring
January 8, 2014
Answered

switching words

  • January 8, 2014
  • 1 reply
  • 1341 views

We want to achieve the following (in works with F&C, but because of the hundreds of changes to be made, we tried a scripting solution):
In a paragraph we need to switch two consecutive words, so word 1 word2 becomes word 2 word1 .
I tried this, but it changes all words in the paragraph

app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;

app.findGrepPreferences.findWhat = “(\\w+) (\\w+) “;
app.changeGrepPreferences.changeTo = “$2 $1 “;

app.activeDocument.changeGrep();

This topic has been closed for replies.
Correct answer Jongware

Unfortunately, the gains may not be as high as you expect. All interaction of a script with InDesign objects (such as text) is slow, especially when working with text -- and it's more noticeable near the end of a long story, where all text manipulations may be agonizingly slow.

Nevertheless ... here is my attempt. It moves the word the cursor is in (at start, in the middle, or even right after the last character) afrer the next word. In itself it should have been a simple "Word" object manipulation, but I noticed the space at the end is not included in this! So I had to resort to the lower level of moving "Character" objects.

It seems to work sort of allright, as long as the words don't include punctuation or a hard return (it always seems to take the unexpected route with these).

p = app.selection[0].parentStory;

i = app.selection[0].insertionPoints[0].index;

wrds = p.words.everyItem().getElements();

w = 0;

while (wrds[w+1].index <= i) w++;

p.characters.itemByRange(wrds.characters[0],p.characters[wrds[w+1].characters[0].index-1]).move(LocationOptions.AFTER, p.characters[wrds[w+1].characters[-1].index+1]);

Tested on a moderately long article (10 pages of continuous text), and the slowdown near the end is already noticeable.

1 reply

Jongware
Community Expert
Community Expert
January 8, 2014

If you do not want to switch *every* two words, what is the criterium? How would the script know which words to target?

gert verrept
Inspiring
January 8, 2014

Indeed, in fact the script should do this:

we put the cursor before the two words to be switched the script should select those words and reverse the order of them. I tried adding \b, but that only works in some cases. ( an example: "artikel bedoeld " should become "bedoelde artikel", the "e" we can add easily afterwards).

The advantage of the script is that we can assign shortcut to it and so gain a lot of time.

Jump_Over
Legend
January 8, 2014

Hi,

You could use a code pasted below. To mark 1st word:

  • place cursor before it
  • place cursor somewhere inside this word
  • select more text to mark 1st word of selection

if ( ! (app.selection[0] && app.selection[0].hasOwnProperty ('baseline') ) )

          { alert ('wrong selection'); exit(); }

var

          mDoc = app.activeDocument,

          mIPs = app.selection[0].insertionPoints[0],

          mIPe = mIPs.paragraphs[0].insertionPoints[-1],

          mTarget = mIPs.parentStory.insertionPoints.itemByRange(mIPs, mIPe).texts[0];

app.findGrepPreferences = app.changeGrepPreferences = null;

app.findGrepPreferences.findWhat = '(\\b\\w+\\b)(\\s+)(\\b\\w+\\b)';

app.changeGrepPreferences.changeTo = '$3$2$1';

if (mTarget.words.length > 1) {

          mTarget = mTarget.words.itemByRange(0,1).texts[0];

          mTarget.changeGrep();

          }

else alert ('wrong position');

app.findGrepPreferences = app.changeGrepPreferences = null;

It should stop if count of words from current point to the end of curr para is less then 2.

Jarek