在 MacOS 上实现"Mouse Follows Focus"



我有多个显示器,在应用程序之间移动时,通常通过键盘更快,所以我倾向于通过键盘快捷键进行大部分窗口管理/导航。但是,有时鼠标是必要的。我的问题是,当我更改活动窗口时,我的鼠标可以在我当前的任何屏幕上,所以我必须首先找到它,然后将点拖动到需要单击的位置。

我想要的是:
A(当我通过cmd选项卡切换活动窗口时,让鼠标移动到活动窗口的中心
B(有一个键盘快捷键,如果我需要,我可以按它快速调用鼠标指针到活动窗口

我发现了许多";黑客;以及单独支持FFM的应用程序,但我想要MFF。有人知道怎么做吗?

基本概念很简单,尽管在实现中有一些怪癖。下面的脚本应该按照你在(A(中的要求执行,但需要注意的是,它无法区分应用程序是如何打开的。每当应用程序激活时,它就会运行——通过cmd选项卡、单击dock图标、双击应用程序图标等——如果没有打开的窗口或出现其他错误情况,它将以静默方式失败。

将以下脚本复制到脚本编辑器中,并将其另存为保持打开的应用程序(从"文件格式"下拉菜单中选择"应用程序",然后单击"运行后保持打开处理程序"复选框(。您需要在安全首选项中为其授予辅助功能权限,并且每次编辑脚本应用程序时都必须切换这些权限。

use AppleScript version "2.4"
use framework "AppKit"
use scripting additions
property NSWorkspace : class "NSWorkspace"
on run
set workSp to NSWorkspace's sharedWorkspace()
set notifCent to workSp's notificationCenter()
tell notifCent to addObserver:me selector:"activeAppHasChanged:" |name|:"NSWorkspaceDidActivateApplicationNotification" object:(missing value)
end run
on idle
-- we don't use the idle loop, so tell the system let the app sleep. this comes out of idle once an hour
return 3600
end idle
on activeAppHasChanged:notif
set targetApp to (notif's userInfo's valueForKey:"NSWorkspaceApplicationKey")
set targetAppName to (targetApp's localizedName) as text
tell application "System Events"
tell process targetAppName
try
set {xpos, ypos} to position of first window
set {w, h} to size of first window
my mouseMove(xpos + w / 2, ypos + h / 2)
end try
end tell
end tell
end activeAppHasChanged:
on mouseMove(newX, newY)
do shell script "

/usr/bin/python <<END
from Quartz.CoreGraphics import CGEventCreateMouseEvent
from Quartz.CoreGraphics import CGEventPost
from Quartz.CoreGraphics import kCGMouseButtonLeft
from Quartz.CoreGraphics import kCGHIDEventTap
from Quartz.CoreGraphics import kCGEventMouseMoved
import time
def mouseEvent(posx, posy):
theEvent = CGEventCreateMouseEvent(None, kCGEventMouseMoved, (posx,posy), kCGMouseButtonLeft)
CGEventPost(kCGHIDEventTap, theEvent)
mouseEvent(" & newX & "," & newY & ");
END"
end mouseMove

启动脚本并使其在后台运行。每次切换应用程序时,它都会将光标集中在应用程序的第一个打开窗口中。如果您更喜欢选项(B(——键盘快捷方式路线——这也是可行的,但方法有点不同。询问您是否需要帮助来实现它。

最新更新