使用单个 Python 脚本从 Linux、Mac 和 Windows 上的剪贴板复制数据

发布于 2024-12-11 19:28:40 字数 654 浏览 0 评论 0原文

我正在尝试用 Python 创建一个脚本,该脚本将收集用户放入剪贴板的数据,并最好将其保存为列表或文本文件或字符串/数组/变量以便稍后使用。

这应该适用于 Linux 所有版本(我假设是 Ubuntu)、Mac OS 所有版本和 Windows 所有版本。我不确定 32 位和 64 位系统是否有不同的方式访问剪贴板中的数据,如果有的话,我想仅对 32 位版本进行此操作是安全的,这样运行 64 位版本的人就可以回退到 32 位版本操作系统的版本。

除了必须在上述操作系统上工作之外,棘手的部分是,只要用户不停止脚本,我就希望脚本运行,并且在运行时,用户复制到剪贴板的所有数据都将被复制到列表或文本文件或字符串/数组/变量中。

当然,用户可以将数据输入到剪贴板中是有时间限制的,所以我正在考虑每秒或每 500 毫秒循环扫描剪贴板,检查内容是否已更改,如果已更改,则复制它,否则请勿复制。

是否有一种统一的方法或模块可以在所有不同的操作系统上执行此操作,或者为不同的操作系统编写单独的脚本来执行此任务会更好吗?

问题是,这是一个更大项目的一部分,我想在 Linux、Mac 和 Windows 上工作,因此涵盖这三个选项,然后使用可在上述操作系统中使用的 Python 代码来完成脚本的其余部分/project 将是理想的选择。我是否对这个脚本要求太多,要求它必须在 Linux、Mac 和 Windows 上运行?

I am trying to create a script in Python that will collect data put in the clipboard by the user and preferably save it as a list or in a text file or string/array/variable to work with later on.

This should work on Linux all versions (I would assume Ubuntu), Mac OS all versions and Windows all versions. I am not sure if 32bit and 64bit systems have different ways of accessing the data at the clipboard, if they do I guess it would be safe to make this work for the 32bit versions only so people running the 64bit versions can fall back onto the 32bit version of the OS.

The tricky part, apart from this having to work on the mentioned OS, is that I would like the script to run as long as the user does not stop it and while it runs all the data copied into the clipboard by the user is being copied to a list or in a text file or string/array/variable.

Of course there is a time limit at which the user can input data into the clipboard so I was thinking of having a loop scanning the clipboard every second or every 500 milliseconds, check if the content has changed, and if it has, copy it, otherwise don't copy it.

Is there a unified way or module that does this on all different OS or would it be better to write separate scripts for this task for the various OS?

The thing is, this is part of a bigger project that I would like to make work on Linux, Mac and Windows, so having those three options covered and then use Python code that can be used across the mentioned OS for the rest of the script/project would be ideal. Am I asking too much in general from this script concerning it having to work on Linux, Mac and Windows?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

痴骨ら 2024-12-18 19:28:40

xerox 库支持 Linux、Mac OS X 和 Windows。

请注意,在短时间(小于一分钟)的时间间隔内执行任何操作都是一个非常糟糕的主意,因为这会使现代处理器定期唤醒。您可能希望在剪贴板更改后使用相应操作系统的 API 来注册回调。

The xerox library supports Linux, Mac OS X, and Windows.

Note that it's a very bad idea to perform any action in short (< a minute) intervals, because that makes modern processors wake up regularily. You may want to use the respective operating system's APIs to register a callback once the clipboard changes.

长途伴 2024-12-18 19:28:40

您可能最好使用比 Tk 更高级的 gui 工具包,但它位于标准库中,因此随处可用。

举一个非常简单的例子:

import Tkinter
root = Tkinter.Tk()
root.withdraw() # Hide the main window (optional)
text_in_clipboard = root.clipboard_get()
print text_in_clipboard

You're probably better off using a more advanced gui toolkit than Tk, but it's in the standard library, so it's available everywhere.

As a really simple example:

import Tkinter
root = Tkinter.Tk()
root.withdraw() # Hide the main window (optional)
text_in_clipboard = root.clipboard_get()
print text_in_clipboard
养猫人 2024-12-18 19:28:40

您可以使用 Qt 等 GUI 工具包来获取便携式剪贴板 API。也就是说,仅仅为此而使用整个 GUI 工具包可能有点过分了。 (当然,除非您也将使用它来制作 GUI。)

也就是说,处理纯文本的剪贴板 API 应该相当简单,可以进行您自己的抽象。

例如,在 OS X 上,您可以使用 PyObjC (与 OS X 一起安装)来获取纯文本剪贴板的内容:

from AppKit import NSPasteboard
from LaunchServices import 
pb = NSPasteboard.generalPasteboard()
text = pb.stringForType_(kUTTypeUTF8PlainText)

CPU 架构

64 位操作系统上的 32 位本机应用程序将访问与 64 位操作系统相同的剪贴板。如果您需要支持操作系统的两种体系结构,并且不编写驱动程序,那么对于 Windows,可以提供 32 位二进制文​​件;对于 Linux,您可能需要同时使用这两个版本;对于 OS X,发布 64 位版本应该相当安全,自 2007 年中期以来的所有 Mac 都配备了 64 位 CPU,并且自 Leopard 以来就支持操作系统。在 Linux 上,Python 脚本可能由发行包管理器中的 Python 安装执行,其位数将与系统匹配,因此您不一定需要担心这一点。

You could use a GUI toolkit such as Qt to get a portable clipboard API. That said it might be a little overkill to use a whole GUI toolkit just for this. (Unless, of course, you're also going to use it to make a GUI.)

That said, clipboard APIs dealing with plain text should be reasonably simple to make your own abstraction over.

For instance, on OS X you can use PyObjC (which is installed along with OS X) to get plain-text contents of a clipboard:

from AppKit import NSPasteboard
from LaunchServices import 
pb = NSPasteboard.generalPasteboard()
text = pb.stringForType_(kUTTypeUTF8PlainText)

CPU architectures

A 32-bit native app on a 64-bit OS will be accessing the same clipboard as a 64-bit one. If you need to support both architectures of an OS, and aren't writing a driver, for Windows it's okay to ship a 32-bit binary; for Linux you'll likely have to do both versions; for OS X, it should be reasonably safe to ship a 64-bit version, all Macs since mid-2007 have had 64-bit CPUs and the OS support is there since Leopard. A Python script will, on Linux, probably be executed by a Python installation from the distribution package manager, whose bitness will match the system, so you don't necessarily need to worry about that.

梦毁影碎の 2024-12-18 19:28:40

轮询并不稳健/可靠。

如果不将数据粘贴到缓冲区进行检查,则无法确定数据是否已更改(无论如何在 Windows 上)。这需要打开剪贴板。如果您循环执行此操作,则会与其他应用程序发生冲突。即用户将另一个项目复制到剪贴板的应用程序。这将引发“无法打开剪贴板”或“内存不足”错误。这种方法不会可靠/稳健地工作。您需要在各种平台中使用正确的剪贴板监控 API。

Polling is NOT robust/reliable.

You cannot determine if data has changed (on windows anyway) without pasting it into a buffer for inspection. This requires opening the clipboard. If you do this in a loop, you're going to collide with other apps. i.e. the app where the user is copying another item onto the clipboard. This will explode with an "cannot open clipboard" or "out of memory" error. This approach will not work reliably/robustly. You need to use proper clipboard monitoring APIs in the various platforms.

ˇ宁静的妩媚 2024-12-18 19:28:40

我怀疑还可以通过以下链接使用 pythonnet + TextCopy 库以平台中立的方式复制剪贴板:

http://pythonnet。 github.io/

https://stackoverflow.com/a/51912933/2338477

(此处查看类似问题:Python 中的快速简单的文件对话框?

但是还没有我自己尝试过这个,可能还需要尝试一下您运行的命令类型以及从哪个控制台运行。如果有人愿意尝试这个,请告诉我,我会在这里更新更多详细信息。

I suspect it's also possible to copy clipboard in platform neutral manner using pythonnet + TextCopy library via following links:

http://pythonnet.github.io/

https://stackoverflow.com/a/51912933/2338477

(See similar problem here: Quick and easy file dialog in Python? )

But haven't tried this by myself, might need also to play around what kind of commands you run and from which console. Let me know if someone will try this, I'll update more details in here.

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