macos python可以将文本发送到Mac剪贴板吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1825692/
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
Can python send text to the Mac clipboard
提问by David Sykes
I'd like my python program to place some text in the Mac clipboard.
我希望我的 python 程序在 Mac 剪贴板中放置一些文本。
Is this possible?
这可能吗?
采纳答案by mavnn
New answer:
新答案:
This pagesuggests:
此页面建议:
Implementation for All Mac OS X Versions
The other Mac module (MacSharedClipboard.py, in Listing 4) implements the clipboard interface on top of two command-line programs called pbcopy (which copies text into the clipboard) and pbpaste (which pastes whatever text is in the clipboard). The prefix "pb" stands for "pasteboard," the Mac term for clipboard.
所有 Mac OS X 版本的实现
另一个 Mac 模块(MacSharedClipboard.py,在清单 4 中)在两个名为 pbcopy(将文本复制到剪贴板)和 pbpaste(粘贴剪贴板中的任何文本)的命令行程序之上实现剪贴板接口。前缀“pb”代表“粘贴板”,这是剪贴板的 Mac 术语。
Old answer:
旧答案:
Apparently so:
显然是这样:
http://code.activestate.com/recipes/410615/
http://code.activestate.com/recipes/410615/
is a simple script demonstrating how to do it.
是一个简单的脚本,演示了如何做到这一点。
Edit: Just realised this relies on Carbon, so might not be ideal... depends a bit what you're using it for.
编辑:刚刚意识到这依赖于碳,所以可能并不理想......取决于你使用它的目的。
回答by David Foster
How to write a Unicode string to the Mac clipboard:
如何将 Unicode 字符串写入 Mac 剪贴板:
import subprocess
def write_to_clipboard(output):
process = subprocess.Popen(
'pbcopy', env={'LANG': 'en_US.UTF-8'}, stdin=subprocess.PIPE)
process.communicate(output.encode('utf-8'))
How to read a Unicode string from the Mac clipboard:
如何从 Mac 剪贴板读取 Unicode 字符串:
import subprocess
def read_from_clipboard():
return subprocess.check_output(
'pbpaste', env={'LANG': 'en_US.UTF-8'}).decode('utf-8')
Works on both Python 2.7 and Python 3.4.
适用于 Python 2.7 和 Python 3.4。
回答by user805627
A simple way:
一个简单的方法:
cmd = 'echo %s | tr -d "\n" | pbcopy' % str
os.system(cmd)
A cross-platform way:
https://stackoverflow.com/a/4203897/805627
一种跨平台的方式:https:
//stackoverflow.com/a/4203897/805627
from Tkinter import Tk
r = Tk()
r.withdraw()
r.clipboard_clear()
r.clipboard_append('i can has clipboardz?')
r.destroy()
回答by FabienAndre
The following code use PyObjC (http://pyobjc.sourceforge.net/)
以下代码使用 PyObjC ( http://pyobjc.sourceforge.net/)
from AppKit import NSPasteboard, NSArray
pb = NSPasteboard.generalPasteboard()
pb.clearContents()
a = NSArray.arrayWithObject_("hello world")
pb.writeObjects_(a)
As explained in Cocoa documentation, copying requires three step :
- get the pasteboard
- clear it
- fill it
- 拿到剪贴板
- 清除它
- 填充
You fill the pasteboard with an array of object (here a
contains only one string).
你用一组对象填充粘贴板(这里a
只包含一个字符串)。
回答by AtomicBen
I know this is an older post, but I have found a very elegant solution to this problem.
我知道这是一篇较旧的帖子,但我找到了一个非常优雅的解决方案。
There is a library named PyClip, which can be found at https://github.com/georgefs/pyclip-copycat.
有一个名为PyClip的库,可以在https://github.com/georgefs/pyclip-copycat找到。
The syntax is pretty simple (example from the Github repo):
语法非常简单(来自 Github 存储库的示例):
import clipboard
# copy some text to the clipboard
clipboard.copy('blah blah blah')
# get the text currently held in the clipboard
text = clipboard.paste()
once you've passed clipboard.copy('foo')
you can just cmd + vto get the text
一旦你通过clipboard.copy('foo')
你就可以cmd + v来获取文本
回答by jellyfishtree
if you just wanted to put text into the mac clipboard, you could use the shell's pbcopy command.
如果您只想将文本放入 mac 剪贴板,则可以使用 shell 的 pbcopy 命令。