-2

So I'm typing in a text frame and I decide to run a script.

I'd like that script to refer to the text frame that my cursor is already in.

In Javascript, how do I refer to that particular textFrame?

Joe
  • 3,804
  • 7
  • 35
  • 55
  • 1
    Can you please [edit your question](https://stackoverflow.com/posts/55210393/edit) to provide the relevant part of your code, what you've tried so far and how it fails to achieve the desired result? – cybernetic.nomad Mar 18 '19 at 15:02
  • @cybernetic.nomad, what I've tried so far is reading lots of documentation about InDesign scripting. I haven't been able to figure out anything to try so far, as all the ways of selecting a textFrame that I've found aren't relevant. – Joe Mar 18 '19 at 19:01
  • Does your text flow from one box to another or is it all in one text box? – cybernetic.nomad Mar 18 '19 at 19:03
  • It usually flows from one box to another. – Joe Mar 18 '19 at 19:05
  • What are you trying to do with this script? Do you actually need to determine what box the cursor is in, you can't select the text box before running the script?It would not be a bad idea to give us the relevant code (these are all question you can answer by editing your question) – cybernetic.nomad Mar 18 '19 at 19:06

1 Answers1

1

Solution:

Consider the following gist:

var selectedItem = app.activeDocument.selection[0];

if (selectedItem instanceof InsertionPoint &&
    selectedItem.parentTextFrames[0] instanceof TextFrame) {

    var textFrame = selectedItem.parentTextFrames[0];

    // This just demonstrates that the variable `textFrame` does
    // hold a reference to the actual text frame - let's delete it !
    textFrame.remove();

} else {
    alert("The cursor has not been placed in a text frame");
}

Explanation:

  1. Firstly we obtain a reference to whatever is selected in the document via the line reading:

    var selectedItem = app.activeDocument.selection[0];
    
  2. We then infer whether the type of selection equates to a "cursor in a Text Frame" by:

    • Firstly checking that it's a instanceof InsertionPoint.
    • Secondly, by checking that it's parentTextFrames is a TextFrame.

    if (selectedItem instanceof InsertionPoint &&
        selectedItem.parentTextFrames[0] instanceof TextFrame) {
    
        // ... If we get here, then the "cursor is in a Text Frame".
    
    }
    
  3. If the conditional checks that infer whether the "cursor is in a Text Frame" are true then we proceed to assign the Text Frame's reference to a variable named textFrame. i.e.

    var textFrame = selectedItem.parentTextFrames[0];
    

    Just to demonstrate that the variable textFrame does hold a reference to the actual text frame we delete it !

    textFrame.remove(); // Do stuff with the text frame !
    
  4. If the conditional checks that infer whether the "cursor is in a Text Frame" are false then we alert the user that "The cursor has not been placed in a text frame".


Selected text characters in a Text Frame

Maybe the user has selected text characters in a text Frame, instead of just placing the cursor in a text frame. If you wanted to get the Text Frame reference in this scenario too - then change your conditional check in the gist above to something like this:

if ((selectedItem instanceof InsertionPoint || selectedItem instanceof Text) 
    && selectedItem.parentTextFrames[0] instanceof TextFrame) {
    // ...
}
RobC
  • 22,977
  • 20
  • 73
  • 80