Skip to content

Instantly share code, notes, and snippets.

@softmarshmallow
Last active December 4, 2025 06:30
Show Gist options
  • Select an option

  • Save softmarshmallow/1095f9d54b84b6196a399a6a8eb0fe9f to your computer and use it in GitHub Desktop.

Select an option

Save softmarshmallow/1095f9d54b84b6196a399a6a8eb0fe9f to your computer and use it in GitHub Desktop.
macOS Clipboard inspection
#!/usr/bin/env swift
import AppKit
let pb = NSPasteboard.general
for (i, item) in (pb.pasteboardItems ?? []).enumerated() {
print("=== Item \(i) ===")
for type in item.types {
print("UTI:", type.rawValue)
guard let data = item.data(forType: type) else {
print(" <no data>")
continue
}
if let string = String(data: data, encoding: .utf8) {
print(" UTF8 String:")
print(string)
} else {
print(" <non-utf8 data, length \(data.count)>")
}
print()
}
}
@softmarshmallow
Copy link
Author

softmarshmallow commented Dec 4, 2025

macOS Clipboard Dumper

This script provides a reliable, developer‑friendly way to inspect the macOS clipboard as‑is, including custom formats, binary payloads, and non‑text pasteboard types.

Why this exists

macOS ships with pbpaste, but it only exposes plain text and a few legacy formats (RTF, PostScript). When you copy data from modern apps—Grida, Sketch, Photoshop, Chrome, VSCode, or any tool that places custom UTI clipboard formatspbpaste returns nothing.

This makes debugging clipboard‑based formats extremely difficult:

  • custom formats do not paste into text editors
  • hex editors cannot directly read the pasteboard
  • apps often store binary or gzipped data
  • many tools (e.g., Grida) write multiple clipboard types at once

This script bypasses all of those limitations by directly accessing the macOS NSPasteboard API via Swift.

What the script does

  • enumerates every clipboard item
  • lists all UTI types (e.g., public.utf8-plain-text, com.figma.clipboard, public.tiff, etc.)
  • retrieves their raw data
  • attempts to print them as UTF‑8 text
  • falls back to safe escaped output for non‑UTF‑8 binary

This reveals clipboard contents exactly as the originating application placed them.

When this is useful

This script is particularly helpful when:

● Reverse‑engineering clipboard formats

Apps like Grida, Sketch, or internal tools often store rich structured data in the clipboard. This script lets you inspect the raw bytes.

● Debugging custom UTI pasteboard entries

If your app uses custom pasteboard types (e.g., com.myapp.node), this tool shows:

  • whether your type was actually written
  • size of the payload
  • whether it contains text or binary

● Working with binary or compressed clipboard data

Many apps compress their clipboard payloads (e.g., gzip). Inspecting and extracting the raw bytes is necessary for:

  • decompressing
  • decoding
  • analyzing JSON payloads

● Verifying what browsers and Electron apps put on the clipboard

Web apps may populate multiple formats: text/html, text/plain, application/json, or vendor‑prefixed formats.

● Developing OS‑level integrations or automation tools

When building automation, internal tools, or plugin systems, understanding clipboard structure is essential.

Summary

This script solves a real gap in macOS developer tooling: printing the complete clipboard, including custom types and binary formats, in a single command.

It is lightweight, requires no dependencies, and runs via:

swift pbdump.swift

Perfect for:

  • debugging
  • reverse engineering
  • automation
  • development workflows

If you're working with complex clipboard formats, this gives you full visibility into the actual data stored by the system.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment