实施“鼠标跟随焦点";在MacOS上 [英] Implement "Mouse Follows Focus" on MacOS
问题描述
我有多台监视器,并且在应用程序之间移动时,通常可以通过键盘更快地完成此操作,因此我倾向于通过键盘快捷键来进行大部分窗口管理/导航.但是,有时需要使用鼠标.我的问题是,当我改变活动窗口时,我的鼠标可以在我当前的任何屏幕上,所以我必须先找到它,然后将点拖到我需要点击的地方.
I have multiple monitors, and when moving between apps, it is normally quicker to do that via the keyboard, so I tend to do most of my window management/navigation via keyboard shortcuts. However, there are times when the mouse is necessary. My problem is, that when I change the active window, my mouse can be on any of my current screens, so I have to find it first, and then drag the point to where I need to click.
我想要什么:
A)当我通过cmd-Tab切换活动窗口时,请将鼠标移至活动窗口的中心.
B)有键盘快捷键,我可以按它快速将鼠标指针移到活动窗口
What I would like tis to either:
A) Have the mouse move to the center of the active window when I switch active windows via cmd-Tab.
B) Have a keyboard shortcut I can press to quickly call the mouse pointer to the active window if I need it
我发现许多黑客"以及分别支持FFM但我想要MFF的应用.有人知道该怎么做吗?
I have found many "hacks" and apps which individually support FFM, but I want MFF. Anyone know how to do this?
推荐答案
基本概念非常简单,尽管实现中存在一些古怪之处.以下脚本应能满足您在(A)中的要求,但要注意的是,它无法区分如何打开应用程序.只要应用程序处于活动状态(通过cmd-tab,单击停靠图标,双击应用程序图标等),它将运行,并且如果没有打开的窗口或发生其他一些错误情况,它将以静默方式失败.
The basic concept is simple enough, though there are quirks in the implementation. The following script should do what you ask for in (A), with the caveat that it can't distinguish how an app is opened. It will operate whenever an app becomes active — through cmd-tab, a click on the dock icon, a double-click on the app icon, etc. — and will fail silently if there are no open windows or some other error condition occurs.
将以下脚本复制到脚本编辑器中,并将其另存为保持打开状态的应用程序(从文件格式"下拉菜单中选择应用程序",然后单击在运行处理程序后保持打开状态"复选框).您需要在安全性首选项中为其授予可访问性"权限,并且每次编辑脚本应用程序时都必须重新打开这些权限.
Copy the following script into the Script Editor, and save it as an stay-open application (select 'Application' from the File Format pulldown menu, and click the 'stay open after run handler' checkbox). You'll need to give it Accessibility permissions in security preferences, and you'll have to toggle those permissions off and on again every time you edit the script application.
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)-键盘快捷键路线-也是可行的,但这是有些不同的方法.询问您是否需要帮助以实现它.
Launch the script and leave it running in the background. Every time you switch apps, it will center the cursor in the first open window of the application. If you prefer option (B) — the keyboard shortcut route — that's also doable, but it's a bit different approach. Ask if you need help implementing it.
这篇关于实施“鼠标跟随焦点";在MacOS上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!