いい加減、CGI::Application(::Dispatch)だけじゃなくて、Catalystも使えるようになりたいの上記の本を購入した。
お金を払ったことで、勉強しなきゃいけない気がしてくるので、怠惰な自分にはよかったかもしれない。
Chapter 3まで読み終って、Chapter 4の途中。
次の土日で写経してみようと思う。
#!/usr/bin/perl
use strict;
use warnings;
use Term::ReadLine;
use Mac::iTunes;
our $VERSION = '0.001';
# setup term
my $prompt = 'iTunes> ';
my $term = Term::ReadLine->new($prompt);
my $OUT = $term->OUT || \*STDOUT;
select($OUT);
# setup itunes controller
my $itunes = Mac::iTunes->controller;
my %return_dispatch = (
state => 1,
current_track_name => 1,
pl => 1,
playlist => 1,
get_playlists => 1,
);
my %alias = (
c => 'current_track_name',
p => 'playpause',
s => 'state',
a => 'activate',
q => 'quit',
h => 'help',
pl => 'get_playlists',
playlsit => 'get_playlists',
);
my %show_status = (
p => 1,
play => 1,
stop => 1,
start => 1,
pause => 1,
playpause => 1,
play_track => 1,
);
while ( defined( $_ = $term->readline($prompt) ) ) {
chomp;
my ( $command, @opts ) = split /\x20/, $_;
my $org_command = $command;
$command = $alias{$command} || $command;
if ( $org_command =~ /^(h|help)$/ ) {
help();
}
else {
my $ret = exec_command( $command, @opts );
if ( $show_status{$org_command} ) {
print $itunes->state;
}
my $show_return = $return_dispatch{$command} ? 1 : 0;
$ret = join "\n", @$ret if ref $ret eq 'ARRAY';
print $ret if $show_return;
}
$term->addhistory($_) if /^\S$/;
}
sub exec_command {
my ( $command, @opts ) = @_;
my $ret;
if ( $command eq 'play' and @opts ) {
$command = 'play_track';
unshift @opts, 1;
}
eval { $ret = @opts ? $itunes->$command(@opts) : $itunes->$command; };
warn $@ if $@;
return $ret;
}
sub help {
print << 'HELP';
command list:
a, activate
start itunes if it stopped.
q, quit
quit itunes if it activated.
play [playlist]
start track.
pl, playlist, get_playlists
show list of iTunes playlist.
stop
stop current track.
pause
pause current track.
next
skip current track.
p, playpause
switch play, pause.
c, current_trach_name
show current track name.
h, help
show this message.
HELP
}
MacRubyがObjective-C(or AppleScript)を乗っ取る
タイトルをつけた人の意図はそこにはないとは思うけど、 これは結構ありそう。
もちろん、Objective-Cのすべてを奪うことはありえないけど、 MacRubyレベルでObjective-Cとの融合が進むと カジュアルな利用はRubyで行うというのは予想以上に広まるかも。
また、AppleScriptの英語モドキよりもいっそRubyのような 「普通のプログラミング言語」を使った方がわかりやすい という人も多いだろう。
sudo cpan
install Mac::Growl
use Mac::Growl qw(RegisterNotifications);
RegisterNotifications(
$appname, # 登録するアプリケーション名
['alert', 'note', 'info'], # 登録したアプリケーションで使用する通知の種類。
# 自分が分かりやすい名前にしておくといいと思う
['note'] # デフォルトで使用する通知の種類。なんで複数の値を渡せるのかは知らない
);
use Mac::Growl qw(PostNotification);
PostNotification(
$appname, # 先ほど登録したアプリケーション名
'alert', # 通知の種類。登録してあるalert, note, infoのうちのどれかを渡す。
$title, # 表示されるタイトル。日本語を渡す場合は、
# Encode等を使用して、UTF-8にしてあげる必要があるようだ。
$description, # 表示される内容。
);