Using clipboard with gambas script. [SOLVED]

Post your Gambas programming questions here.
Post Reply
dinge
Posts: 12
Joined: Friday 5th June 2020 7:37pm

Using clipboard with gambas script. [SOLVED]

Post by dinge »

Dear Gambassers,

had a problem where the clipboard fuction, who works fine in a gambas execute file but did not work in a gambas script.
Solved this by using xclip and xsel.
The little program is to upper/lower a letter and add or delete a "." from sentence. I call the script with a shortcut keysetting from within the program.
Needed to use xclip an xsel as one copied correct but could not retrieve again from the clipboard, so used both, one for copy and the other for retrieving.
I do not now if a missed a function or setting regarding the clipboard function in the gambas script but this works for me on any texteditor or program.
This here is limited to one letter, change the code for a whole sentence or text you want to be inserted.
You can also call with new coding the script with parameters.
I am shure there are Gambassers who can write much smaller and better code than this, i am a amateur Gambasser, so let the community know if it can be improved.
Hope this helps to adapt and solve clipboard copying with scripts.
Greetings Dinge

Code: Select all

#!/usr/bin/gbs3
Public Sub Main()
	Dim oldletter as string
	Dim oldselect as string
	Dim newletter as string

	shell "xsel -cb" wait
	shell "xdotool key Shift+Right" wait to oldselect
	
	if len(trim(oldselect)) > 0 then 
		oldletter = oldselect
		oldselect=""
	else
		shell "xsel -cb" wait
		shell "xdotool key Shift+Left" wait to oldselect
		if len(trim(oldselect)) = "." then
			oldletter = oldselect
		else
			oldletter = " " 
		endif 
		oldselect= ""	
	endif
	shell "xclip -out -sel" to oldletter
	shell "xsel -cb" wait
	select case trim(oldletter)
		case "."
			shell "xdotool key Backspace" wait
			shell "xdotool type " & "'" & "" & "'" wait
		case " "
			newletter = "."
			shell "xdotool key Delete" wait
			shell "xdotool type " & newletter &  chr(32) wait
			shell "xdotool key Left" wait
		case else
			newletter = upper(oldletter)
			if newletter = oldletter then
				newletter = lower(oldletter)
			endif
			newletter = "'" & newletter & "'"
			shell "xdotool key Backspace" wait
			shell "xdotool type " & newletter &  chr(32) wait
			shell "xdotool key Left" wait
		end select
		Print "OLD: "; oldletter ; "  NEW: "; newletter
End
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Using clipboard with gambas script. [SOLVED]

Post by BruceSteers »

Clipboard should work okay in a script.
Did you use the Use "gb.gui" instruction? Clipboard.class is part of gb.qt4 so you need a gui toolkit loaded.
#!/usr/bin/gbs3

Use "gb.gui"
Public Sub Main()

End
Other things to know about though..
The operating system does not provide any sort of clipboard. each running program does and if the program exits it takes it's clip with it.
the only way around it is to install a clipboard manager like clipit.

Although thinking about it,, Is it like a script for a text editor?
it looks like it gets the current character at cursor pos then replaces it.

In which case using clipboard will never work as the clipboard paste functions are specific to the program, ie, the editor would need to perform it's own Paste operation from the clipboard before the script exits. Using Paste inside the script will only get the text in the script not the editor.

Using an editor like MATE Pluma and it's "External tools" plugin will make that easy (and make it work) as you have options to paste text returned by the script. but returning text (Print/echo) is the only way, any Clipboard.Copy() inside the script will die with the script before the editor moves on to paste it.

your way is cool using xdotool that can simulate keyboard strokes (but it will only work on x11 not wayland)

another option...
Write your own gambas text editor. gb.form.editor rocks :)
I wrote one (specifically for writing bash/gambas scripts), even gave it an External Tools feature like Pluma
https://gitlab.com/bsteers4/scripted (it's a WIP)

Happy coding
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Using clipboard with gambas script. [SOLVED]

Post by BruceSteers »

here is a way your script could get the clip then paste a clip. this way you have 5 seconds to paste something.
#!/usr/bin/env gbs3

' Gambas script file

Use "gb.gui"

Public Sub Main()

' Get clipboard
Dim txt As String = Clipboard.Paste("text/plain")

If txt.Len > 1 Then 
  Message.Error("bad clip\n" & txt)
  Return
Endif

' your functions...
Select txt
 Case "."
   Clipboard.Copy(" ", "text/plain")
 Case " "
   Clipboard.Copy(".", "text/plain")
 Case Else
   If IsLetter(txt) Then Clipboard.Copy(If(IsLCase(txt), UCase(txt), LCase(txt)), "text/plain") Else Error.Raise("Not a letter")

End Select

' Make app a daemon so it lets the calling function resume (get the clip) and keep script alive for 5 seconds to give time to paste before the clip vanishes
Application.Daemon = True
Dim c As Integer = 0
While c < 5
  Wait 1
  Inc c
Wend

End


How it works....
It gets the current clip.
converts it as your one did.
then copies to clipboard
then makes Application.Daemon = True , this then allows the calling function to continue like the script has exited.
then stays alive for 5 seconds so the clip stays active while it's pasted somehere by the caller.

probably not what you are wanting but using Daemon and a countdown shows an example of a clipboard survival workaround.
If at first you don't succeed , try doing something differently.
BruceS
Post Reply