Skip to content

Instantly share code, notes, and snippets.

@maxchuquimia
Last active October 16, 2018 15:07
Show Gist options
  • Select an option

  • Save maxchuquimia/ee0b126aaa1b49752ce3 to your computer and use it in GitHub Desktop.

Select an option

Save maxchuquimia/ee0b126aaa1b49752ce3 to your computer and use it in GitHub Desktop.
Clipboard Content Tracker. It's that simple.
/**
* Prints changed to the pasteboard on screen. Can also be made to run a command with each new string.
* Usage: swift paste-log.swift
* Prints all new clipboard items on the screen
* or : swift paste-log.swift wget
* Runs wget with a newly copied string
*/
import Foundation
import Cocoa
let pasteboard = NSPasteboard.generalPasteboard()
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = .ShortStyle
dateFormatter.timeStyle = .LongStyle
let arg: String? = Process.arguments.count > 1 ? Process.arguments.last : nil
func clipboardString() -> String {
return pasteboard.stringForType(NSPasteboardTypeString) ?? ""
}
var preveousString = clipboardString()
print("Started on \(dateFormatter.stringFromDate(NSDate()))")
if let a = arg {
print("\u{001B}[1;30mArgument '\(a)' detected. For each copied string the following will be run: `\u{001B}[1;31m\(a) \"<copied string>\"\u{001B}[1;30m`\u{001B}[0m\n")
}
while true {
let newString = clipboardString()
if preveousString != newString {
let dateString = dateFormatter.stringFromDate(NSDate())
print("\n\u{001B}[0;36m ~~~~~ \(dateString) ~~~~~ \u{001B}[0m")
print(newString)
preveousString = newString
if let a = arg {
var command: NSString = "\(a) \"\(newString)\""
print("\u{001B}[1;30mRunning `\(command)`:\u{001B}[0;37m")
var rawcommand = command.cStringUsingEncoding(NSUTF8StringEncoding)
system(rawcommand)
print("\u{001B}[0m")
}
}
NSThread.sleepForTimeInterval(1.0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment