Retrieve pasteboard contents by type on macOS

Swift CLI helper to retrieve pasteboard content by type.

This script retrieves HTML and text content from the pasteboard, encodes it to JSON and writes the result to STDOUT.

view raw
import Cocoa

struct Pboard: Codable {
	var html: String
	var text: String
var pb = Pboard(html: "", text: "")

// get contents of general pasteboard
let typeHtml = NSPasteboard.PasteboardType.html
let typeText = NSPasteboard.PasteboardType.string

if let s = NSPasteboard.general.string(forType:typeHtml) {
	pb.html = s
if let s = NSPasteboard.general.string(forType:typeText) {
	pb.text = s

// encode to JSON and print
let encoder = JSONEncoder()
let data = try encoder.encode(pb)
let string = String(data: data, encoding: .utf8)!

Run with:

swift pboard.swift

Build with:

xcrun -sdk macosx swiftc pboard.swift -o pboard


Swift pasteboard script