LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   LinuxQuestions.org Member Success Stories (https://www.linuxquestions.org/questions/linuxquestions-org-member-success-stories-23/)
-   -   Auto-paste Date/Time into text apps under KDE (https://www.linuxquestions.org/questions/linuxquestions-org-member-success-stories-23/auto-paste-date-time-into-text-apps-under-kde-508948/)

KWTm 12-09-2006 07:19 PM

Auto-paste Date/Time into text apps under KDE
 
I have figured out a way to paste the date into the text of any application with two keypresses, in KDE. So this would apply to KUbuntu, Mandriva, SuSE, or any other linux distro using KDE. I myself am using KUbuntu Dapper (6.06).

For the longest time, I have been bugged by the lack of a way to automatically enter the current date and time into text, a sort of "timestamp", which would be really useful with something like: "Captain's log: 2006-12-09 16:16" or something. Even with the vaunted OpenOffice word processor, you can't insert the date! (There is a way to insert a code that means "current date/time", but if you type that code yesterday, and then open the file today, it will display today's date. Besides, it takes about one bazillion mouse clicks to insert that code.) I haven't found out how to do it in KWord and other applications; and besides, even if there were a way to do it in one particular application, it wouldn't apply to other applications.

The way that makes the most sense to me is to have it in Klipper, the KDE clipboard. It should be able to generate a clipboard item that's the current date/time, so that all you have to paste it into your document. And, of course, any application from browsers to word processors would have the Paste (from Clipboard) feature, right?

It took some fiddling around, but I figured out how to do it, so I wanted to share this with anyone else who might want it.

Step 1: the DCOP server

What makes this possible in KDE (I'm not sure it can be done in GNOME) is the DCOP server, an amazingly handy and brilliant way to control KDE applications from the command line. I won't get into detail about how to use DCOP, but there are a number of tutorials on the web that I will list at the end.

Suffice it to say that, if you wanted to put the string "ABCxyz" into the clipboard, you could do it with this command in a terminal:

dcop klipper klipper setClipboardContents ABCxyz

Try typing that at the command line. (Note that the word "klipper" is repeated.) Then go to some KDE application like an editor, and paste (Ctrl-V, usually). You'll see the string "ABCxyz" pasted in.

Step 2: Getting the Date

This is simple. To get the date, use the "date" command at the command line. Example:

$ date
Sat Dec 9 16:33:22 PST 2006

I like to have a more compact form for the date and time, so I add the argument "+Time now: %F %R" to the date command:

$ date "+Time now: %F %R"
Time now: 2006-12-09 16:34

(Use "date --help" to see what other formats are available.)

Step 3: Setting up a script file

We need a "program" that will put the date into the clipboard, rather than having us type the actual commands at the command line. Fortunately, this is simply a text file that we can type in. Create a file called "datetime_to_klipper.sh" by opening your favourite editor (such as Kate or KWrite) and type in the following three lines, then save:

#!/bin/sh
CLIPTEXT=`date '+%F %R'`
dcop klipper klipper setClipboardContents "$CLIPTEXT"

Note the quotes carefully. After "CLIPTEXT=", there is a backquote (reverse single quote). After "date ", there is a (normal forwards) single quote. At the end of the second line, there is again a single quote, then a backquote. On the third line, those are double-quotes that surround "$CLIPTEXT". Get these wrong and you'll be totally mixed up!

After saving the file, make it executable with the command "chmod +x datetime_to_klipper.sh" (or whatever you named the file). You have to type the chmod command while in the same directory as the datetime_to_klipper.sh file.

Now, whenever you run the datetime_to_klipper.sh script program that you just typed in, the date and time will be copied to the Klipper clipboard, and you can just paste it into your application. Test it, by actually typing "datetime_to_klipper.sh" at the command line, and then pasting from clipboard to see if the date and time actually gets entered.

Step 4: Set up a hotkey that runs your script file

The last step is to add your new script to the KDE Menu. Start the KDE menu editor by right-clicking on the KDE button on the panel (usually lower left corner, just like the "Start" button in MS Windows). One of the selections is "Menu Editor", so start that. It will be as if we're adding a new program to the menu, but the program is actually the script program that you just entered, datetime_to_klipper.sh!

You'll see a tree structure that corresponds with the actual KDE Menu. Add a new menu item with
F)ile > New (I)tem. Enter some name of your choice (such as "Put date in clipboard") and maybe a description; the exact entry is not important. For the "Command:", you need to enter the name of the script program, which is "datetime_to_klipper.sh". Uncheck the "Enable launch feedback" box.

Now we define a shortcut key combination that triggers this program, which is the whole point of putting this item into the KDE Menu. (We don't actually plan to click on the KDE Menu each time to run this program!) At the bottom of the KDE Menu Editor, it says "Current Shortcut Key". Click on that box, and it will let you choose a keypress. I chose Win+1, but you can choose Alt+D or Ctrl+Shift+X or whatever.

Save the changes in the KDE Menu with F)ile > S)ave, and your system should be updated.

Now test it! Press Win+1 (or whatever shortcut key combo you selected), and then go to some editor and Ctrl-V to paste. You should get the date and time pasted!


Epilogue: You're all done!

You can vary the date format used. I have one key combo for date only, and another one for date and time.

By the way, our example script program takes up two lines (not including the first line, which is a comment). If you want to get it all onto one line, you can try the command:

date "+%F %R is the current time." | xargs -0 dcop klipper klipper setClipboardContents

I'll let you figure that one out.

The following are some web pages that have good DCOP tutorials. You can also check Librenix.com, which often have good tutorials on various aspects of Linux.

http://www.volny.cz/bwian/dcop.html
http://www-128.ibm.com/developerwork...xw16ConnectKDE

(I also googled for "DCOP howto", but the resulting web pages were very technical and not suitable for newbies.)

This posting is hereby licensed under the GNU Free Documentation License. I would love it if anyone pointed out errors, cleaned up the text or put in some screenshots.

KWTm

devdol 12-11-2011 05:35 AM

Ehnace usability by 100%
 
Good Idea! Nowadays on KDE 4
Code:

qdbus org.kde.klipper /klipper setClipboardContents $(date  +"%Y-%m-%d_%H-%M")
does the job.

However, one thing could double usability: The need for only one but two keystrokes to insert the time stamp. Therefore, we'd need an additional command to insert the ClipboardContents into the current window (in other words, to simulate a paste action, triggered by Ctrl+v, middle mouse click or something like that).

By the way, succeeding in doing so, we even would not need to use the clipboard as detour.

On the one hand, qdbusviewer does not show up something like a paste method in "org.kde.klipper.klipper". On the other, it principally must be possible to achieve this, as applications like 'autokey' already have this functionality.

Any ideas to achieve this by means of a tiny bash or python script (which can be triggered by a custom defined hotkey or gesture in 'systemsettings')?

Wattos 02-09-2012 12:15 AM

Why dont you simply use xdotool ?

"xdotool key ctrl+v"

or even better:

"xdotool type $(date +"%Y-%m-%d_%H-%M")"

No need to use klipper at all!

devdol 02-10-2012 04:49 AM

Hmm, seems that some people got used to driving cars and forgot what bicycles or feet were good for ,-)

Thanks for your hint, I never knew about 'xdotool' before.

KWTm 02-10-2012 08:59 AM

why didn't I use xdotool? the answer is simple
 
Quote:

Why dont you simply use xdotool ?
In case you were wondering: xdotool did not exist at the time I wrote the post, twelve Ubuntu versions ago.


All times are GMT -5. The time now is 08:12 PM.