2012年3月1日木曜日

[AppleScript] iTunesの歌詞をQuickLookでカッコよく表示する

iTunes系連続ポストの締め。
せっかく集めた歌詞なので、Macでもいい感じに表示できるようにしました。
今回は歌詞だけど他に応用できる部分がかなりあるので、アイデアだけでも読んでってくださいな。

元ネタは手書き説明書さんのこの記事。
iTunesで再生中の曲の歌詞をQuickLookで表示するAppleScript | Macの手書き説明書

これでちゃんと表示できるんですが、フォント小さくてちょっと読みづらいので大きく表示できるようにしました。
同じく手書き説明書さんの記事でLaunchBarだと大きく表示できたりするんですけど、Alfredじゃそんなのできないしってことで若干無理やりです。

QuickLookはhtmlを表示できるのだ(`・ω・´)


ええ常識ですよね、今まで知りませんでしたごめんなさい。
こないだhtmlまるごと保存して気づきましたorz

QuickLookでテキストを表示すると文字サイズとかフォントとか勝手に固定されちゃいますが、htmlだと自由に変えられます。
上の記事中にあるスクリプトは歌詞をテキストで保存→QuickLookで表示って流れなんですが、じゃあhtmlで保存すればよくね?ってのがこのスクリプト。(コード長いんで最後に書いてます)

保存場所は
/Users/ユーザー名/Library/iTunes/Scripts/

/Users/ユーザー名/Library/Scripts/
がいいと思います。それぞれの利点は次の記事に書きます。
AlfredのPowerPackユーザーさんはグローバルショートカット振るといいと思うよ!

ちなみに実行するとこんな感じ。

120229_02

そこそこ大きいのでふんぞり返って座っても読めます。どんどん猫背になれます。
(いま気づいたけどセンターがズレてる・・・気になる人は直してください)

これでiTunes系記事はオシマイですが、実は歌詞の表示場所に困ってたのが発端です。
元々は壁紙に貼るタイプのアプリを使ってたんですが

120229_03

うん、さすがにこれは読めんわ・・・(・ω・`)
壁紙が自由に変えられなくなっちゃって悲しかったので、じゃあ他の方法で表示しないとーってことでした。
これからは好きな壁紙にできるし、デスクトップがアイコンで溢れても大丈夫!

実はちょっと普通のQuickLookとは違うよ


QuickLookはSpace押すと簡単に終了できますけど、この子は無理です。
メンドイけど左上の×ボタン押さないといけません。どうやら仕方ないんだそうです。
使用頻度次第だけど、終了用のスクリプト作るのもいいかもですね。
(できるかは知らないけど)強制終了させるとか、左上の×ボタンの座標押すとか。
なんか思いついたら作るかもです。

html表示すればわりと何でもできるんじゃね?


ちょっと別の話になるけど、QuickLookでhtml表示させたらけっこういろんなことできるんじゃないの?という気がしてきました。
だってWebベースの表示だよ?リンク貼れるんだよ?やりたい放題なんじゃないの?
まだ大した構想じゃないけど、近いうち形にしてみたいと思います。

長くなったけど今回のスクリプトコードがこちら。
「レートとかいらんわ」って人は好き勝手いじってくださいな。
tell application "iTunes"
try
set thisTrack to current track
on error errMsg number errNum
return
end try

set i_artist to artist of current track
set i_title to the name of current track
set i_album to album of current track
set i_lyrics to lyrics of current track
set i_rate to rating of current track

if i_rate = 0 then
set hoshi to "・・・・・"
end if
if i_rate = 20 then
set hoshi to "★・・・・"
end if
if i_rate = 40 then
set hoshi to "★★・・・"
end if
if i_rate = 60 then
set hoshi to "★★★・・"
end if
if i_rate = 80 then
set hoshi to "★★★★・"
end if
if i_rate = 100 then
set hoshi to "★★★★★"
end if

end tell

if i_lyrics = "" then
display dialog "歌詞がないです" buttons {"OK"} default button 1 giving up after 2
else
set h_path to the path to home folder as Unicode text
set lyric_path to h_path & "quicklook_lyrics.html" as text

tell application "Finder" to make file at h_path with properties {name:"quicklook_lyrics.html"}
write i_lyrics to file lyric_path as text
set i_lyrics_2 to do shell script "
tr \"\\r\" \"\\n\" <~/quicklook_lyrics.html > ~/quicklook_lyrics_2.html;
sed -e 's/$/
/g;' ~/quicklook_lyrics_2.html;
"
set i_lyrics_3 to "




" & i_title & "
" & i_artist & "
" & i_album & "



" & hoshi & "



" & i_lyrics_2 & "




"
set lyric_path_3 to h_path & "quicklook_lyrics_3.html" as text
tell application "Finder" to make file at h_path with properties {name:"quicklook_lyrics_3.html"}
write i_lyrics_3 to file lyric_path_3 as text

do shell script "
iconv -f SHIFT-JIS -t UTF-8 ~/quicklook_lyrics_3.html > ~/quicklook_lyrics_3_iconv.html;
xattr -w com.apple.TextEnvoding \"UTF-8;134217984\" ~/quicklook_lyrics_3_iconv.html;
qlmanage -p ~/quicklook_lyrics_3_iconv.html;
rm ~/quicklook_lyrics.html ~/quicklook_lyrics_2.html ~/quicklook_lyrics_3.html ~/quicklook_lyrics_3_iconv.html;
"
end if

1 件のコメント:

  1. I became just looking for this info for a while. After 6 hours of continuous Googleing, finally I obtained it in your website. I wonder is there a insufficient Google strategy that do not rank such a informative websites in the surface of the list. Usually the top websites are filled with garbage.

    返信削除