Python 中有适用于 Mac 的 sendKey 吗?
在 Mac 10.6 中,我想让一个活动的应用程序变得不活动,或者通过 Python 最小化
我知道我可以在 Windows 中使用 Python 来使用 sendKey,那么在 Mac 中呢?
In Mac 10.6, I want to cause an active application to become de-active, or minimized by Python
I know I could use sendKey in Windows with Python, then what about in Mac?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是我从 Stack Overflow 上的另一个问题中发现的内容。它对我的问题非常有效。
Here is what I found from a different question on Stack Overflow. It works pretty good for my problem.
尝试 appscript,PyPI 中提供的 Apple 事件桥:
Try appscript, an Apple event bridge available in PyPI:
除了 Yinan 将击键发送到当前活动的应用程序之外,您还可以将其发送到特定应用程序,如下所示。像以前一样将以下内容传递给 osascript,或者将其保存到文件并将文件传递给 osascript
这将在将其带到前台后将 Cmd + r 发送到 Safari
In addition to Yinan, which will send the keystroke to the currently active application, you can send it to a specific application as follows. Pass the following to osascript as before, or save it to a file and pass the file to osascript
This will send Cmd + r to Safari after bringing it to the foreground
例如,也许您可以从 Python 运行 OSA 脚本 (man osascript) 并驱动应用程序?
Maybe you could run an OSA script (man osascript) from Python, for instance, and drive the application?
为了使已经在 Windows 上使用
pip
中的SendKeys
运行的脚本也能在 OS X 上运行,我创建了一个文件/Library/Python/2.7/site-packages /SendKeys/__init__.py
(site-packages
是pip
放置它在我的 Mac 上安装的所有内容的地方...不确定这是否可配置。)该文件的内容是:
显然它不是很健壮,所以我不会把它放在 pypi 上,但它足以让我的脚本在 OS X 和 Windows 上运行。
To make my scripts which already work on Windows using
SendKeys
frompip
also work on OS X, I made a file/Library/Python/2.7/site-packages/SendKeys/__init__.py
(site-packages
is wherepip
puts everything it installs on my Mac... not sure if that's configurable or not.)The contents of the file are:
Obviously it's not very robust, so I won't be putting it on
pypi
, but it's enough to make my scripts run on both OS X and Windows.