How can I capture all the text from the topmost window / document on OSX as much as possible?

I want to be able to capture all the text from the very first window on my screen , pass it to the url parser and choose which url to open from the parsed text.

To do this, I need a way to access all the text content of the topmost window. I came close using applescript, but cannot find a way to get the actual text. I suspect that a quick one might help, but I have lost even Google how to do it, since most of my searches end up looking for only trivial approaches like getting a url from a tab in chrome, etc.

This is the essence of what I want:

tell the frontmost application
    get contents of frontmost window as text
end tell
+4
source share
2 answers

Have you tried to get all the user interface elements for a particular application?

set appname to "System Preferences" -------------------------- Set this to the App you want to look at

display dialog "Set the app you want to look at" default answer "System Preferences" buttons {"OK"} default button 1
set appname to text returned of result

set winstuff to "defaultval"
set menustuff to "defaultval"

tell application appname
    activate
end tell

delay 0.5

tell application "System Events"
    tell process appname
        set winstuff to entire contents of front window
        set menustuff to entire contents of menu bar 1
    end tell
end tell
--return winstuff & "\r\r\r\r" & menustuff -- comment this out to get just winstuff
return winstuff -- comment this out too to get just menustuff
--return menustuff
+1
source

Solution for JXA:

//Example get all text from "ScreenFlow"
var sProc  = "ScreenFlow"
var system = Application('System Events')
var elements = system.processes[sProc].windows[0].entireContents()
var s = ""
for (var i=0;i<elements.length;i++){
    s = s + elements[i].name() + ","
}
s = s.replace(/null,/g,"")

//Return s
console.log(s)

There are probably other children that you can add to make this list more dense. Example output from my video editor:

"Video,+ Action,Color:,Offset:,Blur Size,Shadow,Opacity,Scale:,Z Rotation:,Reflection,Y Rotation:,Opacity:,Cropping,X Rotation:,Position:,Color Controls,Corner Round:,Video Filters,Duration: 1 min 0 secs,UI Mass Email App,UI Mass Email App,"

Please note that this does not capture menu text. For this you have to go throughsystem.processes[sProc].menuBars

0
source

Source: https://habr.com/ru/post/1649616/


All Articles