?ここでのゴール
MacのメニューバーにTogglの内容を表示して、やっていることに集中できるようにする。
?手順
BitBarをインストールする
- Releases · matryer/bitbar · GitHubからBitBarをダウンロードしてインストールする。
- BitBarとは、スクリプトの標準出力をメニュー・バーに出してくれるMacアプリ。
BitBarを起動してスクリプト置き場を決める
- BitBarを起動すると、どのディレクトリをスクリプト置き場にするか問われるので決める。
- 僕は Dropbox にフォルダを作った。
TogglのAPI tokenをメモる
- My Profileにある「API Token」をメモっておく。
スクリプトを書く
toggl.1m.php
#!/usr/local/bin/php
<?php
const API_TOKEN = "ここに上でメモったトークンをコピペする";
function get($path) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.toggl.com$path");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_USERPWD, API_TOKEN . ":" . "api_token");
$result = curl_exec($ch);
if (curl_errno($ch)) {
throw new RuntimeException('Error:' . curl_error($ch));
}
curl_close($ch);
return json_decode($result, true)['data'];
}
$entry = get("/api/v8/time_entries/current");
if ($entry) {
$description = isset($entry['description']) ? $entry['description'] : '(no description)';
$interval = (new DateTime($entry['start']))->diff(new DateTime());
$duration = str_replace('0h ', '', $interval->format('%hh %im'));
$projectName = '';
$options = '';
if (isset($entry['pid'])) {
$project = get('/api/v8/projects/' . $entry['pid']);
$projectName = $project['name'];
$options .= 'color='.$project['hex_color'];
}
echo $description . ' ' . ($projectName ? "• $projectName " : '') . $duration . ($options ? ' | ' . $options : ''), PHP_EOL;
} else {
echo 'Toggl not running | color=red', PHP_EOL;
}
スクリプト置き場に置き、実行権限を付与しておく
chmod +x toggl.1m.php
BitBarでスクリプトを再読込する
- BitBarのメニューの「Preferences」→「Refresh all」
?所感
- BitBarは活用次第でいろいろできそう。
- こういうのをついPHPで書いてしまうのをどうにかしたい。
- Togglの新UIが日本語打ちにくくてつらい。