Text Transformations With Ruby?

I have written a ruby script that will convert dates from one format to another for me. My goal is to simply select some text that is the date I need to convert, execute a key combo, and then have the result replace the selected text.

I thought that this would be pretty simple. I set up a Copy action, chained to a Set Clipboard to Text action, then followed with an Execute script file. I then placed my script into the file field and attempted to execute. I found that I received no output.

I then realized that Keyboard Maestro must not be passing the pasteboard contents in as a standard command line argument to this script and so I started investigating using pbpaste within the script. Should be no problem, but when I execute it, it seems to be blank still.

How would you normally accomplish the goal I set forth initially and most efficiently with Keyboard Maestro? Am I doing something incorrectly or is something broken?

Correct. When KM executes a script action, it does not pass anything to the script. The script must either read the Clipboard, or a KM variable set prior to executing the script.

I could tell you how to read the Clipboard in either AppleScript or JXA, but I don't know anything about Ruby. Perhaps a google of "ruby read clipboard" will give you some ideas.

KM does provide for capturing script output, either to the Clipboard or a KM variable.

Good luck.

You can access OS X´s clipboard with the pbpaste and pbcopy shell commands. These can be called from ruby easily.

Here is an example script that prints the clipboard contents (converting them to uppercase):

#!/usr/bin/env ruby

clipboard_contents = `pbpaste`
puts clipboard_contents.upcase
1 Like

To access a Keyboard Maestro variable:

#!/usr/bin/env ruby

clipboard_contents = `echo "$KMVAR_VariableName"`
puts clipboard_contents.upcase

-Chris

3 Likes