Created
August 5, 2024 11:27
-
-
Save caarmen/d2aa8f6c4c953f1a0f0ad90f6ace638c to your computer and use it in GitHub Desktop.
Script to monitor which application is currently opened (based on dbus-monitor).
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
#!/bin/bash | |
process_active_app() { | |
local last_date_str="" | |
local last_timestamp_s=0 | |
local last_active_app="" | |
while IFS= read -r current_active_app; do | |
local current_timestamp | |
local current_date | |
current_timestamp=$(date +%s) | |
current_date=$(date +"%Y-%m-%d %H:%M:%S") | |
if [ $last_timestamp_s -ne 0 ] | |
then | |
local time_in_previous_app | |
time_in_previous_app=$((current_timestamp - last_timestamp_s)) | |
echo "${last_date_str},${time_in_previous_app},${last_active_app}" | |
fi | |
last_active_app=${current_active_app} | |
last_timestamp_s=${current_timestamp} | |
last_date_str=${current_date} | |
done | |
} | |
echo "datetime_opened,duration_opened_s,application" | |
dbus-monitor \ | |
| grep --line-buffered -B3 "active-on-seats" \ | |
| grep --line-buffered "\.desktop" \ | |
| sed --unbuffered -e 's/^ *string *"\([^"]*\)"/\1/g' \ | |
| process_active_app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tested on ubuntu using wayland.