Files
karuo-ai/01_卡资(金)/金仓_存储备份/脚本/handy_macos_asr_paste.sh
2026-07-19 00:21:46 +08:00

44 lines
1.4 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# Handy 0.8.3 ExternalScript$1 接收转写;脚本负责把最终文本输入焦点框。
set -euo pipefail
KARUO_AI_ROOT="${KARUO_AI_ROOT:-/Users/karuo/Documents/个人/卡若AI}"
APPLY_PY="${KARUO_AI_ROOT}/运营中枢/工作台/脚本/apply_karuo_voice_corrections.py"
LOG_DIR="$HOME/Library/Logs/com.pais.handy"
PLUGIN_LOG="$LOG_DIR/karuo_asr_plugin.log"
RAW="${1:-}"
mkdir -p "$LOG_DIR"
if [[ -z "${RAW//[[:space:]]/}" ]]; then
printf '%s missing-argument\n' "$(date '+%F %T')" >>"$PLUGIN_LOG"
exit 2
fi
if [[ -f "$APPLY_PY" ]]; then
CORRECTED="$(python3 "$APPLY_PY" "$RAW")"
CORRECTED="${CORRECTED%$'\n'}"
else
CORRECTED="$RAW"
fi
printf '%s' "$CORRECTED" | pbcopy
# Handy浮层关闭和原应用恢复焦点需要短暂时间。
sleep "${KARUO_ASR_FOCUS_DELAY:-0.35}"
if [[ "${KARUO_ASR_TEST_ONLY:-0}" != "1" ]]; then
if [[ -x /usr/local/bin/cliclick ]]; then
PASTE_CMD=(/usr/local/bin/cliclick kd:cmd t:v ku:cmd)
else
PASTE_CMD=(osascript -e 'tell application "System Events" to keystroke "v" using command down')
fi
if ! "${PASTE_CMD[@]}" >>"$PLUGIN_LOG" 2>&1; then
printf '%s paste-failed\n' "$(date '+%F %T')" >>"$PLUGIN_LOG"
exit 3
fi
fi
printf '%s ok raw_len=%s corrected_len=%s changed=%s\n' \
"$(date '+%F %T')" "${#RAW}" "${#CORRECTED}" "$([[ "$RAW" == "$CORRECTED" ]] && echo 0 || echo 1)" \
>>"$PLUGIN_LOG"