ひとりぶろぐ

価値ある情報をユーザー視点で発信するブログ

Growlの知られざる機能「Rules」で自宅待機のMacをTwitterからリモートコントロールする

   

何か面白いことができそうなGrowlのRules


Skitched 20140307 031802Skitched 20140307 031802
OS XGrowl

Growl AppApp : 
: 400

OS X MavericksOSGrowl

GrowlRules

AppleScriptGrowl 2.1

Growl  AppleScript Rules Documentation for Growl
OS X 10.8OS X 10.7Rules

ScriptActionOS X 10.9[Read More]

AppleScript


AppleScriptRules.scpt
/Users/ユーザー名/Library/Application Scripts/com.Growl.GrowlHelperApp/Rules.scpt

GrowlRules使RulesRules.scpt



Rules.scptGrowlUse RulesRules使

Skitched 20140306 044801Skitched 20140306 044801


何もしないRules.scptと仮引数notification


Rules.scptAppleScript
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 from

Growl notification 

notificationGrowlNotificationGrowlNotificationGrowlAppleScript
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 Twitter
host localhost
note description @hitoriblog 先輩はアホですね
note title hitoriblog_testからの@ツイート

 TwitterAppApp : Social Networking
 : Twitter, Inc.: 3.9 MB
 : 667 
 

AppleScript

Rules.scptGrowl


RubyRuleResultGrowlRuleResultGrowlAppleScript

Skitched 20140306 045357Skitched 20140306 045357
notificationnote descriptionGrowlMacignoring case
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 from

ifRuleResult{display:Music Video}Music Video

display

Skitched 20140306 134717Skitched 20140306 134717
RuleResult{display:none, actions:Speech}

Rules.scpt


Rules.scpt{notification return:notification}

notificationdescription@Growl@GrowlMacnotificationdescription{notification return:notification}AppleScript!
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

Ruby / Python


Ruby / Python

AppleScriptRuby / Python

Ruby / Python使

AppleScriptAppleScript使osascriptRuby / Python

notification使

Skitched 20140307 030125Skitched 20140307 030125
Rules.scpttest.rb

Rules.scpt

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

test.rb


Rules.scpttest.rbRules.scpt

app nameTwitternote titlehitoriblog@
#!/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] 

MacTwitter


Skitched 20140307 035803Skitched 20140307 035803
MacGrowlRules.scpttest.rb

Twitterbot

Skitched 20140307 031622Skitched 20140307 031622
note description
#!/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


botTwitterMac

botTwitter

GrowlRules使

Growl AppApp : 
: 400

 - AppleScript, Mac, OS X, Python, Ruby, Twitter