Applescriptでプレイリスト作り

動機

ヤスタカの曲をシャッフルで聞きたいなぁ→探すの面倒→あ、Applescriptで簡単に作れないのかな
iTunes for Mac まとめ - アットウィキを参考にしました。
Applescriptの初歩はAppleScriptを使う - I am Electrical machineを参考に

ステップ1

まずは、アーティスト名を入力してプレイリストを作成する機能を実装する。

tell application "iTunes"
	display dialog "Enter the ArtistName" default answer "" buttons {"Cancel", "OK"} default button 2
	copy the result as list to {text_returned, button_pressed}
	
	set thePlaylist to make new playlist with properties {name:text_returned}
	with timeout of 60000 seconds
		duplicate (every track of playlist 1 whose artist contains text_returned) to thePlaylist
	end timeout
end tell

Applescriptシンタックスハイライトがない( ;ω;)
これだけで実装できちゃうのだ。Applescriptすげぇ。

ステップ2

追加できるアーティスト名数*1を増やす

tell application "iTunes"
	display dialog "Enter the PlayListName" default answer "" buttons {"Cancel", "OK"} default button 2
	copy the result as list to {text_returned, button_pressed}
	set thePlaylist to make new playlist with properties {name:text_returned}
	
	repeat
		display dialog "Enter the ArtistName" default answer "" buttons {"Cancel", "OK", "More..."} default button 3
		if button returned of result = "More..." then
			copy the result as list to {text_returned, button_pressed}
			with timeout of 60000 seconds
				duplicate (every track of playlist 1 whose artist contains text_returned) to thePlaylist
			end timeout
		else if button returned of result = "OK" then
			copy the result as list to {text_returned, button_pressed}
			with timeout of 60000 seconds
				duplicate (every track of playlist 1 whose artist contains text_returned) to thePlaylist
			end timeout
			exit repeat
		end if
	end repeat
end tell

あー、疲れた。ステップ2を作るだけで2,3時間くらい格闘してました。最初にPlayListの名前を入力できるようになり、More...を押すとどんどん追加できるようになりました。

テスト

  1. Yasutaka works
  2. capsule
  3. More...
  4. Perfume
  5. More...
  6. MEG
  7. OK

とすると、Yasutaka worksというPlayListが出来て追加したアーティストの曲が入ったよ!

感想

Applescript気持ち悪いっていうか、これやっぱ使い難いんじゃねーかと思ってしまった。注意点を挙げておくと

  • if文の後は改行しましょう
    • ここでかなり詰まりました。エラーが「elseがくるのはおかしい」しか言ってくれないので、おい、if文にelseがダメってどういうことだ?とかなり悩んだ
  • if文の後はend ifを付けましょう
    • 上と同じく。ちょくちょく忘れてはエラーに悩まされました
  • エディタの使い勝手が微妙
    • 保存しないとシンタックスハイライトとかしてくれないのは不便だった。しかも、エラー出ると保存出来ない→読み難い→原因発見が遅れる

ぶっちゃけ、D&Dでやった方が早い気がしたんだぜ。あと、XCodeApplescriptのアプリ作れるの?TigerにはProjectBuilderとかいう奴があったと思うんだけど、検索してもないんだよなー。XCodeに食われちゃったのかな?
文法はPython,Rubyに近い。しかし、ある程度闘わないと使い心地は悪い。

*1:謎い