3

Is it possible to get the text of Pages app, from its current cursor position? My requirement is like, when user type something in "Pages", I have to show suggestions for the word they are typing.

so I want to find out the current or last word, near current cursor position from "Pages" app. Either by using AppleScript or Accessibility? Text is not selected. I am not looking for "Services" also. For apps other than "Pages", I used Accessibility and appleScript. but for pages I am not finding any way.

I have also tried below AppleScript, but some reason it works perfectly in "Script Editor", but when I use it in my code, it goes to infinite loop.

tell application "Pages"
    activate
end tell
tell application "System Events"
    tell application "System Events"
        key code 123 using {shift down, command down} -- shift-command-left
    end tell
    tell process "Pages"
        keystroke "c" using {command down}
        delay 1
        tell application "System Events"
            key code 124 -- shift-command-left
        end tell
        set myData to (the clipboard) as text
        return myData
    end tell
end tell

If I run this AppleScript in my app, it freeze my Mac only, I have to force quit the Mac to stop it.

Krishna Maru
  • 164
  • 13
  • Possible duplicate of [Adding item to Contextual menu and send highlighted text to other app on MacOS](https://stackoverflow.com/questions/41484162/adding-item-to-contextual-menu-and-send-highlighted-text-to-other-app-on-macos) – Willeke Dec 26 '18 at 09:03
  • Possible duplicate of [Programmatically catch selected text in other application from my MacOSX application](https://stackoverflow.com/questions/52845062/programmatically-catch-selected-text-in-other-application-from-my-macosx-applica) – Willeke Dec 26 '18 at 09:03
  • Possible duplicate of [How to get global screen coordinates of currently selected text via Accessibility APIs](https://stackoverflow.com/questions/6544311/how-to-get-global-screen-coordinates-of-currently-selected-text-via-accessibilit) – Willeke Dec 26 '18 at 09:04
  • Possible duplicate of [Get currently selected text in active application in Cocoa](https://stackoverflow.com/questions/19980020/get-currently-selected-text-in-active-application-in-cocoa) – Willeke Dec 26 '18 at 09:05
  • Possible duplicate of [How to obtain the selected text from another application?](https://stackoverflow.com/questions/1487175/how-to-obtain-the-selected-text-from-another-application) – Willeke Dec 26 '18 at 09:06
  • @Willeke, thanks for links, but I am not asking for selected text. I want text which is before the caret position. if there is selection, I am able to get the text using Accessibility for some apps and for others I can use appleScript. But I want the text which is not selected, in Pages application. just by finding cursor position I want to get the text which is before that cursor/caret is placed. For the apps like Notes/Textedit, I got cursor position (even if there is no selection) by getting NSRange, and then I took text from 0 to the NSRange location I got. – Krishna Maru Dec 26 '18 at 09:38
  • Note: the linked questions are possible duplicates of the original question. – Willeke Dec 26 '18 at 10:17
  • How do you execute the AppleScript in code? Are Xcode and/or your app allowed to control your computer? – Willeke Dec 26 '18 at 10:20
  • @Willeke, using NSAppleScript. and yes my app is not sandbox so, app is allowed. – Krishna Maru Dec 26 '18 at 10:30
  • I tried `NSAppleScript` and the script and it runs ok (after solving the privacy issues). – Willeke Dec 26 '18 at 23:11
  • The insertion point (text cursor) is just a zero-length selection. So, the links for getting the selection are relevant. – Ken Thomases Dec 27 '18 at 02:53
  • @Willeke, what privacy issues did you face? – Krishna Maru Dec 27 '18 at 05:42
  • @KenThomases, yes, links have solutions for getting zero-length selection, but does accessibility work with pages app? I am already using accessibility and zero length selection for apps like Notes & text editor, but for pages I am not getting any selection range. – Krishna Maru Dec 27 '18 at 05:42
  • Hmm, good point. The Accessibility Inspector doesn't show any elements within the scroll area of the document window. That's odd. I would have expected better accessibility support from an Apple app. For a different approach, can your task be achieved by implementing an input method? Those have a degree of access to the document into which you're typing. – Ken Thomases Dec 27 '18 at 06:21
  • @KenThomases, sorry, I didn't get what you mean by "For a different approach, can your task be achieved by implementing an input method? Those have a degree of access to the document into which you're typing." – Krishna Maru Dec 27 '18 at 06:37
  • `executeAndReturnError` returns an error "Not authorized to send Apple events". – Willeke Dec 27 '18 at 11:00
  • There is one app "ViTre", which has same feature, while typing it shows suggestions. that app is showing suggestions for "Pages" app too. Even after I deny Accessibility permission for that app, it shows suggestions , that means they have used some other way. – Krishna Maru Dec 27 '18 at 12:17
  • I was suggesting that rather than an app (or perhaps in addition to an app), you implement an input method to achieve the ability to provide the suggestions UI you want. – Ken Thomases Dec 27 '18 at 16:26

1 Answers1

4

This works for me using the latest versions of macOS Mojave and Pages

property theApp : "Pages" -- change value to name of any other application (TextEdit)

tell application theApp to activate
delay 3
tell application "System Events"
    tell application process theApp
        -- Move the insertion point to the beginning of the previous word.
        key code 123 using {option down} -- left arrow key while holding option down
        delay 0.2
        -- Move the insertion point to the end of the next word. (selects the word)
        key code 124 using {shift down, option down} -- right arrow key while holding option and shift down
        delay 0.2
        keystroke "c" using {command down} -- copies selected wprd 
        delay 0.2
        -- Next 2 key code commands attempt to restore cursor location  
        key code 124 using {option down} -- right arrow key while holding option down
        delay 0.2
        key code 123 using {option down} -- left arrow key while holding option down
        tell current application to set myData to (the clipboard) as text
        delay 4
        return myData
    end tell
end tell
wch1zpink
  • 3,026
  • 1
  • 8
  • 19
  • I am still facing same issue. when I run this AppleScript in my project it goes to infinite loop and freeze the Mac, I have to force shut down to stop it. I am calling this appleScript on NSEvent.addGlobalMonitorForEvents, so that when user type something I will get the last words from cursor position. – Krishna Maru Dec 27 '18 at 07:13
  • @KrishnaMaru it would help us to help you if you provide this information in the question. Your monitor block is triggered by the keystrokes from the AppleScript. – Willeke Dec 27 '18 at 11:10
  • @KrishnaMaru updated the code in my post. Does the new version work any better for you? – wch1zpink Dec 29 '18 at 02:35
  • @wch1zpink, found the issue. I am calling this AppleScript on NSEvent.addGlobalMonitorForEvents(matching: .keyDown), so first time it will run the appleScript, but again in our appleScript we have 124/123 keyDown event, which will make this AppleScript to run in infinite loop. – Krishna Maru Jan 03 '19 at 13:19