Skip to content

Commit b593883

Browse files
authored
Merge pull request HarshCasper#614 from abdallah1097/desktop-time-tracker-bash
Adds Desktop_Time_Tracker using Bash Scripting
2 parents 00f7892 + c902b5a commit b593883

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

Bash/Desktop_Time_Tracker/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Desktop Time Tracker
2+
3+
## Description
4+
`time_tracker.sh` script will track your applications activities e.g. Time Elapsed
5+
It uses [xdotool](https://www.freebsd.org/cgi/man.cgi?query=xdotool&apropos=0&sektion=1&manpath=FreeBSD+8.1-RELEASE+and+Ports&format=html)
6+
7+
### Language
8+
- [X] Bash
9+
10+
11+
### Installation
12+
```bash
13+
$ sudo apt-get install xdotool
14+
```
15+
16+
### Usage
17+
```bash
18+
$ bash time_tracker.sh
19+
```
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/bin/bash
2+
3+
draw_pie_chart() {
4+
declare -n apps_names_array=$1
5+
declare -n win_app_array=$2
6+
7+
# This array obtain the time information only from the win array to use it to draw the pie chart
8+
time_elapsed_array=()
9+
for((i=2;i<${#win_app_array[@]};i+=3)); do
10+
time_elapsed_array+=("${win_app_array[i]}")
11+
done
12+
13+
# To be plotted
14+
echo -e "To be plotted ${apps_names_array[@]}\n ${time_elapsed_array[@]}\n"
15+
16+
}
17+
18+
# Update frequency in seconds
19+
update_frequency=1
20+
# Pie chart update frequency in seconds
21+
pie_chart_update_frequency=1
22+
# Array that stores all apps names
23+
app_array=()
24+
# Array that stores corresponding information to those apps in applist
25+
# This array holds (app_name, executed_path, time_elapsed)
26+
# Accessing in this array will be in multiple of 3, because each app holds 3 entities in the array
27+
# e.g. (app1_name, app1_path, app1_time, app2_name, app2_path, app2_time)
28+
win_array=()
29+
# Get the current working directoy to save the pie chart
30+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
31+
32+
# Main Loop
33+
while true
34+
do
35+
# Sleep for the update frequency
36+
sleep $update_frequency
37+
# Get the current opened window process id
38+
frpid=$(xdotool getactivewindow getwindowpid)
39+
# Get the current opened window name
40+
frname=$(xdotool getactivewindow getwindowname)
41+
# Obtain the app name using ps terminal command
42+
app="$(ps -p $frpid -o comm=)" # echo $app
43+
# adding the app to the app list
44+
if [[ " ${app_array[*]} " != *"$app"* ]]; then
45+
echo "New App is opened, tracking it..."
46+
app_array+=($app)
47+
fi
48+
49+
checklist=()
50+
for ((i=1;i< ${#win_array[@]} ;i+=3)); do
51+
checklist+=("${win_array[i]}")
52+
done
53+
54+
if [[ "${checklist[@]}" == *"$frname"* ]]; then
55+
# The app has already a history of time elapsed, we have to increment the time only
56+
57+
# Getting the app index
58+
for ((k=0; k<${#checklist[@]}; k++)); do
59+
if [[ "${checklist[$k]}" == *"$frname"* ]]; then
60+
# Incrementing, the 3*k+2 term: 3*k because each app holds 3 entries
61+
# +2 because we're accessing the time element regarding this app
62+
win_array[$(("3*$k+2"))]="$(("${win_array[$((3*k+2))]}" + "$update_frequency"))"
63+
fi
64+
done
65+
else
66+
temp_arr=("$app" "$frname" "$update_frequency")
67+
win_array+=("${temp_arr[@]}")
68+
fi
69+
draw_pie_chart app_array win_array
70+
done

0 commit comments

Comments
 (0)