Skip to content

Instantly share code, notes, and snippets.

@kristopherjohnson
Created January 11, 2015 00:58
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save kristopherjohnson/f301434cdecc28950041 to your computer and use it in GitHub Desktop.
Save kristopherjohnson/f301434cdecc28950041 to your computer and use it in GitHub Desktop.
Utility functions to copy/paste text
import Foundation
#if os(iOS)
import UIKit
#else
import AppKit
#endif
/// Return string value currently on clipboard
func getPasteboardContents() -> String? {
#if os(iOS)
let pasteboard = UIPasteboard.generalPasteboard()
return pasteboard.string
#else
let pasteboard = NSPasteboard.generalPasteboard()
return pasteboard.stringForType(NSPasteboardTypeString)
#endif
}
/// Write a string value to the pasteboard
func copyToPasteboard(text: String) {
#if os(iOS)
let pasteboard = UIPasteboard.generalPasteboard()
pasteboard.string = text
#else
let pasteboard = NSPasteboard.generalPasteboard()
pasteboard.clearContents()
pasteboard.setString(text, forType: NSPasteboardTypeString)
#endif
}
@lfaoro
Copy link

lfaoro commented Aug 25, 2015

thanks

@dkarter
Copy link

dkarter commented Nov 9, 2015

wow the clearContents() part is exactly what i needed. It won't work without it. Thanks!

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