Gets the user selected text ? #5624
-
How to get user selected text in other app ? For example, select text in the browser 。 |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 10 replies
-
I don't think that this is possible at all. At least not if you don't target a specific app where you have access to stuff like this. Apps and gui frameworks generally draw this themselves without necessarily telling the OS that text is selected or providing an API that other apps can call to get selected text. The only thing that could be possible is to take a screenshot of the display and analyze that - this will very likely be really difficult, with many false detections. In other words: Not possible. |
Beta Was this translation helpful? Give feedback.
-
For macOS, I've worked around having to install an extension by using AppleScript: import { readText, writeText } from "@tauri-apps/api/clipboard";
import { register, isRegistered } from "@tauri-apps/api/globalShortcut";
import { Command } from "@tauri-apps/api/shell";
const script = `
tell application "System Events"
keystroke "c" using {command down}
end tell
`;
const args = script
.trim()
.split("\n")
.flatMap((line) => ["-e", line]);
const command = new Command("osascript", args);
async function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function getSelectedText() {
const initialClipboardContent = await readText();
try {
await command.spawn();
await new Promise((resolve) => {
command.on("close", resolve);
});
let attempts = 10;
let currentClipboardContent = initialClipboardContent;
while (
attempts > 0 &&
currentClipboardContent === initialClipboardContent
) {
await sleep(100);
currentClipboardContent = await readText();
attempts--;
}
return currentClipboardContent;
} finally {
if (initialClipboardContent) {
await writeText(initialClipboardContent);
}
}
}
async function main() {
const shortcut = "Command+E";
if (!(await isRegistered(shortcut))) {
await register(shortcut, async () => {
const text = await getSelectedText();
document.body.innerHTML = text || "—";
});
}
}
main(); |
Beta Was this translation helpful? Give feedback.
I don't think that this is possible at all. At least not if you don't target a specific app where you have access to stuff like this.
Apps and gui frameworks generally draw this themselves without necessarily telling the OS that text is selected or providing an API that other apps can call to get selected text.
The only thing that could be possible is to take a screenshot of the display and analyze that - this will very likely be really difficult, with many false detections.
In other words: Not possible.