如何通过键盘快捷键将鼠标定位到指定应用(如Spotify)所在位置
解决方案:通过AppleScript + Karabiner实现鼠标精准定位
一、快速定位鼠标到Spotify界面
步骤1:编写定位脚本
创建AppleScript脚本,自动获取Spotify窗口位置并将鼠标移至窗口中心:
tell application "Spotify" activate set winBounds to bounds of front window set winX to item 1 of winBounds set winY to item 2 of winBounds set winCenterX to winX + (item 3 of winBounds - winX) / 2 set winCenterY to winY + (item 4 of winBounds - winY) / 2 end tell tell application "System Events" set position of mouse to {winCenterX, winCenterY} end tell
将脚本保存为mouse-to-spotify.scpt,存放至~/Library/Scripts/目录。
步骤2:Karabiner绑定快捷键
- 打开Karabiner-Elements,进入「Complex Modifications」
- 添加自定义规则,通过
shell_command执行脚本:osascript ~/Library/Scripts/mouse-to-spotify.scpt - 绑定专属快捷键(比如
Ctrl+Opt+Cmd+S)
二、定位到特定网站的Chrome窗口
针对不同网站的Chrome窗口,通过标题匹配实现精准定位:
步骤1:编写网站专属脚本
以定位Google Docs窗口为例,脚本如下:
tell application "Google Chrome" set targetWin to first window whose title contains "Google Docs" activate targetWin set winBounds to bounds of targetWin set winX to item 1 of winBounds set winY to item 2 of winBounds set winCenterX to winX + (item 3 of winBounds - winX) / 2 set winCenterY to winY + (item 4 of winBounds - winY) / 2 end tell tell application "System Events" set position of mouse to {winCenterX, winCenterY} end tell
可根据需求修改title contains后的关键词(比如GitHub、Notion),保存为对应名称的.scpt文件(如mouse-to-github.scpt)。
步骤2:Karabiner绑定对应快捷键
重复Spotify的绑定流程,为每个网站脚本分配独立快捷键(比如Ctrl+Opt+Cmd+D对应Docs,Ctrl+Opt+Cmd+G对应GitHub)。
关键注意事项
- 需在「系统设置」→「隐私与安全性」→「辅助功能」中,给Karabiner-Elements和Script Editor授予权限,否则脚本无法控制窗口与鼠标。
- 若目标应用处于最小化状态,脚本会自动激活并恢复窗口后再移动鼠标。
- Chrome窗口标题匹配可根据实际情况调整为
title starts with或title is,提升精准度。
内容的提问来源于stack exchange,提问作者WeaZeLb0y
相关产品推荐
相关产品推荐

