Announcement

Collapse
No announcement yet.

Script to copy card number to clipboard

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    [SOLVED] Script to copy card number to clipboard

    Hi,

    I share with you a script to copy to the clipboard a card number. The script is completed with a launcher in the menu. For my part, I put a shortcut of launcher on the desktop. Of course, do not use the script to copy a password!

    Code:
    #!/bin/bash
         
    # Copy to clipboard the card No. specified by the CARDNO variable.
    
    CARDNO="0123456789"
    
    
    # Launch klipper service if it is not already running.
    
    if ! ps ax | grep '[k]lipper' > /dev/null
    then
        # Start klipper as a process independent of this process.
        nohup klipper &
    fi
    
    # Copy to clipboard
    qdbus org.kde.klipper /klipper setClipboardContents $CARDNO
    
    # Kill klipper process to leave only the clipboard service in the taskbar.
    # cf. http://stackoverflow.com/questions/3510673/find-and-kill-a-process-in-one-line-using-bash-and-regex 
    # for explanation on the Tip of the [x] with grep.
    
    #kill $(ps aux | grep '[k]lipper' | awk '{print $2}')
    There are two small problems with the script. The first appears to be related to Plasma Kubuntu 15.04. It takes two clicks on the icon to get the copy of CARDNO in the clipboard. The first click starts Klipper, the second click makes the copy. The second problem is related to the last line of code commented out. For whatever reason, klipper is not killed.

    Suggestions for improvements are welcome.

    Thank you.

    Pierre

    #2
    IMO klipper should already be running, and be left running. So your script reduces to using qdbus to put stuff on the clipboard, which is cool. I hadn't thought to do that.
    Regards, John Little

    Comment


      #3
      Klipper vs Clipboard widget

      Earlier: https://www.kubuntuforums.net/showth...ma-5-Clipboard
      Try Me !

      Comment


        #4
        Nice, I suspected not have reinvented the wheel. But, why the number is copied at second click?

        Comment


          #5
          If you just want to send something to the clipboard, how about piping through xsel ? Like,

          echo $CARDNO | xsel -b

          Here:

          Code:
          stuart@office:/$ CARDNO="0123456789"
          stuart@office:/$ echo $CARDNO | xsel -b
          stuart@office:/$ 0123456789
          0123456789: command not found
          The last command there I just did a Shift-Insert to print the clipboard. xsel is not installed by default though.
          Attached Files

          Please Read Me

          Comment


            #6
            Wait a moment - i'm starting....

            The first click starts Klipper, the second click makes the copy... But, why the number is copied at second click?
            Executing the script in the konsole:

            $ bash card.sh
            Cannot find '.setClipboardContents' in object /klipper at org.kde.klipper
            The Klipper start will take a moment. The first click will start the Klipper the second click will do the copy.

            You could fix this by adding a wait (sleep) before the qdbus call or add a few lines to check if the Klipper is up and running.

            Searching with the 'Bash wait for process start' - a hit: http://stackoverflow.com/questions/1...-process-start


            The second problem is related to the last line of code commented out. For whatever reason, klipper is not killed.


            Powerfull tool: pkill

            Code:
            man pkill
            Code:
            [FONT=monospace]PGREP(1)                                            User Commands                                           PGREP(1)
            
            [B]NAME[/B]
                   pgrep, pkill - look up or signal processes based on name and other attributes
            
            [B]SYNOPSIS[/B]
                   [B]pgrep[/B] [options] pattern
                   [B]pkill[/B] [options] pattern
            [/FONT]


            Comment


              #7
              oshunluvr : Great! It is much simpler with xsel. I adopt this solution.

              Wheel Inventor : Thank you! I should have thought at the delay.

              Comment


                #8
                Well, xsel does not write on the plasma clipboard. klipper should be functional. So I have to go start klipper. In short, I inserted a command "sleep 1" and my problem was solved.

                Comment


                  #9
                  Okay, here's a version that works. klipper and clipboard have a relationship rather schizoid! To recall, it seems impossible to interact with clipboard via bash. So I have to go through klipper to start a service and then access the service via qdbus.

                  Well, it works. Klipper once started, all that I found in the clipboard of klipper is also in Plasma clipboard ... as long as klipper is running. If I stop klipper, all contents of klipper in the Plasma memory's disappears! It's annoying because I wish klipper stop immediately after the "copy" so to make it disappear from the taskbar because the icon of klipper is the same as that of clipboard and that's make a doublon.



                  Code:
                  #!/bin/bash
                       
                  # Copy a number (CARDNO) into the clipboard.
                  
                  CARTENO="020833550624"
                  
                  
                  # Start klipper service if not already running.
                  
                  if ! ps ax | grep '[k]lipper' > /dev/null
                  then
                  
                      # Start klipper service as an independant service.
                      nohup klipper &
                      
                      # Introduce a delay to allow time for the process to start
                      sleep 1
                      
                  fi
                  
                  # Copy into the clipboard (klipper).
                  qdbus org.kde.klipper /klipper setClipboardContents $CARTENO

                  Comment


                    #10
                    Still need help! Now I want to delete the history of the clipboard. With Klipper, there is the method ClearClipboardHistory. And it works very well. But how do with the Plasma clipboard to erase its historical?

                    Thanks

                    Comment


                      #11
                      The clipboard widget is a bit broken; Bug 352171 - clipboard history in systray cannot be really disabled, security problem: https://bugs.kde.org/show_bug.cgi?id=352171

                      KDE forums: KDE 5, how I totally disable clipboard systray app? - https://forum.kde.org/viewtopic.php?f=66&t=128086
                      Try Me !

                      Comment

                      Working...
                      X