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)
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.
When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.
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)
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
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.
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).
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
elements
|
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.
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.
applescript - text caret position