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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7881230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:20:47  来源:igfitidea点击:

Copy data from the clipboard on Linux, Mac and Windows with a single Python script

pythonwindowslinuxmacosclipboard

提问by lowtechsun

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.

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

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.

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

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.

当然,用户可以将数据输入剪贴板是有时间限制的,所以我想每秒钟或每 500 毫秒循环扫描剪贴板,检查内容是否已更改,如果已更改,则复制它,否则不要复制它。

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?

问题是,这是我希望在 Linux、Mac 和 Windows 上工作的一个更大项目的一部分,因此涵盖了这三个选项,然后使用可在上述操作系统中使用的 Python 代码用于脚本的其余部分/project 将是理想的。关于它必须在 Linux、Mac 和 Windows 上工作,我是否从这个脚本中提出了太多的一般要求?

采纳答案by millimoose

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.)

您可以使用诸如 Qt 之类的 GUI 工具包来获取便携式剪贴板 API。也就是说,为此使用整个 GUI 工具包可能有点矫枉过正。(当然,除非您还将使用它来制作 GUI。)

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

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

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

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

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

CPU architectures

CPU架构

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.

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

回答by phihag

The xeroxlibrary supports Linux, Mac OS X, and Windows.

施乐库支持在Linux,Mac OS X和Windows。

Note that it's a very bad idea to perform anyaction 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.

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

回答by Joe Kington

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.

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

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

回答by Chris Thornton

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.

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

回答by TarmoPikaro

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

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

http://pythonnet.github.io/

http://pythonnet.github.io/

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

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

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

(在这里看到类似的问题: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.

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