Extension to Ctrl+C what you see, then paste (online?)

Hi, I thought it might be cool to be able to hit a keyboard shortcut to copy whatever your program is drawing and then paste it online somewhere. The usual approach is tedious: save a screenshot, go to the website where you want to post, click on “add image” then find and select the image on your drive. I wish it was just ctrl+c and ctrl+v.

OPENRNDR does offer Clipboard access thanks to GLFW, but they never got to implement images on the clipboard. There’s a 7 year old issue waiting there.

I asked myself about what could be done as a workaround, and I found that the Linux xclip program can place an image into the clipboard, so I wrote this simple extension that enables the feature with just one line of code in your program:

import org.openrndr.Extension
import org.openrndr.Program
import org.openrndr.extensions.Screenshots
​
/**
 * OPENRNDR extension to enable
 * CTRL+C to copy the visible content to the clipboard on Linux or
 * other OSes providing the `xclip` command line program.
 *
 * Usage: `extend(ClipboardScreenshot())`
 * then press CTRL+C to copy what you see, paste the image somewhere.
 */
​
class ClipboardScreenshot : Extension {
    override var enabled: Boolean = true
​
    val screenshots = Screenshots().also {
        it.name = "/tmp/openrndr-screenshot.png"
        it.async = false
        it.afterScreenshot.listen { _ ->
            Runtime.getRuntime().exec(
                "xclip -selection clip -t image/png ${it.name}"
            )
        }
    }
​
    override fun setup(program: Program) {
        program.extend(screenshots)
        program.keyboard.keyDown.listen {
            if (program.keyboard.pressedKeys.contains("left-control") &&
                it.name == "c"
            ) screenshots.trigger()
        }
    }
}

Unfortunately it’s Linux-only for now. If you’re using a different platform and find out a way to make this extension work for you, please share how you did it! :slight_smile:

Really like the idea!

I tried to make it work using applescript, but I think I’m running into trouble with macOS strict sandbox permission system. I tried copy pasting and running the applescript in Script Editor and it threw up a dialog box and worked after that: in the Script Editor.

I tried adding IntelliJ IDEA to exception of the sandbox but no luck. It should be mention that I work on my projects on an external disk so maybe that contributes as well?

(EDIT: Added slightly more detail).

Anyway, here’s the code so far. Maybe someone else will have more luck.

imports

import org.openrndr.Extension
import org.openrndr.KeyModifier
import org.openrndr.Program
import org.openrndr.extensions.Screenshots
import java.nio.file.Paths

class ClipboardScreenshot : Extension {
    override var enabled = true

    val screenshots = Screenshots().apply {
        name = "tmp/openrndr-screenshot.png"
        async = false
        val filePath = Paths.get("").toAbsolutePath().toString() + "/" + name
        val applescript = "set the clipboard to (read (POSIX file \"$filePath\") as JPEG picture)"
        println(applescript)
        afterScreenshot.listen {
            Runtime.getRuntime().exec(
               "osascript -e '$applescript'"
            )
        }
    }

    override fun setup(program: Program) {
        program.extend(screenshots)

        program.keyboard.keyDown.listen {
            if (it.modifiers.contains(KeyModifier.SUPER) && it.name == "c") {
                screenshots.trigger()
            }
        }
    }
}
1 Like

Nice! Thanks for sharing :slight_smile:
Do you mean it works, but the first time it asks for confirmation? I see what you mean, first run manually, after that it works. Is this compilable program any better? Or a macports?

ps. Is it expected that it’s a png image but it mentions as JPEG picture?

I didn’t know about it.modifiers.contains. Thanks.