-
Notifications
You must be signed in to change notification settings - Fork 429
feat: add macOS send command #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
BiboyQG
wants to merge
2
commits into
jackwener:main
Choose a base branch
from
BiboyQG:codex/macos-send-command
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,94 @@ | ||||||||||||||
| use anyhow::Result; | ||||||||||||||
|
|
||||||||||||||
| #[cfg(target_os = "macos")] | ||||||||||||||
| use anyhow::{bail, Context}; | ||||||||||||||
| #[cfg(target_os = "macos")] | ||||||||||||||
| use std::process::Command; | ||||||||||||||
|
|
||||||||||||||
| #[cfg(target_os = "macos")] | ||||||||||||||
| const SEND_SCRIPT: &str = r#" | ||||||||||||||
| on run argv | ||||||||||||||
| if (count of argv) < 2 then error "chat and message are required" | ||||||||||||||
| set chatName to item 1 of argv | ||||||||||||||
| set messageText to item 2 of argv | ||||||||||||||
| set previousClipboard to missing value | ||||||||||||||
| try | ||||||||||||||
| set previousClipboard to the clipboard as text | ||||||||||||||
| end try | ||||||||||||||
| try | ||||||||||||||
| tell application id "com.tencent.xinWeChat" to activate | ||||||||||||||
| delay 0.3 | ||||||||||||||
| tell application "System Events" | ||||||||||||||
| set wxProc to first application process whose bundle identifier is "com.tencent.xinWeChat" | ||||||||||||||
| set frontmost of wxProc to true | ||||||||||||||
| delay 0.2 | ||||||||||||||
| keystroke "f" using command down | ||||||||||||||
| delay 0.1 | ||||||||||||||
| keystroke "a" using command down | ||||||||||||||
| delay 0.05 | ||||||||||||||
| my pasteText(chatName) | ||||||||||||||
| delay 1.5 | ||||||||||||||
| key code 36 | ||||||||||||||
| delay 0.8 | ||||||||||||||
| my pasteText(messageText) | ||||||||||||||
| delay 0.1 | ||||||||||||||
| key code 36 | ||||||||||||||
| end tell | ||||||||||||||
| my restoreClipboard(previousClipboard) | ||||||||||||||
| on error errorMessage number errorNumber | ||||||||||||||
| my restoreClipboard(previousClipboard) | ||||||||||||||
| error errorMessage number errorNumber | ||||||||||||||
| end try | ||||||||||||||
| end run | ||||||||||||||
|
|
||||||||||||||
| on pasteText(textValue) | ||||||||||||||
| tell application "System Events" | ||||||||||||||
| set the clipboard to textValue | ||||||||||||||
| delay 0.05 | ||||||||||||||
| keystroke "v" using command down | ||||||||||||||
| end tell | ||||||||||||||
| end pasteText | ||||||||||||||
|
|
||||||||||||||
| on restoreClipboard(previousClipboard) | ||||||||||||||
| if previousClipboard is not missing value then set the clipboard to previousClipboard | ||||||||||||||
| end restoreClipboard | ||||||||||||||
| "#; | ||||||||||||||
|
|
||||||||||||||
| #[cfg(target_os = "macos")] | ||||||||||||||
| pub fn cmd_send(chat: String, message: String) -> Result<()> { | ||||||||||||||
| if chat.trim().is_empty() { | ||||||||||||||
| bail!("聊天对象名称不能为空"); | ||||||||||||||
| } | ||||||||||||||
| if message.is_empty() { | ||||||||||||||
| bail!("消息不能为空"); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| let output = Command::new("osascript") | ||||||||||||||
| .arg("-e") | ||||||||||||||
| .arg(SEND_SCRIPT) | ||||||||||||||
| .arg(&chat) | ||||||||||||||
| .arg(&message) | ||||||||||||||
| .output() | ||||||||||||||
| .context("无法运行 osascript")?; | ||||||||||||||
|
|
||||||||||||||
| if !output.status.success() { | ||||||||||||||
| let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string(); | ||||||||||||||
| let reason = if stderr.is_empty() { | ||||||||||||||
| format!("osascript exited with status {}", output.status) | ||||||||||||||
|
||||||||||||||
| format!("osascript exited with status {}", output.status) | |
| if let Some(code) = output.status.code() { | |
| format!("osascript exited with code {}", code) | |
| } else { | |
| format!("osascript failed with status {}", output.status) | |
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
message只用is_empty()校验会放过仅包含空格/换行的内容,与 PR 描述里“验证空消息”不一致。建议改为基于message.trim()判断空白,并(可选)像chat一样将chat.trim()的结果用于传给脚本/打印,避免用户输入前后空格导致搜索不到会话。