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

**URL:** <https://openrndr.discourse.group/t/extension-to-ctrl-c-what-you-see-then-paste-online/251>\
**Category:** Libraries and extensions\
**Created:** [June 3, 2021, 6:09pm UTC](https://openrndr.discourse.group/t/extension-to-ctrl-c-what-you-see-then-paste-online/251 "2021-06-03T18:09:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![abe](https://yyz2.discourse-cdn.com/free1/user_avatar/openrndr.discourse.group/abe/32/700_2.png) [@abe](https://openrndr.discourse.group/u/abe)\
**Post date:** [June 3, 2021, 6:09pm UTC](https://openrndr.discourse.group/t/extension-to-ctrl-c-what-you-see-then-paste-online/251/1 "2021-06-03T18:09:34Z")

</div>

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](https://github.com/glfw/glfw/issues/260) 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:

```kotlin
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! 🙂

---

<div class="post-metadata">

**Author:** ![torb](https://avatars.discourse-cdn.com/v4/letter/t/2bfe46/32.png) [@torb](https://openrndr.discourse.group/u/torb)\
**Post date:** [June 4, 2021, 8:45pm UTC](https://openrndr.discourse.group/t/extension-to-ctrl-c-what-you-see-then-paste-online/251/2 "2021-06-04T20:45:26Z")

</div>

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

```auto
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()
            }
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![abe](https://yyz2.discourse-cdn.com/free1/user_avatar/openrndr.discourse.group/abe/32/700_2.png) [@abe](https://openrndr.discourse.group/u/abe)\
**Post date:** [June 4, 2021, 8:58pm UTC](https://openrndr.discourse.group/t/extension-to-ctrl-c-what-you-see-then-paste-online/251/3 "2021-06-04T20:58:23Z")

</div>

Nice! Thanks for sharing 🙂  
~~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](https://gist.github.com/mwender/49609a18be41b45b2ae4) any better? Or [a macports](https://ports.macports.org/port/xclip/summary)?

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.
