Skip to main content
ali u
Inspiring
April 7, 2023
Answered

Is it possible to use Grep pattern with IndexOf()?

  • April 7, 2023
  • 2 replies
  • 1245 views

I use this code to find and select the next occurrence of a search string from cursor point, is it possible to do this with grep search patterns?

 

var doc = app.activeDocument;
var insertionPoint = doc.selection[0].insertionPoints[0];

// Get the text content following the insertion point
var text = insertionPoint.parent.characters.itemByRange(insertionPoint.index + 1, -1).contents;

// Convert the text content to a string
var textString = new String(text);

// Define the search string
var searchString = "ali";

// Find the next occurrence of the search string from the insertion point
var nextSearchString = textString.indexOf(searchString);

// Check if the search string is found
if (nextSearchString >= 0) {
// If found, move the insertion point to the next search string
foundtext = insertionPoint.parent.characters.itemByRange(insertionPoint.index + 1 + nextSearchString, insertionPoint.index + 1 + nextSearchString + searchString.length - 1);

// Select the next search string
foundtext.select();

}

 

This topic has been closed for replies.
Correct answer m1b

Or if you just want to select the next found instance of the grep?

var doc = app.activeDocument;
var insertionPoint = doc.selection[0].insertionPoints[0];

// Get the text content following the insertion point
var textFromCursor = insertionPoint.parent.characters.itemByRange(insertionPoint.index + 1, -1);

// reset grep prefs
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;

// the find grep (note: must escape backslash)
app.findGrepPreferences.findWhat = '\\bali\\b';

// the change string
app.changeGrepPreferences.changeTo = 'Ali';

// do this if you want to process the results yourself
var found = textFromCursor.findGrep();

found[0][0].showText();
found[0][0].select();

2 replies

Robert at ID-Tasker
Brainiac
April 8, 2023

Is "insertionPoint.index + 1" really needed to address character after it?

As InsertionPoint always preceedes Character - you can use its index as an index of the next character? Or in JS it works differently? 

Or am I missing something? 

 

m1b
Community Expert
April 8, 2023

Hi Robert, no you're correct about how it works. The reason for adding +1 to the index here is so that the script will find the next instance if the selected text is already a found instance. For example, if I run the above script and it finds and selects the first "ali", without the +1, subsequent executions of the script will keep finding that same instance of "ali".

 

The downside, as you noticed, is that if the user puts the cursor literally at the start of an instance of "ali" and runs script it won't find that instance, because it starts searching from the "l". However, I think this is acceptable in most cases. To fix it, we'd need to remove the +1, and check if the found instance is selected, and if so, ignore it and go to the next.

- Mark

Robert at ID-Tasker
Brainiac
April 9, 2023

Thanks for the clarification 🙂

 

But OP's request is "from cursor point"?

So Loop should "move" cursor AFTER processing 1st occurrence - not before? 

 

Ok, you may be right @m1b  ... or not😉 

 

@ali u could you please clarify your request?

Do you want to find & change ALL instances of "ali" - including the one right after the InsertionPoint - or ignore the 1st one and only change the rest? 2nd, 3rd, ...

 

m1b
Community Expert
April 7, 2023

Hi @ali u, yes it is possible, if I understand you correctly. See below. You just set the findWhat to a grep and set the changeTo, and run the script. Your code to get the text from the cursor position seems to work fine (I just removed contents because we want a Text object not a String—Text objects have findGrep and changeGrep methods).

- Mark

function main() {

    var doc = app.activeDocument;
    var insertionPoint = doc.selection[0].insertionPoints[0];

    // Get the text content following the insertion point
    var textFromCursor = insertionPoint.parent.characters.itemByRange(insertionPoint.index + 1, -1);
    
    // reset grep prefs
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;

    // the find grep (note: must escape backslash)
    app.findGrepPreferences.findWhat = '\\bali\\b';

    // the change string
    app.changeGrepPreferences.changeTo = 'Ali';

    // do this if you want to process the results yourself
    var found = textFromCursor.findGrep();

    // do this if you just want to make the changes
    textFromCursor.changeGrep();
    
    alert(found[0].length + ' found.');

};

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Find Grep From Cursor Position');

 

m1b
m1bCorrect answer
Community Expert
April 7, 2023

Or if you just want to select the next found instance of the grep?

var doc = app.activeDocument;
var insertionPoint = doc.selection[0].insertionPoints[0];

// Get the text content following the insertion point
var textFromCursor = insertionPoint.parent.characters.itemByRange(insertionPoint.index + 1, -1);

// reset grep prefs
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;

// the find grep (note: must escape backslash)
app.findGrepPreferences.findWhat = '\\bali\\b';

// the change string
app.changeGrepPreferences.changeTo = 'Ali';

// do this if you want to process the results yourself
var found = textFromCursor.findGrep();

found[0][0].showText();
found[0][0].select();
ali u
ali uAuthor
Inspiring
April 10, 2023

@m1b 

Thank you very much, this does what I want. I was wondering if it is somehow possible to use grep patterns with IndexOf() but I guess thats not possible, and probably not needed.