On 29/09/26 at 16:33, Greg Wooledge wrote:
On Tue, Sep 29, 2026 at 14:03:10 -0000, CGS wrote:
The following command works:

~$ toclip "$(cat b.txt)"

OK.  I'm going to ignore all the dbus/X11 stuff and look *only* at the
shell part of the question.  The underlying tool used by your "toclip"
command obviously accepts the payload passed as an argument.  If you
can't find an option for that command to accept the payload on stdin,
then we'll work with what you have.


frank@itek:~$ LC_MESSAGES=C.UTF-8 man qdbus
No manual entry for qdbus
frank@itek:~$ LC_MESSAGES=C.UTF-8 man qdbus6
No manual entry for qdbus6
frank@itek:~$ qdbus6 --help
Usage: qdbus [--system] [--bus busaddress] [--literal] [servicename] [path] [method] [args]

  servicename       the service to connect to (e.g., org.freedesktop.DBus)
  path              the path to the object (e.g., /)
  method            the method to call, with or without the interface
  args              arguments to pass to the call
With 0 arguments, qdbus will list the services available on the bus
With just the servicename, qdbus will list the object paths available on the service With service name and object path, qdbus will list the methods, signals and properties available on the object

Options:
  --system          connect to the system bus
  --bus busaddress  connect to a custom bus
  --literal         print replies literally

I didn't find anything about placing <stdin> on the command-line of qdbus6 so I assume that using an alias isn't viable.

Here's the obvious way:

unalias toclip      # if you're testing in a shell that has the alias set

toclip() {
     local payload
     payload=$(cat)
     qdbus6 org.kde.klipper /klipper \
         org.kde.klipper.klipper.setClipboardContents "$payload"
}

I predict you will have follow-up questions.  I predict that one of
them will be "how do I write it so that it takes the payload either
from stdin *or* from the argument list?"  So, here's a variant that
looks for arguments, and uses those if present, and otherwise uses
stdin:

toclip() {
     local payload
     if (($#)); then
         payload="$*"
     else
         payload=$(cat)
     fi
     qdbus6 org.kde.klipper /klipper \
         org.kde.klipper.klipper.setClipboardContents "$payload"
}

Awesome, the above function works in all the following test cases:

frank@itek:~$ toclip "someText"
frank@itek:~$ cat b.txt | toclip
frank@itek:~$ toclip <b.txt


If another of your follow-up questions is "how do I do this as an alias",
the answer is "you don't".  Functions are required for this purpose.


Thank again!
--
Franco Martelli

Reply via email to