1

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.

muru
  • 207,228
xian555
  • 75

1 Answers1

0

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.

vanadium
  • 97,564