Using the system clipboard (macOS)

I have a simple .exs script that generates a string based on some inputs. How would I get that onto the system clipboard on macOS.

Here’s what I have so far:

  1. Gather input from the user via IO.gets/1
  2. Write the output to a file temp using File.write/3
  3. Run a bash script that outputs that temp file to pbcopy

This technically works but there must be a better way that I wouldn’t be ashamed of. Any help would be very much appreciated.

Hi,

  1. if it works it’s nothing you should be ashamed of
  2. can you pipe into pbcopy?
    Greez!

Hi, thanks for the response!

I first thought I could do something like $ elixir go.exs | pbcopy but that won’t work because it gets stuck waiting for user input.

you can use a Port https://hexdocs.pm/elixir/Port.html

eg:

port = Port.open({:spawn, "pbcopy"}, [:binary])  
send port, {self(), {:command, "hello"}}
send port, {self(), :close}

will put hello in your clipboard

4 Likes

That worked perfectly. Thanks for help!

1 Like