Python 中有适用于 Mac 的 sendKey 吗?

发布于 2024-08-12 03:09:31 字数 112 浏览 4 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

你的他你的她 2024-08-19 03:09:31

这是我从 Stack Overflow 上的另一个问题中发现的内容。它对我的问题非常有效。

import os
cmd = """
osascript -e 'tell application "System Events" to keystroke "m" using {command down}' 
"""
# minimize active window
os.system(cmd)

Here is what I found from a different question on Stack Overflow. It works pretty good for my problem.

import os
cmd = """
osascript -e 'tell application "System Events" to keystroke "m" using {command down}' 
"""
# minimize active window
os.system(cmd)
电影里的梦 2024-08-19 03:09:31

尝试 appscript,PyPI 中提供的 Apple 事件桥:

from appscript import app, k
app('System Events').keystroke('N', using=k.command_down)

Try appscript, an Apple event bridge available in PyPI:

from appscript import app, k
app('System Events').keystroke('N', using=k.command_down)
被你宠の有点坏 2024-08-19 03:09:31

除了 Yinan 将击键发送到当前活动的应用程序之外,您还可以将其发送到特定应用程序,如下所示。像以前一样将以下内容传递给 osascript,或者将其保存到文件并将文件传递给 osascript

tell application "Safari"
    activate
    tell application "System Events" to keystroke "r" using {command down}
end tell

这将在将其带到前台后将 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

tell application "Safari"
    activate
    tell application "System Events" to keystroke "r" using {command down}
end tell

This will send Cmd + r to Safari after bringing it to the foreground

落在眉间の轻吻 2024-08-19 03:09:31

例如,也许您可​​以从 Python 运行 OSA 脚本 (man osascript) 并驱动应用程序?

Maybe you could run an OSA script (man osascript) from Python, for instance, and drive the application?

始于初秋 2024-08-19 03:09:31

为了使已经在 Windows 上使用 pip 中的 SendKeys 运行的脚本也能在 OS X 上运行,我创建了一个文件 /Library/Python/2.7/site-packages /SendKeys/__init__.pysite-packagespip 放置它在我的 Mac 上安装的所有内容的地方...不确定这是否可配置。)

该文件的内容是:

def SendKeys(keys):
    if keys == '{ENTER}'
        keys = 'return'
    from os import system
    system('osascript -e \'tell application "System Events" to keystroke ' + keys + "'")

显然它不是很健壮,所以我不会把它放在 pypi 上,但它足以让我的脚本在 OS X 和 Windows 上运行。

To make my scripts which already work on Windows using SendKeys from pip also work on OS X, I made a file /Library/Python/2.7/site-packages/SendKeys/__init__.py (site-packages is where pip puts everything it installs on my Mac... not sure if that's configurable or not.)

The contents of the file are:

def SendKeys(keys):
    if keys == '{ENTER}'
        keys = 'return'
    from os import system
    system('osascript -e \'tell application "System Events" to keystroke ' + keys + "'")

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文