使用单个Python脚本从Linux,Mac和Windows上的剪贴板复制数据 [英] Copy data from the clipboard on Linux, Mac and Windows with a single Python script

查看:474
本文介绍了使用单个Python脚本从Linux,Mac和Windows上的剪贴板复制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这应该在Linux所有版本(我假设是Ubuntu),Mac OS所有版本和Windows所有版本上都有效.我不确定32位和64位系统在剪贴板上访问数据的方式是否不同,如果确实如此,那么仅对32位版本进行这项工作是可以放心的,因此运行64位版本的用户可以使用32位版本版本的操作系统.

除了必须在提到的OS上工作之外,棘手的部分是,我希望脚本能够运行,只要用户不停止它并且在运行由用户复制到剪贴板的所有数据时正在复制到列表或文本文件或字符串/数组/变量中.

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

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

问题是,这是一个更大的项目的一部分,我想在Linux,Mac和Windows上进行工作,因此涵盖了这三个选项,然后使用可在上述OS上使用的Python代码进行其余操作脚本/项目的理想选择.我是否对这个脚本要求在Linux,Mac和Windows上正常工作的要求过高?

解决方案

您可以使用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开始就提供OS支持.在Linux上,Python脚本可能由分发包管理器中的Python安装执行,其位数将与系统匹配,因此您不必为此担心.

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?

解决方案

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.

这篇关于使用单个Python脚本从Linux,Mac和Windows上的剪贴板复制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆