How Might I Pass Text Data from the Ruby Console into My Clipboard Without Saving to a File

How might I pass text data from the ruby console into my clipboard without saving to a file?

You can just echo it instead if there are no newline characters in the string; otherwise, use the IO class.

Using echo:

system "echo #{stringdata} | pbcopy"

OR

`echo #{stringdata} | pbcopy`

Ruby will then just rip the text from memory, inject it into the shell command which opens a pipe between the echo and pbcopy processes.

Using the IO class:

If you want to do it the Ruby way, we simply create a pipe with pbcopy using the IO class. This creates a shared files between the processes which we write to, and pbcopy will read from.

IO.popen("pbcopy", "w") { |pipe| pipe.puts "Hello world!" }

use ruby in WSL to copy text to clipboard

Thanks to @max for the insight. I was able to come up with a solution that does what I need it to, here's the code:

exec( "echo '#{data2}' | clip.exe" )

this code snippet takes the value stored inside data2 and copys it to the clipboard. When the script has finished running in my Ubuntu WSL, I can paste and the value on my clipboard is the value that was contained in data2 when the script was running.

Hope this helps anyone else with this question.

Unable to save pbcopy to a new file without opening a text-editor

Try: pbpaste >newFile

Running keystrokes from program

If sending arbitrary keystrokes to other applications is what you're after you can use the gem https://github.com/erinata/auto_click for it. However, if you can't use gems, what you can do instead is run NirCmd (or one of its alternatives) with the appropriate command line arguments to achieve the same result.

For example:

# Tell the OS to bring up the Notepad window and give it the time
# to do so.
`nircmd win activate ititle notepad`
sleep 0.5
# Select all text in the Notepad window and copy it to the
# clipboard.
`nircmd sendkeypress ctrl+a`
`nircmd sendkeypress ctrl+c`

In-place progress output in the terminal or console

Use carriage return. '\r' should usually work.

How to write to file in Ruby?

The Ruby File class will give you the ins and outs of ::new and ::open but its parent, the IO class, gets into the depth of #read and #write.

Pipe to/from the clipboard in a Bash script

2018 answer

Use clipboard-cli. It works with macOS, Windows, Linux, OpenBSD, FreeBSD, and Android without any real issues.

Install it with:

npm install -g clipboard-cli

Then you can do:

echo foo | clipboard 

If you want, you can alias to cb by putting the following in your .bashrc, .bash_profile, or .zshrc:

alias cb=clipboard


Related Topics



Leave a reply



Submit