Skip to content

Commit

Permalink
1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Pylogmon committed May 5, 2024
1 parent 5c841a5 commit 622e8f3
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 12 deletions.
15 changes: 10 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,24 @@ repository = "https://github.com/pot-app/Selection"
keywords = ["selection", "linux", "windows", "macos"]
categories = ["gui"]
license = "GPL-3.0-only"
version = "1.1.1"
version = "1.2.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
log = "0.4.20"
log = "0.4"

[target.'cfg(windows)'.dependencies]
windows = {version="0.52.0",features= ["Win32_UI_WindowsAndMessaging", "Win32_Foundation","Win32_System_Threading","Win32_UI_Input_KeyboardAndMouse","Win32_System_DataExchange","Win32_UI_Accessibility","Win32_System_Com"] }
windows = {version="0.56.0",features= ["Win32_UI_WindowsAndMessaging", "Win32_Foundation","Win32_System_Threading","Win32_UI_Input_KeyboardAndMouse","Win32_System_DataExchange","Win32_UI_Accessibility","Win32_System_Com"] }
enigo = "0.2.0"
arboard = "3.2.0"
arboard = "3.4.0"

[target.'cfg(target_os = "linux")'.dependencies]
x11-clipboard = "0.8.1"
x11-clipboard = "0.9.2"
wl-clipboard-rs = "0.8.0"

[target.'cfg(target_os = "macos")'.dependencies]
core-foundation = "0.9.4"
accessibility-ng = "0.1.6"
accessibility-sys-ng = "0.1.3"
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Get the text selected by the cursor
## Example

```toml
selection = "1.1.0"
selection = "1.2.0"
```

```rust
Expand Down Expand Up @@ -36,6 +36,7 @@ fn main() {

### MacOS

- Accessibility API
- Clipboard

### Linux
Expand Down
65 changes: 60 additions & 5 deletions src/macos.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,71 @@
use accessibility_ng::{AXAttribute, AXUIElement};
use accessibility_sys_ng::{kAXFocusedUIElementAttribute, kAXSelectedTextAttribute};
use core_foundation::string::CFString;
use log::{error, info};
use std::error::Error;

pub fn get_text() -> String {
match get_selected_text_by_ax() {
Ok(text) => {
if !text.is_empty() {
return text;
} else {
info!("get_selected_text_by_ax is empty");
}
}
Err(err) => {
error!("get_selected_text_by_ax error:{}", err);
}
}
info!("fallback to get_text_by_clipboard");
match get_text_by_clipboard() {
Ok(text) => {
if !text.is_empty() {
return text;
} else {
info!("get_text_by_clipboard is empty");
}
}
Err(err) => {
error!("{}", err)
error!("get_text_by_clipboard error:{}", err);
}
}
// Return Empty String
String::new()
}

// Copy from https://github.com/yetone/get-selected-text/blob/main/src/macos.rs
fn get_selected_text_by_ax() -> Result<String, Box<dyn Error>> {
let system_element = AXUIElement::system_wide();
let Some(selected_element) = system_element
.attribute(&AXAttribute::new(&CFString::from_static_string(
kAXFocusedUIElementAttribute,
)))
.map(|element| element.downcast_into::<AXUIElement>())
.ok()
.flatten()
else {
return Err(Box::new(std::io::Error::new(
std::io::ErrorKind::NotFound,
"No selected element",
)));
};
let Some(selected_text) = selected_element
.attribute(&AXAttribute::new(&CFString::from_static_string(
kAXSelectedTextAttribute,
)))
.map(|text| text.downcast_into::<CFString>())
.ok()
.flatten()
else {
return Err(Box::new(std::io::Error::new(
std::io::ErrorKind::NotFound,
"No selected text",
)));
};
Ok(selected_text.to_string())
}

fn get_text_by_clipboard() -> Result<String, Box<dyn Error>> {
let output = std::process::Command::new("osascript")
.arg("-e")
Expand All @@ -37,21 +87,26 @@ use scripting additions
use framework "Foundation"
use framework "AppKit"
tell application "System Events"
set frontmostProcess to first process whose frontmost is true
set appName to name of frontmostProcess
end tell
set savedAlertVolume to alert volume of (get volume settings)
-- Back up clipboard contents:
set savedClipboard to the clipboard
set thePasteboard to current application's NSPasteboard's generalPasteboard()
set theCount to thePasteboard's changeCount()
tell application "System Events"
set volume alert volume 0
end tell
-- Copy selected text to clipboard:
tell application "System Events" to keystroke "c" using {command down}
delay 0.1 -- Without this, the clipboard may have stale data.
tell application "System Events"
set volume alert volume savedAlertVolume
end tell
if thePasteboard's changeCount() is theCount then
return ""
end if
Expand Down
2 changes: 1 addition & 1 deletion src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn get_text() -> String {
// Available for Edge, Chrome and UWP
fn get_text_by_automation() -> Result<String, Box<dyn Error>> {
// Init COM
unsafe { CoInitialize(None) }?;
let _ = unsafe { CoInitialize(None) };
// Create IUIAutomation instance
let auto: IUIAutomation = unsafe { CoCreateInstance(&CUIAutomation, None, CLSCTX_ALL) }?;
// Get Focused Element
Expand Down

0 comments on commit 622e8f3

Please sign in to comment.