Skip to content

Instantly share code, notes, and snippets.

@slyphon
Created May 28, 2013 03:04
Show Gist options
  • Save slyphon/5660281 to your computer and use it in GitHub Desktop.
Save slyphon/5660281 to your computer and use it in GitHub Desktop.
This, more than anything, has improved my productivity an embarassing amount (w/ the scala repl)
import java.io.{PrintWriter, OutputStreamWriter, BufferedOutputStream}
// copy given string to the mac clipboard by launching pbcopy in a subprocess
object Clipboard {
def pbcopy(string: String) {
val pb = new ProcessBuilder("/usr/bin/pbcopy")
val p = pb.start()
val stdin = new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(p.getOutputStream)))
stdin.print(string)
stdin.close()
val exitStatus = p.waitFor()
if (exitStatus != 0)
throw new RuntimeException("pbcopy process exited with status" + exitStatus)
}
}
@ryanlecompte
Copy link

Nice! Here's a bit more concise version of that, too:

import java.io.ByteArrayInputStream
import scala.sys.process._

object Clipboard {
  def pbcopy(s: String) {
    "/usr/bin/pbcopy" #< new ByteArrayInputStream(s.getBytes("UTF-8")) !
  }
}

@slyphon
Copy link
Author

slyphon commented May 28, 2013

fuckin' show-off...

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