Javascript 如何获取光标所在的文本框?

Javascript 如何获取光标所在的文本框?,javascript,adobe-indesign,Javascript,Adobe Indesign,所以我在一个文本框中输入,我决定运行一个脚本 我希望该脚本引用光标已经位于的文本框 在Javascript中,如何引用特定的文本框?解决方案: 考虑以下要点: var selectedItem = app.activeDocument.selection[0]; if (selectedItem instanceof InsertionPoint && selectedItem.parentTextFrames[0] instanceof TextFrame) {

所以我在一个文本框中输入,我决定运行一个脚本

我希望该脚本引用光标已经位于的文本框

在Javascript中,如何引用特定的文本框?

解决方案: 考虑以下要点:

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");
}
说明:

  • 首先,我们通过行读取获得对文档中所选内容的引用:

    var selectedItem=app.activeDocument.selection[0];
    
  • 然后,我们通过以下方式推断选择类型是否等同于“文本框中的光标”:

    • 首先检查它是否是InsertionPoint的
      实例
    • 其次,通过检查它是否是
      parentTextFrames
      TextFrame

  • 如果推断“光标是否在文本框中”的条件检查为
    true
    ,则我们继续将文本框的引用指定给名为
    textFrame
    的变量。i、 e

    var textFrame=selectedItem.parentTextFrames[0];
    
    为了证明变量
    textFrame
    确实包含对实际文本框的引用,我们删除了它

    textFrame.remove();//用文本框做东西!
    
  • 如果推断“光标是否在文本框中”的条件检查为
    false
    ,则我们会提醒用户“光标未放置在文本框中”


  • 文本框中选定的文本字符

    可能用户在文本框中选择了文本字符,而不是将光标放在文本框中。如果您也想在此场景中获取文本框引用,请将上面要点中的条件检查更改为类似以下内容:

    if((选择插入点的editem实例| |选择文本的editem实例)
    &&选择EdItem.parentTextFrames[0]实例(文本框的实例){
    // ...
    }
    
    请您提供代码的相关部分,您迄今为止尝试了什么,以及它如何未能达到预期的结果?@controlnetic.nomad,到目前为止,我尝试的是阅读大量有关InDesign脚本的文档。到目前为止,我还没有找到任何可以尝试的方法,因为我发现的所有选择文本框的方法都不相关。您的文本是从一个框流向另一个框,还是全部都在一个框中?它通常从一个框流向另一个框。您打算如何使用此脚本?你真的需要确定光标在哪个框中吗?在运行脚本之前,你不能选择文本框?给我们相关的代码不是一个坏主意(这些都是你可以通过编辑你的问题来回答的问题)
    if (selectedItem instanceof InsertionPoint &&
        selectedItem.parentTextFrames[0] instanceof TextFrame) {
    
        // ... If we get here, then the "cursor is in a Text Frame".
    
    }