iamsudo

Q: applescript - text caret position

How can I get the text caret coordinates of the frontmost application?

 

I cannot find a way to get its position.

MacBook Air, OS X Mountain Lion (10.8.4)

Posted on Jun 28, 2013 7:36 AM

Close

Q: applescript - text caret position

  • All replies
  • Helpful answers

  • by Camelot,

    Camelot Camelot Jun 28, 2013 9:18 AM in response to iamsudo
    Level 8 (47,233 points)
    Mac OS X
    Jun 28, 2013 9:18 AM in response to iamsudo

    There is no unified way to do this - some applications don't even have a text caret since they're not text-based.

     

    Therefore any solution would be application-specific (and -dependent). WIthout knowing which app(s) you're working with there's no way to answer your question directly (and even knowing the apps might not give you what you want if the app doesn't expose that data to AppleScript).

  • by iamsudo,

    iamsudo iamsudo Jun 28, 2013 10:28 AM in response to Camelot
    Level 1 (1 points)
    Jun 28, 2013 10:28 AM in response to Camelot

    Of course I'm talking about text based applications like TextEdit, MacVim, WriteRoom or that at least make use of text fields like browsers, Safari, Chrome, etc.

  • by Camelot,

    Camelot Camelot Jun 28, 2013 11:19 AM in response to iamsudo
    Level 8 (47,233 points)
    Mac OS X
    Jun 28, 2013 11:19 AM in response to iamsudo

    Why 'of course'? That wasn't apparent in your original post.

     

    In either case, my answer still stands - there is no universal attribute for this. Any support for determining the insertion point will be application-specific. You'll have to look at each application's dictionary to determine what options (if any) it has for this.

     

    For example, Pages.app has a rather extensive definition of 'insertion point':

     

    insertion point

     

    insertion point (noun), pl insertion pointslocation between two characters

    properties

    Property
    Access
    Type
    Description
    alignmentget/setalignmentThe horizontal alignment.
    baseline shiftget/setrealRaise or lower the target text.
    boldget/setbooleanWhether the font style is bold.
    capitalization typeget/setcapitalization typeWhether a capitalization style is applied.
    character background colorget/setcolorThe color of the character's background.
    character offsetgetintegerThe offset of the text from the beginning of the enclosing text object.
    character styleget/setcharacter styleThe representative character style of the object.
    classgettype classThe class of the object.
    item
    collapsedget/setbooleanWhether the paragraph is collapsed in the outline view.
    colorget/setcolorThe color of the font.
    containing pagegetpageThe page on which this text starts.
    contentsget/setanything
    first line indentget/setrealThe space between the first line of the paragraph and the left margin.
    following paragraph styleget/settextThe name of the following paragraph style. The empty string implies this style.
    font nameget/settextThe name of the font.
    font sizeget/setrealThe size of the font.
    hiddenget/setbooleanWhether the paragraph is hidden in the outline view.
    indent levelget/setintegerThe list indent level assigned to the paragraph, from 1 through 9.
    italicget/setbooleanWhether the font style is italic.
    keep lines togetherget/setbooleanKeep all lines of the paragraph on the same page.
    keep with next paragraphget/setbooleanKeep the target and following paragraph on the same page.
    label baseline shiftget/setrealThe amount to move the label up or down relative to the first line of the paragraph.
    label image dataget/setimage binaryThe image used for the label.
    label indentget/setrealThe distance from the left margin to the list label.
    label sizeget/setrealWhen "scale with text" is disabled the label size is a text point size for text labels or a multiplier of original image size for image labels. When enabled, it is always a multiplier of the representative font size of the paragraph.
    label typeget/setlabel typeThe type of label to use.
    left indentget/setrealThe space between the paragraph and the left margin.
    lengthgetintegerThe length of the text, in characters.
    ligaturesget/setligaturesRemove ligatures from the target text if the document is set to use ligatures.
    line spacingget/setrealThe amount of space between lines in the current spacing style.
    line spacing typeget/setline spacing typeThe type of line spacing.
    list styleget/setlist styleThe list style, if any, for the target.
    number label styleget/setnumber label styleThe type of label for number and tiered number types.
    number label tieredget/setbooleanWhether a numeric label displays the complete hierarchy for each level or just the label of the level.
    outlineget/setbooleanWhether the font style is outline.
    paragraph background colorget/setcolorThe color of the object's fill.
    paragraph styleget/setparagraph styleThe representative paragraph style of the text.
    prevent widows and orphansget/setbooleanPrevent the first or last line of a paragraph from appearing alone on a page.
    propertiesget/setrecordAll of the object's properties.
    item
    remove hyphenationget/setbooleanRemove hyphenation from the paragraph if the document is set to hyphenate words automatically.
    right indentget/setrealThe space between the paragraph and the right margin.
    scale with textget/setbooleanWhether the label size proportionally changes with the paragraph text size.
    shadowget/setbooleanWhether the object casts a shadow or not.
    shadow angleget/setrealThe directional angle, in degrees, that the shadow is cast.
    shadow blurget/setintegerThe relative amount of blur of images seen through the shadow.
    shadow colorget/setcolorThe color of the shadow.
    shadow offsetget/setrealThe offset from the text box content that the shadow extends to.
    shadow opacityget/setrealThe amount of opacity for the shadow, in percent.
    space afterget/setrealThe space after the paragraph, in points.
    space beforeget/setrealThe space before the paragraph, in points.
    start new pageget/setbooleanStart the paragraph at the beginning of the next page.
    strikethrough colorget/setcolorThe color of the strikethrough line(s).
    strikethrough typeget/setstrikethrough typeWhether one or more lines are drawn through the characters.
    subscriptget/setbooleanDecrease the font size and lower the baseline of the text.
    superscriptget/setbooleanDecrease the font size and raise the baseline of the text.
    text indentget/setrealThe distance from the label to the text.
    text label stringget/settextOne to nine characters can be specified for a text label.
    trackingget/setrealThe space between text characters, in percent.
    underline colorget/setcolorThe color of the underline(s).
    underline typeget/setunderline typeWhether the font style is underline.

    elements

    Element
    Access
    Key Forms
    Description
    characterget/ make/ deleteby name
    by index
    by range
    relative to others
    by whose/where
    by unique ID

    chartget/ make/ deleteby name
    by index
    by range
    relative to others
    by whose/where
    by unique ID

    graphicget/ make/ deleteby name
    by index
    by range
    relative to others
    by whose/where
    by unique ID

    imageget/ make/ deleteby name
    by index
    by range
    relative to others
    by whose/where
    by unique ID

    insertion pointget/ make/ deleteby name
    by index
    by range
    relative to others
    by whose/where
    by unique ID

    lineget/ make/ deleteby name
    by index
    by range
    relative to others
    by whose/where
    by unique ID

    paragraphget/ make/ deleteby name
    by index
    by range
    relative to others
    by whose/where
    by unique ID

    shapeget/ make/ deleteby name
    by index
    by range
    relative to others
    by whose/where
    by unique ID

    tableget/ make/ deleteby name
    by index
    by range
    relative to others
    by whose/where
    by unique ID

    textget/ make/ deleteby name
    by index
    by range
    relative to others
    by whose/where
    by unique ID

    text boxget/ make/ deleteby name
    by index
    by range
    relative to others
    by whose/where
    by unique ID

    wordget/ make/ deleteby name
    by index
    by range
    relative to others
    by whose/where
    by unique ID

    where used

    The insertion point class is used in the following ways:

    element of attachment class

    element of character class

    element of paragraph class

    element of text class

    element of word class

     

    whereas other applications (e.g. TextEdit) have no apparent support. It's all down to individual applications.

     

    In addition, you haven't stated what it is you expect to determine - are you looking for a position on screen? relative to the document? relative to the window/view portal?

     

    Maybe an even more pertinent question is what are you trying to do with this, anyway? It seems like an odd request that may hint at there being a different (better?) way of achieving the result you're after.

  • by iamsudo,

    iamsudo iamsudo Jun 28, 2013 12:44 PM in response to Camelot
    Level 1 (1 points)
    Jun 28, 2013 12:44 PM in response to Camelot

    I use MouseTools to right-click on the cursor position and get the CM.

     

    I'm searching for a way to determine the text caret/insertion point coordinates to right-click on text.

  • by Mike Retondo,

    Mike Retondo Mike Retondo Sep 3, 2016 8:51 PM in response to iamsudo
    Level 1 (13 points)
    Mac OS X
    Sep 3, 2016 8:51 PM in response to iamsudo

    There's code here that I use to get insertion position in Xcode.

     

    -- check if there is no selection

    --delete (paragraphs startLine through endLine)

    --make new paragraph at beginning of paragraph (startLine - 1) with data theText

    --set selected paragraph range to {startLine - 1, endLine - 1}

     

    using terms from application "Xcode"

      tell application "Xcode"

      set lastDoc to last source document

      set docText to the text of lastDoc

      --set sel to selection of lastDoc

     

      if (my isSelectedCharacterRangeEmpty(lastDoc)) then

      --copy the current line to the clipboard

      --set the clipboard to my copyCurrentLine(lastDoc)

     

      set selectedCharacterRange to selected character range of lastDoc

      set {startPos, endPos} to selectedCharacterRange

      set selectionText to the (text startPos thru startPos) in docText

      my sendCopyCmd("Xcode")

      else

      --set selectedCharacterRange to selected character range of lastDoc

      --set startPos to first item in selectedCharacterRange

      --set endPos to last item in selectedCharacterRange

      --or

      --set {startPos, endPos} to selected character range of lastDoc

     

      --set selectionText to the (text startPos thru endPos) in docText

     

      my sendCopyCmd("Xcode")

      end if

      end tell

     

      on isSelectedCharacterRangeEmpty(theContainer)

      tell application "Xcode"

      set selectedCharacterRange to selected character range of theContainer

      set {startPos, endPos} to selectedCharacterRange

     

      if {endPos < startPos} or ¬

      (selectedCharacterRange is equal to {}) or ¬

      (length of selectedCharacterRange is equal to 0) then

      return true

      else

      return false

      end if

      end tell

      end isSelectedCharacterRangeEmpty

     

      on copyCurrentLine(theDoc)

      tell application "Xcode"

      set docText to the text of theDoc

     

      --for text documents paragraph means line

      set {startLine, endLine} to the selected paragraph range in theDoc

     

      --only one line can be copied

      if startLine > 0 and startLine = endLine then

      set theText to the (paragraphs startLine through endLine) in docText as text

      else

      beep 1

      end if

     

      return theText

      end tell

      end copyCurrentLine

    end using terms from

     

    on sendCopyCmd(appName)

      activate application "Xcode"

     

      tell application "System Events" to tell process appName

      keystroke "c" using {command down}

      end tell

    end sendCopyCmd

     

    on sendCutCmd(appName)

      activate application appName

      tell application "System Events" to keystroke "c" using command down

     

      -- tell application "System Events" to tell process appName

      -- keystroke "x" using {command down}

      -- end tell

    end sendCutCmd