How to Obtain the Selected Text from the Frontmost Application in MACos

How to obtain the selected text from the frontmost application in macOS?

Accessibility will work, but only if access for assistive devices is on.

You'll need to get the current application, then get its focused UI element, then get its selected text ranges and its value (whole text) and selected text ranges. You could just get its selected text, but that would either concatenate or ignore multiple selections.

Be prepared for any of those steps to fail: The app may not have any windows up, there may be no UI element with focus, the focused UI element may have no text, and the focused UI element may have only an empty selected text range.

Obtaining the currently selected text in front-most application

Services may be what you want, though the user has to choose your service through the service menu, or contactual menu.

Copy and pasting selected text from the current, from a Mac sandboxed application

so far all my attempts failed

There is a reason for that...

A core purpose of the sandbox is to provide isolation between applications, to prevent one application from accessing the data of another. So what you are asking is how to break out of the sandbox...

If you are writing accessibility software take a look at How to use Accessibility with sandboxed app? for maybe the smallest glimmer of a hope.

Otherwise your current path is a (hopefully, its a sandbox) dead-end, security has a price, sorry.

How to get name of frontmost app with AppleScript and use it to get the filepath

Either get the path property of a document or use System Events to get value of attribute "AXDocument":

try
tell application (path to frontmost application as text)
(path of document 1) as text
end tell
on error
try
tell application "System Events" to tell (process 1 where frontmost is true)
value of attribute "AXDocument" of window 1
end tell
do shell script "x=" & quoted form of result & "
x=${x/#file:\\/\\/}
x=${x/#localhost} # 10.8 and earlier
printf ${x//%/\\\\x}"
end try
end try

The first method didn't work with Preview, TextMate 2, Sublime Text, or iChm, and the second method didn't work with Acorn. The second method requires access for assistive devices to be enabled.

Get the string from selected text/ highlighted text using JXA

In general, you can use the System Events app to copy and paste with any app.

'use strict';
//--- GET A REF TO CURRENT APP WITH STD ADDITONS ---var app = Application.currentApplication()app.includeStandardAdditions = true
var seApp = Application('System Events')
//--- Set the Clipboard so we can test for no selection ---app.setTheClipboardTo("[NONE]")
//--- Activate the App to COPY the Selection ---var safariApp = Application("Safari")safariApp.activate()delay(0.2) // adjust the delay as needed
//--- Issue the COPY Command ---seApp.keystroke('c', { using: 'command down' }) // Press ⌘C delay(0.2) // adjust the delay as needed
//--- Get the Text on the Clipboard ---var clipStr = app.theClipboard()console.log(clipStr)
//--- Display Alert if NO Selection was Made ---if (clipStr === "[NONE]") { var msgStr = "NO Selection was made" console.log(msgStr) app.activate() app.displayAlert(msgStr)}


Related Topics



Leave a reply



Submit