Last active
August 29, 2015 14:12
-
-
Save kitsuyui/b779ba7ec4a47088ffe8 to your computer and use it in GitHub Desktop.
.bash_history のように、アプリケーションの使用履歴をファイルに記録するスクリプト
This file contains 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/sh | |
#### begin settings #### | |
log_to=~/.app_history | |
idle_min_seconds=300 | |
duration=1 | |
idling_app_name='#Idle' | |
#### end settings #### | |
osascript=/usr/bin/osascript | |
sleep=/bin/sleep | |
nohup=/usr/bin/nohup | |
date=/bin/date | |
ioreg=/usr/sbin/ioreg | |
awk=/usr/bin/awk | |
selfname="${0##*/}" | |
selfpath="$(cd $(dirname $0) && pwd)/$selfname" | |
idle_time (){ | |
$ioreg -c IOHIDSystem \ | |
| $awk '/HIDIdleTime/ {OFMT = "%0.f"; print $NF/1000000000; exit}' | |
} | |
front_app_path (){ | |
$osascript -e "(path to frontmost application)'s POSIX path" | |
} | |
idle_message (){ | |
echo $idling_app_name | |
} | |
current_date (){ | |
$date +'%Y-%m-%dT%H:%m:%S' | |
} | |
main (){ | |
while true | |
do | |
local current_idle_seconds=$(idle_time) | |
if (( current_idle_seconds < idle_min_seconds )) ; then | |
local current_app=$(front_app_path) | |
else | |
local current_app=$(idle_message) | |
fi | |
if [ "$current_app" != "$previous_app" ] ; then | |
echo "$(current_date)\t$current_app" | |
fi | |
local previous_app="$current_app" | |
$sleep $duration | |
done | |
} | |
start_background (){ | |
$nohup "$selfpath" &> /dev/null & | |
} | |
if [ ! -t 0 ] && [ ! -t 1 ] ; then | |
exec >> $log_to | |
main | |
else | |
start_background | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment