GeekFactory

int128.hatenablog.com

睡眠時間を通知して夜更かしを防ぐスクリプト

暖かくなってきて、ついつい夜更かししてしまう季節になりました。睡眠時間は翌日の生産性に大きく影響するため、なるべく多くの睡眠時間を確保したいものです。Enterprise Neetなら昼前に出社する生活を堪能できますが、そんな機会はなかなかないものです。

そこで、翌朝の起床時刻までの残り時間を通知センタに表示するスクリプトを書きました。

https://cloud.githubusercontent.com/assets/321266/6993045/24263e16-db22-11e4-94a9-776b0ef41dd1.png

スクリプトのインストールはREADME.mdをご覧ください。

github.com

仕組み

AppleScriptで翌朝の起床時刻までの残り時間を計算して通知センタに表示しています。

set awake_time to date "08:00:00"
set title_head to "Remaining"
set description to "You can sleep only"

set remain_time to date "0:0" + (awake_time - current date)
display notification description with title title_head & " " & time string of remain_time

定期実行はLaunchd plistにベタ書きで実現しています。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <!-- 中略 -->
    <key>StartCalendarInterval</key>
    <array>
      <dict><key>Hour</key><integer>22</integer><key>Minute</key><integer>0</integer></dict>
      <dict><key>Hour</key><integer>22</integer><key>Minute</key><integer>10</integer></dict>
      <dict><key>Hour</key><integer>22</integer><key>Minute</key><integer>20</integer></dict>
      <dict><key>Hour</key><integer>22</integer><key>Minute</key><integer>30</integer></dict>
      <!-- 中略 -->
    </array>
  </dict>
</plist>

このスクリプトを書くために夜更かししてしまうという本末転倒ぶりが何とも言えない感じです。