Last active
October 10, 2024 13:27
-
-
Save ftk/bdbe79468e119ef6cf00b7ddfb5e8f82 to your computer and use it in GitHub Desktop.
check for one-time codes coming as notifications via KDE connect and copy them to clipboard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# check for one-time codes coming as notifications via KDE connect and copy them to clipboard | |
# requirements: qdbus(qt-tools), dbus-monitor, kde-connect, klipper | |
# the script must be run in user session | |
set -euo pipefail | |
shopt -s lastpipe | |
if [[ $# -eq 0 ]] | |
then | |
echo "Usage: $0 [deviceid]" | |
echo "Usage: $0 --all" | |
echo "Available devices:" | |
kdeconnect-cli --list-available | |
exit 2 | |
fi | |
if [[ "$1" == "--all" ]] | |
then | |
# run a process per available device | |
kdeconnect-cli --list-available --id-only | xargs --max-procs=0 --max-args=1 --no-run-if-empty -- "$0" | |
exit $? | |
fi | |
device="$1" | |
echo "Listening for notifications from device $device" | |
# modern solution for an uncivilized age | |
# listen for dbus signals when a notification is posted or updated | |
path="/modules/kdeconnect/devices/${device}/notifications" | |
interface="org.kde.kdeconnect.device.notifications" | |
dbus-monitor --session --monitor \ | |
"type=signal, interface=$interface, path=$path, member=notificationPosted" \ | |
"type=signal, interface=$interface.notification, member=ready" \ | |
| sed -u -n "s%^.* path=$path/\([0-9]*\);.*$%\1%p;s%^ *string \"\([0-9]*\)\"$%\1%p" \ | |
| while read -r id | |
do | |
# extract notification app title and text | |
appname="$(qdbus org.kde.kdeconnect "$path/$id" $interface.notification.appName)" | |
title="$(qdbus org.kde.kdeconnect "$path/$id" $interface.notification.title)" | |
text="$(qdbus org.kde.kdeconnect "$path/$id" $interface.notification.text)" | |
# extract one time code | |
code="$(echo "$text" | grep --only-matching --perl-regexp ' \d{4,8}\b' | tail -n 1 || true)" # ignore error | |
echo "$appname $title $text $code" | |
# copy code to clipboard (klipper) | |
[[ -n $code ]] && qdbus org.kde.klipper /klipper org.kde.klipper.klipper.setClipboardContents $code | |
done | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment