03/08 Twitterrificを使って、特定のアカウントの発言を監視
Twitterrificを使って、特定のアカウントの発言内容を監視するAppleScriptを作成しました。
Twitter経由で自宅のMacに命令を送ってテレビ録画を……などと考え、TwitterクライアントでAppleScript対応のTwitterrificをコントロール。自宅のMacにTwitterrificをインストールして、適宜自動処理用のTwitterアカウントを取得。Twitterrificが動作するようになったら、監視対象のTwitterアカウント(自分)をフォローするようにして、外でのTwitter経由の発言が自宅マシンでも見えるようにしておきます。
本Scriptの中に、
property masterAccount : “piyomaru”
という行があり、これが監視対象の(自分の)Twitterアカウントです。とりあえず、このあたりを書き換えていただいて、本Scriptを「アプリケーション」形式で保存。その際に、「実行後、自動的に終了しない」をチェックしておきます。
これで、本Scriptは10秒間隔でTwitterrificに問い合わせを行い、「#m2tv」の文字列を含む監視対象アカウントの発言が流れて来ないか、監視します。
条件に合致する発言があれば、その内容をそのまま発言者にエコーバックするようにしています。

実際には、このエコーバックした後にコマンド解釈部を入れて、さまざまなコマンドを実行させるようにするとよいでしょう。
ただ……実際に動かしてみて分かったのですが、Twitterrificが3分間隔でTwitterにアクセスするため、最悪の場合にはコマンドを送って返ってくるまで6分ぐらいかかることになり……Twitter経由でTV録画予約はできないことはないですけれども、ほかの手段を考えるかTwitterrific以外のアクセス手段を検討したほうがよさそうです(自前でTwitterへのアクセスプログラムをAppleScriptで作ったほうがよさそう)。
| スクリプト名:twittEcho v2 |
| property idleTime : 10 property idleF : false property lastTweet : "" property lastTweetDate : "" property masterAccount : "piyomaru" –監視対象のアカウント idleMain(true) of me repeat idleMain(false) of me delay 5 end repeat on idleMain(initF) tell application "Twitterrific" set aList to every tweet whose screen name = masterAccount and text of it contains "#m2tv" if aList is not equal to {} then set anItem to contents of first item of aList set thisPostDate to date of anItem –初期化フラグをtrueにして呼んだ場合には、処理に必要な変数の初期化のみ行ってリターン if initF = true then set lastTweetDate to thisPostDate return end if if thisPostDate is not equal to lastTweetDate then set lastCommand to text of anItem try with timeout of 30 seconds post update ("@" & masterAccount & " (echo)" & lastCommand) if lastCommand contains "beep" then say "Message from twitter" end if end timeout end try set lastTweetDate to thisPostDate end if end if end tell end idleMain |







