Score:0

Timestamp keyboard shortcut

bm flag

On Ubuntu Mate 20.10 on a Lenovo T450 notebook, I have been trying to create a timestamp keyboard shortcut for quite a while. The intent is to be able to insert a time stamp into any sort of file as if I typed it, whether a text file, LibreOffice Write, Calc, draw or any other type of document.
The two closest questions similar to this one are:

Following the suggestions in the first link I created the following bash script file Timestamp.sh:

 #!/bin/bash
 sleep 0.3s && date +%Y.%m.%d' : '%H.%M.%S

which when executed produces a timestamp formatted to my needs and looks like this:

2021.07.11 : 21.10.26

Now using the keyboard shortcut app in Mate mate-keybinding-properties, and tying the above script to F9 for testing purposes I've tried the following:

/home/xak/scripts/Timestamp.sh

xdotool keyup exec /home/xak/scripts/Timestamp.sh

xdotool keyup sleep 0.3s type `date +%Y.%m.%d' : '%H.%M.%S`

xdotool type "$(printf 'date\n\e ')"

bash -c 'xdotool type "`date +"%Y-%m-%d_%T`"'

bash -c 'xdotool type --clearmodifiers -delay 0 "`date +"%Y-%m-%d_%T`"'

None of these work. No matter how long I hack at this (in the old positive sense of hack) or how much I search I cannot fine how to make this relatively simple thing work.

Now some of the things above that I've tried were proposed by people with a lot more experience than me but when you put them for execution as a keyboard shortcut using mate-keybinding-properties they don't seem to work. What am I missing?

To make this work would give all users the capacity to insert a timestamp into any application as needed.

Score:0
cn flag

That is what I use to insert a date stamp:

#!/bin/sh
xdotool keyup ctrl+shift+d sleep 0.4 type $(date +"%Y-%m-%d")

The "keyup" statement first releases the hotkey I assigned to this script, to avoid interference of these pressed keys with the send keystrokes. A delay is still needed for me mainly for it to work in Firefox and Thunderbird. For other applications, the delay can be smaller.

This is the version I previously used (and to which I may revert to again): it uses the clipboard instead, and prevents the possibility that some keystrokes are discarded.

#!/bin/sh
OLDSELECT=$(xsel --clipboard --input)
CURRENTDATE=$(date +"%Y-%m-%d")
echo -n $CURRENTDATE | xsel -b -i
sleep 0.3
xdotool keyup ctrl+shift+d key Control_L+v
sleep 0.3s
echo -n $OLDSELECT | xsel  --clipboard --input

This saves the current (textual) keyboard and restores it in the end. In the mean time, the date is put on the clipboard and pasted into the active application using Ctrl+v. Again, unfortunatelly, sleep is required to have it work - Linux is very eager to throw away keystrokes if you type too fast. Again for Firefox and Thunderbird, a 0.3 second sleep is required for me before the clipboard is correctly restored.

xian555 avatar
bm flag
Ok so maybe I'm not crazy. When I use your first example: #!/bin/sh xdotool keyup ctrl+shift+d sleep 0.4 type $(date +"%Y-%m-%d") in Pluma or LO7 Write it does nothing, or at least it does not output anything. If I put : xdotool keyup sleep 0.33s type --clearmodifiers 'Test xdotools' this works. But I can't get it to work with $(date +...) . I just don't understand what the problem is.
xian555 avatar
bm flag
Another question about your first example: what's the ctrl+shift+d for ? Thanks
vanadium avatar
cn flag
As I said in the answer.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.