Growlの知られざる機能「Rules」で自宅待機のMacをTwitterからリモートコントロールする
価値ある情報をユーザー視点で発信するブログ
もくじ
/Users/ユーザー名/Library/Application Scripts/com.Growl.GrowlHelperApp/Rules.scptGrowlを再起動すると、Rulesを使うかどうかの問い合わせが入り、受け入れればRulesが動作を開始し、通知を表示する直前に、毎回Rules.scptが実行されるようになります。 ちょっと隠し機能のような趣がありますね。 いったんRules.scptの存在が認識されると、Growlの﹁環境設定>一般﹂に﹁Use Rules﹂というチェックボックスが出現するので、Rulesの使用を中止したい場合は、ここのチェックボックスを外します。
using terms from application "Growl" on evaluate notification with notification --Rules go in here --Ultimately return what you want Growl to do with the notification end evaluate notification end using terms fromGrowl側から、仮引数 notification で通知情報が渡されてきます。 notificationの型はGrowlNotification。GrowlNotificationの持つプロパティはGrowlのAppleScript用語説明に書いてありますが、表にすると以下の通りです。
Property | Access | Type | Description |
---|---|---|---|
app name | get/set | unicode text | The application name |
host | get/set | unicode text | The computer from which the note came, localhost for the local machine |
note description | get/set | unicode text | The note’s main text |
note icon | get/set | Image | The icon the note will display with |
note title | get/set | unicode text | The note’s title |
note type | get/set | unicode text | The underlying note type |
priority | get/set | GrowlPriority | The priority of the notification |
sticky | get/set | boolean | Whether the notification will be sticky |
公式TwitterクライアントでMentionを受け取ったときは、notificationのプロパティは以下のようになります。
Property | Value |
---|---|
app name | |
host | localhost |
note description | @hitoriblog 先輩はアホですね |
note title | hitoriblog_testからの@ツイート |
using terms from application "Growl" on evaluate notification with notification ignoring case if notification's note description contains "GrowlMac" then return {display:"Music Video"} end if end ignoring end evaluate notification end using terms fromif文が成立したときはRuleResultのレコード﹁{display:”Music Video”}﹂を返しているので、以降の通知の表示スタイルが﹁Music Video﹂に切り替わります。 プロパティdisplayの中身に指定できるのは、表示スタイルのリストに出てくるものです。
on replace_chars(this_text, search_string, replacement_string) set AppleScript's text item delimiters to the search_string set the item_list to every text item of this_text set AppleScript's text item delimiters to the replacement_string set this_text to the item_list as string set AppleScript's text item delimiters to "" return this_text end replace_chars using terms from application "Growl" on evaluate notification with notification ignoring case set description to notification's note description if description contains "@Growl" then set notification's note description to replace_chars(description, "@Growl", "@GrowlMac") return {notification return:notification} end if end ignoring end evaluate notification end using terms from
on rulesdir() tell application "System Events" return quoted form of (POSIX path of (home folder) & "/Library/Application Scripts/com.Growl.GrowlHelperApp/") end tell end rulesdir using terms from application "Growl" on evaluate notification with notification do shell script『 export GROWL_NOTE_DESCRIPTION=』& quoted form of notification's note description &『; export GROWL_HOST=』& quoted form of notification's host &『; export GROWL_APP_NAME=』& quoted form of notification's app name &『; export GROWL_NOTE_TITLE=』& quoted form of notification's note title &『; /usr/bin/ruby 』& rulesdir() & "test.rb" end evaluate notification end using terms from●ダウンロード: Rules.scpt.zip
#!/usr/bin/ruby # -*- coding: utf-8 -*- $KCODE = 'u' require 'kconv' host = ENV['GROWL_HOST'] app_name = ENV['GROWL_APP_NAME'].toutf8 note_title = ENV['GROWL_NOTE_TITLE'].toutf8 note_description = ENV['GROWL_NOTE_DESCRIPTION'].toutf8 # Twitterアプリで @hitoriblog からのメンションの通知が来たら if app_name == "Twitter" && note_title == "hitoriblogからの@ツイート" # 何かする end日本語が含まれる場合は、事前にUTF-8に変換しておかないとダメなようでした。 Pythonの場合は、環境変数の参照は、 os.environ[‘GROWL_APP_NAME’] といった感じになりますね。
#!/usr/bin/ruby # -*- coding: utf-8 -*- $KCODE = 'u' require 'kconv' my_master = "@hitoriblog" my_account = "@hitoriblog_test" host = ENV['GROWL_HOST'] app_name = ENV['GROWL_APP_NAME'].toutf8 note_title = ENV['GROWL_NOTE_TITLE'].toutf8 note_description = ENV['GROWL_NOTE_DESCRIPTION'].toutf8 # Twitterアプリで @hitoriblog からのメンションの通知が来たら if app_name == "Twitter" && note_title == "hitoriblogからの@ツイート" # tweet関数は別途定義する必要があります case note_description when『#{my_account} アップタイム教えれ』then tweet("#{my_master} " + `uptime` + "デス!") when『#{my_account} ディスク使用率教えれ』then tweet("#{my_master} " + `df | awk '/disk0s2/{print $8}'` + "デス!") else tweet("#{my_master} 命令が分からないデス!") end end●ダウンロード:test.rb.zip こんな感じにしておくと、bot用アカウントへのメンションで、Twitter経由でどこからでも自宅のMacに何かをさせるということが実現できるわけです。 bot用のアカウントをアクティブにしておくのは、公式Twitterクライアントは、自分から自分へのメンションの場合、通知を出してくれないからです。 これは一例にすぎませんが、GrowlはRulesを使うと活用の道が広がりますね。遊んでみてください。