Skip to main content
Obi-wan Kenobi
Legend
October 2, 2016
Question

Find/Replace limited to a text selection or the active story! …

  • October 2, 2016
  • 1 reply
  • 473 views

Hi all,

I can't find the good syntax to limit my research! 

Placing the cursor in a story or selecting a story text frame:

var mySelectedContent = app.selection[0].parentStory.contents;

… or selecting text:

var mySelectedContent = app.selection[0].contents;

… but after!? …

app.findGrepPreferences = app.changeGrepPreferences = null; 

app.findGrepPreferences.findWhat = "……";

……

Thanks in advance!

(^/)

This topic has been closed for replies.

1 reply

Jongware
Community Expert
Community Expert
October 2, 2016

It's so easy If you know how! Four lines of scripting, and all 'a's in the selection only are changed to 'b's!

Obi-wan Kenobi
Legend
October 2, 2016

Thanks Jongware! … 3 lines seem to be enough! 

app.findTextPreferences.findWhat = "a"; 

app.changeTextPreferences.changeTo = "b";

app.selection[0].changeText( );

(^/)

Jongware
Community Expert
Community Expert
October 2, 2016

Heh heh – No, that's exactly what I did (!), but my 1st line is "app.findGrepPreferences = app.changeGrepPreferences = null;". If you omit that, you could be nastily surprised if there were a couple of previous settings in the dialog. (Does this mean that you were able to answer your own question? I think you did.)

A little comment on your previous attempts. Addressing 'contents' anywhere inside your script converts the formatted InDesign text to a plain JavaScript string. This not only discards all formatting, but also silently translates some characters (all kinds of page and column breaks become regular paragraph returns) and throws away stuff such as embedded text frames, tables, and footnotes.

So, safest is to forget 'contents' exists and stick to InDesign's 'formatted text' objects such as 'story', 'paragraph', and 'texts' itself. 'contents' has its uses but only if you are inserting new text, or re-purposing text for use 'outside' your document.

Nice to see you're getting along in scripting, by the way!