Using clipboard with gambas script. [SOLVED]

Post

Posted
Rating:
#1 (In Topic #856)
Trainee
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

#!/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
Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’
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.

Code (gambas)

  1. #!/usr/bin/gbs3
  2.  
  3. Use "gb.gui"
  4. Public Sub Main()
  5.  
  6.  

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
Bruce Steers / scripted · GitLab  (it's a WIP)

Happy coding
Online now: No Back to the top

Post

Posted
Rating:
#3
Guru
BruceSteers is in the usergroup ‘Guru’
here is a way your script could get the clip then paste a clip. this way you have 5 seconds to paste something.

Code (gambas)

  1. #!/usr/bin/env gbs3
  2.  
  3. ' Gambas script file
  4.  
  5. Use "gb.gui"
  6.  
  7. Public Sub Main()
  8.  
  9. ' Get clipboard
  10. Dim txt As String = Clipboard.Paste("text/plain")
  11.  
  12. If txt.Len > 1 Then
  13.   Message.Error("bad clip\n" & txt)
  14.  
  15. ' your functions...
  16. Select txt
  17.  Case "."
  18.    Clipboard.Copy(" ", "text/plain")
  19.  Case " "
  20.    Clipboard.Copy(".", "text/plain")
  21.    If IsLetter(txt) Then Clipboard.Copy(If(IsLCase(txt), UCase(txt), LCase(txt)), "text/plain") Else Error.Raise("Not a letter")
  22.  
  23.  
  24. ' 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
  25. Application.Daemon = True
  26. Dim c As Integer = 0
  27. While c < 5
  28.   Wait 1
  29.   Inc c
  30.  
  31.  
  32.  


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.
Online now: No Back to the top
1 guest and 0 members have just viewed this.