可以pbpaste访问Mac OSX上的屏幕截图



在Mac OSX上,用户使用:cmd-option-shift-4将屏幕捕获到粘贴板(剪贴板)。这个存储在哪个纸板中?pbpaste可以访问吗?

命令行工具pbpaste允许访问粘贴板。我尝试了该命令的所有变体,但从未从屏幕截图中生成输出(但是,如果粘贴到Preview中,则会输出屏幕截图)。

pbpaste [-help] [-pboard {general | ruler | find | font}] [-Prefer {txt | rtf | ps}]

我尝试了-pboard和-Prefer值的每种排列,但没有运气。

预期用途是这样的脚本:

bash脚本:

#/bin/bash
pbpaste > /tmp/tmp.png
tesseract /tmp/tmp.png /tmp/tmp -l eng #open source ocr tool
cat /tmp/tmp.txt                       #tool adds .txt
编辑:

对于命令pbpaste,我接受的答案是正确的。但是,我找到了我要找的东西。一个名为pngpaste的命令。我通过brew安装了这个,以防其他人最终需要它。

那么,png剪贴板中的ocr现在是这样工作的:

#!/bin/bash
#https://github.com/jcsalterego/pngpaste
pngpaste /tmp/tmp.png
#open source ocr tool
tesseract /tmp/tmp.png /tmp/tmp -l eng
#tesseract adds .txt
cat /tmp/tmp.txt | pbcopy     
                                

For @ELLIOTTCABLE:)


对于命令pbpaste,我接受的答案是true。但是,我找到了我要找的东西。一个名为pngpaste的命令。我通过brew安装了这个,以防其他人最终需要它。

那么,png剪贴板中的ocr现在是这样工作的:

#!/bin/bash
#https://github.com/jcsalterego/pngpaste
pngpaste /tmp/tmp.png
#open source ocr tool
tesseract /tmp/tmp.png /tmp/tmp -l eng
#tesseract adds .txt
cat /tmp/tmp.txt | pbcopy     

No。pbpaste只能从粘贴板中检索纯文本、EPS或RTF数据。截图不属于这些类型,所以pbpaste无法访问它。

更新答案

从你的问题和评论来看,你想要做的事情的大意是:

  • 标记屏幕区域
  • OCR它
  • 将OCR文本放入剪贴板

如果我们进入更详细的…您需要将屏幕区域复制到剪贴板中,将其粘贴到/tmp中的文件中,运行tesseract,并将tesseract输出放入剪贴板中。

现在,这是行不通的,因为,正如@duskwuff所说,pbpaste不支持图形,所以你的问题现在变成了,大概,如何在不使用pbpaste的情况下获得屏幕截图到文件。在我看来,你有两个选择…

选项1

编写一个脚本,调用screencapture并将屏幕捕获到一个文件和ocr。脚本如下所示:

#/bin/bash
screencapture -i /tmp/tmp.png
tesseract /tmp/tmp.png /tmp/tmp -l eng
pbcopy < /tmp/tmp.txt

选项2

编写一个脚本,持续监控/tmp的新屏幕截图,当一个新的到达时,OCR它,并在剪贴板中填充它。要使用这种方法,您需要使用我的原始答案中的两个命令来强制所有屏幕截图转到/tmp。您的脚本将看起来像这样

#!/bin/bash
fswatch /tmp | while read file; do tesseract ...; pbcopy < ...; done

这两个方法的主要区别在于调用它们的方式。选项1要求您运行一个脚本或双击一个脚本来启动捕获过程。选项2要求您只需按下cmd-option-shift-4并标记屏幕,然后OCR自动发生。

原始回答

不知道你到底想做什么,但你可以通过发出这两个命令使屏幕截图以PNG格式保存在/tmp中;

defaults write com.apple.screencapture type png
defaults write com.apple.screencapture location /tmp

您需要在进行这些更改后重新启动GUI

killall SystemUIServer

我认为你不需要访问剪贴板。修改咆哮。

  1. 更改截图文件夹。 defaults write com.apple.screencapture location ~/screens/ killall SystemUIServer
  2. 复制ss2CB.plist~/Library/LaunchAgents/
  3. 复制ss2cb.py~/
  4. Cmd + Shift + 4 &粘贴!
  5. 完成了!

ss2CB.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Disabled</key>
    <false/>
    <key>Label</key>
    <string>copy Screen Shots to ClipBoard</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/bin/python</string>
        <string>~/ss2cb.py</string>
    </array>
    <key>WatchPaths</key>
    <array>
        <string>~/screens/</string>
    </array>
</dict>
</plist>

ss2cb.py

#! /usr/bin/python
import pygtk
pygtk.require('2.0')
import gtk
import os
import sys
def copy_image(f):
    assert os.path.exists(f), "file does not exist"
    image = gtk.gdk.pixbuf_new_from_file(f)
    clipboard = gtk.clipboard_get()
    clipboard.set_image(image)
    clipboard.store()
def all_files_under(path):
    path = os.path.expanduser(path);
    cur_path = path
    for filename in os.listdir(path):
        yield os.path.join(cur_path, filename)
if len(sys.argv) < 2:
    file = max(all_files_under('~/screens/'), key=os.path.getctime)
else:
    file = sys.argv[1]
copy_image(file);
print 'File: "' + file + '" Copied'

您也可以使用更少的命令并且不创建.txt文件:

pngpaste /tmp/tmp.png
tesseract /tmp/tmp.png - -l eng | pbcopy

最新更新