Lua/ReaScript Conurbation

A place for thoughts and quandaries regarding Lua and ReaScript things.

You did mention a Lua script that accessed the clipboard,
and could paste to a jsfx
Something like that

Using io.popen you can do something like:

-- using:
-- local function print(str) reaper.ShowConsoleMsg(tostring(str) .. "\n") end

local function getClipboard()
  local os = reaper.GetOS()
  local pipe, res
  if os == "Other" then
    pipe = io.popen("xclip -out selection")
  elseif os:sub(1, 3) == "Win" then
    pipe = io.popen("powershell get-clipboard")
    error("Please upgrade to Linux, or Windows.")
  if pipe then
    res = pipe:read("*a")
  return res

local clip = getClipboard()
if clip then

… to get the clipboard on the two OSes that matter. On Linux this doesn’t work with stuff copied from the IDE, which is weird, even though I get a notification that it copied in the OS itself. It needs xclip available in Linux too.

Getting it into a JSFX would involve writing with these functions in ReaScript:

reaper.gmem_write(index, value)

… and then handling it in the JSFX.

1 Like

Here’s an addition to the end of that script:

local kMsgReady = 0
local kMsgBuffer = 1

local clip = getClipboard()
if clip then
  reaper.gmem_write(kMsgReady, clip:len())
  for i = 1, clip:len() do
    local c = clip:sub(i, i)
    reaper.gmem_write(kMsgBuffer + i - 1, c:byte());

That copies the clipboard into the gmem for a plugin called ‘gmemtest’. Here’s a simple JSFX plugin that shows the clipboard:



  kMsgSize = 0;
  kMsgBuffer = 1;
@gfx 300 300
  gfx_setfont(1, "Verdana", 13, 'b');
  gfx_x = gfx_y = 20;
  gmem[kMsgSize] ? (
    i = 0;
      gfx_drawchar(gmem[kMsgBuffer + i]);
      i += 1;
    gmem[kMsgSize] = 0;

It works on Linux anyhoo, with the caveat that it doesn’t work copying stuff from Reaper itself.

The clipboard in linux distros is a mess, like everything else. Ha ha. Possibly of help:

Cheers, that’s weirder… that xclip -selection clipboard -o hangs (Reaper) when there’s nothing in the clipboard or when something is copied from Reaper.

I’m not sure how it might matter, but Justin did mention at one time targeting GDK instead of GTK for linux. It should come up if you search here: Ask Justin Frankel

Yeah, maybe it would be better that on Linux. There’s a cross platform lib I can use, there’s defo some selective strangeness between ‘primary’ and ‘clipboard’ clipboards. If it didn’t hang when checking `clipboard’ then xclip would do.

What is being copied/pasted? Is it maybe an issue with size of the data? I wonder if a really small file hangs the same. Possibly related: If so, see ‘hackerb9’ in that issue.

I think it’s just with an empty clipboard, I checked again and the copy/paste from the Reaper IDE isn’t going to the system clipboard, at least not today. I’ve rustled together a tiny app using this library that just outputs the clipboard that works when it’s empty so that’s that solved.

Nice. Seems a pretty janky issue to have to deal with in the first place. Probably worth reporting to cockos.

Oh, I guess bug reporting is via the forum. Irony.

xclip hangs on the command line so it’s not ReaScript’s fault. If I do come across any bugs I’ll be sure to log in and report them! :slight_smile:

With this I’m using Lua’s popen which when we read with "*a" will wait until the program has finished so I’ve just changed it to read a line and only get the rest if there’s something in that line. Works well enough.

Should probably put it in a coroutine for the most graceful solution, getting up to X lines at a time since too many scripts doing lengthy things could bog a DAW down after all and nobody cares about a couple of 1/30s delays in getting a clipboard.

Or put a clipboard hunter in its own thread with C++ so that Lua just checks and gets like the JSFX does instead of doing any dirty work itself.

The last one would only be a Windows and Linux solution though since I can’t compile for macOS.

What’s the issue there?

Oh it would be a shared library I’d be compiling to use from Lua, but you need a mac to legally compile for macOS. Or there are online services, but I can’t jump through any hoops because Apple forbid running macOS VMs on non-Apple branded hardware.


Yeah, they are weirdos. They’re not preventing anybody from actually doing it, but I’d rather not since I respect the spirit of laws unlike some corporations. :laughing: