LoginSignup
6
6

More than 5 years have passed since last update.

.bash_history のように、 OS X のアプリケーションの使用履歴を記録するスクリプト

Last updated at Posted at 2015-01-02

.bash_history のように、アプリケーションの使用履歴をファイルに記録するスクリプトを書きました。

目的

自分のアプリケーションの使用履歴を記録して、利用頻度や傾向を調べたいと思ったからです。
充分に記録が集まったら、曜日や時間帯の違いを比較してみたいです。

使い方

1 ファイルだけなので wget して executable なパーミッションにするだけです。
~/.bin あたりに保存してパスを通すと良いと思います。

$ wget https://gist.githubusercontent.com/kitsuyui/b779ba7ec4a47088ffe8/raw/a7a3c68b823f5b82b7ecdd47b2a730f39522eb51/apphistory.sh
$ chmod +x apphistory.sh
$ ./apphistory.sh

& を付けなくとも、 tty がなければ勝手に nohup して立ち上げます。

記録する内容

日時とアプリケーションのフルパスを、 ~/.app_history に記録します。
デフォルトでは 1 秒おきに記録し、同じアプリケーションは連続して記録されません。

また、 5 分以上連続してユーザの操作がない場合は #Idle というアプリケーション名として記録します。
席を外している間はアプリケーションの利用とは違うものとみなす意図です。

行のフォーマットは

{ISO 8601 の日時}{TAB文字}{アプリケーションのフルパス}{LF}

になっています。

記録例

2015-01-02T21:01:23 #Idle
2015-01-02T23:01:03 /Applications/iTunes.app/
2015-01-02T23:01:04 /Users/yui/Applications/iTerm.app/
2015-01-02T23:01:14 /opt/homebrew-cask/Caskroom/sublime-text3/Build 3065/Sublime Text.app/
2015-01-02T23:01:18 /opt/homebrew-cask/Caskroom/google-chrome/latest/Google Chrome.app/
2015-01-02T23:01:22 /Applications/Tweetbot.app/
2015-01-02T23:06:25 #Idle

中身

設定は begin settings から end settings の間に書けるようにしています。

#!/usr/bin/env bash

#### 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
6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6