Skip to content

Instantly share code, notes, and snippets.

@JamesHopbourn
Last active March 27, 2025 01:27
Show Gist options
  • Save JamesHopbourn/2f0548d8ca0c48bcf301a5eb32c86fa7 to your computer and use it in GitHub Desktop.
Save JamesHopbourn/2f0548d8ca0c48bcf301a5eb32c86fa7 to your computer and use it in GitHub Desktop.

imgcopy & imgpaste

通过命令行复制与粘贴图片到 macOS 剪贴板的 Swift 工具集。

项目简介

本项目提供两个 Swift 编写的 CLI 工具:

  • imgcopy:将图片文件或标准输入的图像数据复制到系统剪贴板。
  • imgpaste:从系统剪贴板中获取图像数据并以数据流的形式输出。

适用于在 macOS 上进行图像处理、脚本自动化或提升终端工作效率的场景。

使用场景举例

  • 将本地或网络图片快速复制到剪贴板,用于粘贴到微信、Slack、浏览器等。
  • 编写自动化截图、上传、分享脚本。
  • 从剪贴板提取图片做图像识别、OCR 或二维码扫描处理。
  • 在本机与远程 Mac 之间通过 SSH 跨设备复制图像。

安装方法

确保你已安装 Swift 编译器(Xcode Command Line Tools):

xcode-select --install

编译方式:

./build.sh

编译完成后会生成两个可执行文件:

  • imgcopy
  • imgpaste

可将它们复制到系统 PATH 中:

sudo cp imgcopy imgpaste /usr/local/bin/

使用示例

以下是常见用法示例,展示如何结合 imgcopyimgpaste 进行图像复制与提取操作。


1. 通过标准输入将本地 JPEG 文件复制到剪贴板

cat result.jpeg | imgcopy -

等价于 imgcopy result.jpeg,适用于需要从管道中传输图像数据的场景。


2. 直接将本地 PNG 文件复制到剪贴板

imgcopy result.png

适用于常规图像文件快速复制到剪贴板。


3. 将在线图片通过 URL 下载后直接复制到剪贴板

curl 'https://images.unsplash.com/photo-1569158049406-6dc6f71ccd48' | imgcopy -

无需保存图片,直接将网络图片复制到剪贴板。


4. 从剪贴板中粘贴图像并保存为 PNG 文件

imgpaste > result.png

将当前剪贴板中的图像保存到 result.png 文件中。


5. 从剪贴板中提取图像并进行二维码识别

imgpaste | zbarimg -

适用于扫码类脚本,从剪贴板中提取图像后直接分析二维码/条形码。


6. 从本机剪贴板读取图像并通过 SSH 复制到远程主机的剪贴板

imgpaste | ssh [email protected] '/usr/local/bin/imgcopy -'

实现跨设备复制图片到远程 macOS 主机的剪贴板(需双方都安装本工具)。

swiftc -o imgcopy imgcopy.swift
swiftc -o imgpaste imgpaste.swift
import Cocoa
func copyToClipboard(from path: String) -> Bool {
var image: NSImage?
if path == "-" {
// 从标准输入读取
let inputData = FileHandle.standardInput.readDataToEndOfFile()
image = NSImage(data: inputData)
} else {
image = NSImage(contentsOfFile: path)
}
guard let validImage = image else {
print("Failed to load image.")
return false
}
let pasteboard = NSPasteboard.general
pasteboard.clearContents()
return pasteboard.writeObjects([validImage])
}
// MARK: - Main
if CommandLine.argc < 2 {
print("""
Usage:
Copy file to clipboard:
./imgcopy path/to/image.png
Copy stdin to clipboard:
cat image.png | ./imgcopy -
""")
exit(EXIT_FAILURE)
}
let path = CommandLine.arguments[1]
let success = copyToClipboard(from: path)
exit(success ? EXIT_SUCCESS : EXIT_FAILURE)
import Cocoa
func pasteFromClipboard() -> Data? {
let pasteboard = NSPasteboard.general
guard let image = pasteboard.readObjects(forClasses: [NSImage.self], options: nil)?.first as? NSImage else {
return nil
}
guard let tiffData = image.tiffRepresentation,
let bitmap = NSBitmapImageRep(data: tiffData),
let pngData = bitmap.representation(using: .png, properties: [:]) else {
return nil
}
return pngData
}
// MARK: - Main
if let imageData = pasteFromClipboard() {
FileHandle.standardOutput.write(imageData)
exit(EXIT_SUCCESS)
} else {
fputs("No valid image found in clipboard.\n", stderr)
exit(EXIT_FAILURE)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment