Click to copy to clipboard?

hi there -

i was wondering if there’s a way to embed some sort of function in a button that copies a bit of code into the computer’s clipboard.

i was thinking maybe there’d be something in the Document class, but I’m not seeing it - curious if anyone has done this.

thanks!

I have not used it myself but you might want to try ddwSnippets: GitHub - jamshark70/ddwSnippets: Rudimentary snippets facility for ScIDE, implemented in sclang

You can also go to Github and search for ‘Supercollider snippets’, there are a few options.

I think this method doesn’t exist in SC.

(ddwSnippets doesn’t do anything with the clipboard btw – it’s a mechanism to insert pre-written code bits into a document.)

hjh

1 Like

there may be a workaround that relies on calling external tools (but these are platform dependent → if this approach works, it probably would be a good reason to hide the platform depencies from a user in a quark)

  • for MacOS, commands like “pbcopy” and “pbpaste” exist which can be used to copy / paste things to the clipboard.
  • for linux, similar tools xclip and xsel exist (which may need to be installed first)
  • for windows, I’m not sure what exists - (probably some powershell commands exist)
var clipboardContent = "pbpaste".unixCmd;

I have found that the methods work on all platforms.

There are a few prerequisites:

1. to register this functionality:

(
Document.current.mouseDownAction = { |... args| 
	if(args[5] == 2) {
		Platform.case(
			\osx,       { 
				"osascript -e 'tell application" + 
				"System Events".quote + 
				"to keystroke" + 
				"c".quote + 
				"using command down'"
			},
			\linux,     { 
				"xdotool key ctrl+c"
			},
			\windows,   { 
				"echo CreateObject(" ++
				"WScript.Shell".quote ++ 
				").SendKeys" +
				"^c".quote +
				"> sendctrlc.vbs && cscript //nologo sendctrlc.vbs && del sendctrlc.vbs"
			}
		).unixCmd
	}
}
)

2. to test:

(123456)  
// Double-click the bracket to copy `(123456)`, 
// then paste it elsewhere.

3. to remove this functionality:

Document.current.mouseDownAction = nil

Would it be useful if this functionality is provided as a built-in method? (I am not sure…)

1 Like

Thanks, @prko - this is pretty close to what I’m trying to do. I’ll see if I can tie it to a button object…
The one part I’m a little uncertain about is whether or not there’s a way to evaluate a variable within this…for example:

~a = (123456)  
// Double-click the bracket to copy `~a`, 
// then paste it elsewhere.

If you want to copy a code allocated to a variable. you can enclose brackets as follows:

(~a = 123456)
(
a = { |x = 0, y = 0|
x + y
}
)

However, you seem to want to allocate two actions to a double-click action:

  1. evaluate the selected code block
  2. copy the selected code block

Then the part related to allocating keystrokes should be edited.

However, I think it should be better to implement these actions as the default action in sclang or SC-IDE (or its equivalents), but I do not know how to do it.

1 Like

On linux there’s a lot of distros moving from X11 to wayland these days (maybe ubuntu studio is already using wayland?) – i use arch linux with hyprland – i tried before to use xdotool without sucess. some clipboards managers that works with wayland on this link: Clipboard managers – Hyprland Wiki

Step forward after evaluating a line in the Help Browser would be nice.

@igormpc
What I used is not a clipboard manager, but a keystroke functionality.
On Ubuntu, wayland is already installed, but ydotool and ydotoold are needed for the same task, but there is no ydotoold on the system, even though ydotool is installed.

I cannot find a general solution for implementing the functionality on Linux. Sorry about that.

@boxnumber here is my revision:

(
Document.current.mouseDownAction = { |... args|
	if(args[5] == 2) {
		Platform.case(
			\osx,       {
				(
					"osascript -e 'tell application" +
					"\"System Events\"" +
					"to keystroke return using shift down' &&" +
					"sleep 0.01 &&" +
					"osascript -e 'tell application" +
					"\"System Events\"" +
					"to keystroke \"c\" using command down'"
				).unixCmd;
			},
			\linux,     {
				"xdotool key shift+Return && sleep 0.01 && xdotool key ctrl+c".unixCmd;
			},
			\windows,   {
				(
					"echo CreateObject(\"WScript.Shell\").SendKeys \"+{ENTER}\" :" +
					"WScript.Sleep 10 :" +
					"CreateObject(\"WScript.Shell\").SendKeys \"^c\"" +
					"> sendkeys.vbs && cscript //nologo sendkeys.vbs && del sendkeys.vbs").unixCmd;
			}
		)
	}
}
)
1 Like

@boxnumber
I also added code indentation functionality:

1 Like