You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

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

Reply
5 replies

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 (textstartPos 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)


activateapplication "Xcode"


tell application "System Events" to tell process appName


keystroke "c" using {command down}

end tell

end sendCopyCmd


on sendCutCmd(appName)


activateapplicationappName

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

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).

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
alignment get/set alignment The horizontal alignment.
baseline shift get/set real Raise or lower the target text.
bold get/set boolean Whether the font style is bold.
capitalization type get/set capitalization type Whether a capitalization style is applied.
character background color get/set color The color of the character's background.
character offset get integer The offset of the text from the beginning of the enclosing text object.
character style get/set character style The representative character style of the object.
class get type class The class of the object.
item
collapsed get/set boolean Whether the paragraph is collapsed in the outline view.
color get/set color The color of the font.
containing page get page The page on which this text starts.
contents get/set anything
first line indent get/set real The space between the first line of the paragraph and the left margin.
following paragraph style get/set text The name of the following paragraph style. The empty string implies this style.
font name get/set text The name of the font.
font size get/set real The size of the font.
hidden get/set boolean Whether the paragraph is hidden in the outline view.
indent level get/set integer The list indent level assigned to the paragraph, from 1 through 9.
italic get/set boolean Whether the font style is italic.
keep lines together get/set boolean Keep all lines of the paragraph on the same page.
keep with next paragraph get/set boolean Keep the target and following paragraph on the same page.
label baseline shift get/set real The amount to move the label up ➕ or down ➖ relative to the first line of the paragraph.
label image data get/set image binary The image used for the label.
label indent get/set real The distance from the left margin to the list label.
label size get/set real When "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 type get/set label type The type of label to use.
left indent get/set real The space between the paragraph and the left margin.
length get integer The length of the text, in characters.
ligatures get/set ligatures Remove ligatures from the target text if the document is set to use ligatures.
line spacing get/set real The amount of space between lines in the current spacing style.
line spacing type get/set line spacing type The type of line spacing.
list style get/set list style The list style, if any, for the target.
number label style get/set number label style The type of label for number and tiered number types.
number label tiered get/set boolean Whether a numeric label displays the complete hierarchy for each level or just the label of the level.
outline get/set boolean Whether the font style is outline.
paragraph background color get/set color The color of the object's fill.
paragraph style get/set paragraph style The representative paragraph style of the text.
prevent widows and orphans get/set boolean Prevent the first or last line of a paragraph from appearing alone on a page.
properties get/set record All of the object's properties.
item
remove hyphenation get/set boolean Remove hyphenation from the paragraph if the document is set to hyphenate words automatically.
right indent get/set real The space between the paragraph and the right margin.
scale with text get/set boolean Whether the label size proportionally changes with the paragraph text size.
shadow get/set boolean Whether the object casts a shadow or not.
shadow angle get/set real The directional angle, in degrees, that the shadow is cast.
shadow blur get/set integer The relative amount of blur of images seen through the shadow.
shadow color get/set color The color of the shadow.
shadow offset get/set real The offset from the text box content that the shadow extends to.
shadow opacity get/set real The amount of opacity for the shadow, in percent.
space after get/set real The space after the paragraph, in points.
space before get/set real The space before the paragraph, in points.
start new page get/set boolean Start the paragraph at the beginning of the next page.
strikethrough color get/set color The color of the strikethrough line(s).
strikethrough type get/set strikethrough type Whether one or more lines are drawn through the characters.
subscript get/set boolean Decrease the font size and lower the baseline of the text.
superscript get/set boolean Decrease the font size and raise the baseline of the text.
text indent get/set real The distance from the label to the text.
text label string get/set text One to nine characters can be specified for a text label.
tracking get/set real The space between text characters, in percent.
underline color get/set color The color of the underline(s).
underline type get/set underline type Whether the font style is underline.

elements

Element
Access
Key Forms
Description
character get/ make/ delete by name
by index
by range
relative to others
by whose/where
by unique ID
chart get/ make/ delete by name
by index
by range
relative to others
by whose/where
by unique ID
graphic get/ make/ delete by name
by index
by range
relative to others
by whose/where
by unique ID
image get/ make/ delete by name
by index
by range
relative to others
by whose/where
by unique ID
insertion point get/ make/ delete by name
by index
by range
relative to others
by whose/where
by unique ID
line get/ make/ delete by name
by index
by range
relative to others
by whose/where
by unique ID
paragraph get/ make/ delete by name
by index
by range
relative to others
by whose/where
by unique ID
shape get/ make/ delete by name
by index
by range
relative to others
by whose/where
by unique ID
table get/ make/ delete by name
by index
by range
relative to others
by whose/where
by unique ID
text get/ make/ delete by name
by index
by range
relative to others
by whose/where
by unique ID
text box get/ make/ delete by name
by index
by range
relative to others
by whose/where
by unique ID
word get/ make/ delete by 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.

applescript - text caret position

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.