clipboard_darwin.go 927 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2013 @atotto. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //go:build darwin
  5. // +build darwin
  6. package clipboard
  7. import (
  8. "os/exec"
  9. )
  10. var (
  11. pasteCmdArgs = "pbpaste"
  12. copyCmdArgs = "pbcopy"
  13. )
  14. func getPasteCommand() *exec.Cmd {
  15. return exec.Command(pasteCmdArgs)
  16. }
  17. func getCopyCommand() *exec.Cmd {
  18. return exec.Command(copyCmdArgs)
  19. }
  20. func readAll() (string, error) {
  21. pasteCmd := getPasteCommand()
  22. out, err := pasteCmd.Output()
  23. if err != nil {
  24. return "", err
  25. }
  26. return string(out), nil
  27. }
  28. func writeAll(text string) error {
  29. copyCmd := getCopyCommand()
  30. in, err := copyCmd.StdinPipe()
  31. if err != nil {
  32. return err
  33. }
  34. if err := copyCmd.Start(); err != nil {
  35. return err
  36. }
  37. if _, err := in.Write([]byte(text)); err != nil {
  38. return err
  39. }
  40. if err := in.Close(); err != nil {
  41. return err
  42. }
  43. return copyCmd.Wait()
  44. }