●アップルスクリプト その3 (iTunes関連)



その1 (web関連)その2その3 (iTunes関連)その4 (ルーチンのようなもの、小ネタ)

iTunes に関するアップルスクリプト集です。
MacOSX 10.4.9、iTunes 7.3.2の使用を前提にしています。
大方はそれ以前のバージョンでも動きます。
テキストを「スクリプトエディタ」にコピペし、保存(フォーマットは「スクリプト」で)、
そのスクリプトを「ユーザ>ライブラリ>iTunes>Scripts」フォルダに入れれば、
(「Scripts」フォルダがなければそこに作ってください)
iTunesのメニューにアップルスクリプトのマークが出て、そこからスクリプトを実行することができます。
例によってエラー処理は適当です。あしからず。

■複数トラックのチェックマークをつける/はずす
■選択項目をAACに変換
■選択項目をAIFFに変換
■選択項目をMP3に変換
■選択項目のフォーマットを変換して現在表示しているプレイリストに追加
■再生回数を変更
■マイレートを変更
■最後に再生した日を変更
■並べ替えを編集
■Podcastの「説明」「長い説明」を編集
■現在再生している曲をフェードアウトで終了させて次の曲を再生する
■選択した曲のアートワークをプレビューで表示
■選択した曲のアートワークを保存
■キャピタライズ(語頭を大文字に)
■追加日を変更

MacOSX10.3.xでインストールされるAppleScriptには、スクリプトをアプリケーション形式で保存すると日本語が通らないという
重大なバグがあります。アプリケーション形式で保存する場合は、以下の手順で日本語に対応させてください。

  (1) 保存時の「フォーマット」を「アプリケーションバンドル」に。
  (2) Finderで「Japanese.lproj」という名前の空フォルダを作っておく。
  (3) Finderで(1)で保存したアプリを選択し、
    情報を見る=>言語=>追加で、(2)のJapanese.lprojを選ぶ。
  (4) 情報を見る=>言語で、「Japanese」にチェックがついていることを確認。



■複数トラックのチェックマークをつける/はずす
チェックがついているものははずれ、ついていないものはつきます。
tell application "iTunes" set seltrack to selection of window 1 repeat with x in seltrack if enabled of x is true then set enabled of x to false else set enabled of x to true end if end repeat end tell


■選択項目をAACに変換
※ビットレートなどの設定はiTunesの「環境設定」の「詳細」>「読み込み」での設定値に従います。
try tell application "iTunes" --aac set thisEncoder to item 1 of encoders set current encoder to thisEncoder convert selection end tell end try


■選択項目をAIFFに変換
※ビットレートなどの設定はiTunesの「環境設定」の「詳細」>「読み込み」での設定値に従います。
try tell application "iTunes" --aiff set thisEncoder to item 2 of encoders set current encoder to thisEncoder convert selection end tell end try


■選択項目をMP3に変換
※ビットレートなどの設定はiTunesの「環境設定」の「詳細」>「読み込み」での設定値に従います。
try tell application "iTunes" --mp3 set thisEncoder to item 4 of encoders set current encoder to thisEncoder convert selection end tell end try


■選択項目のフォーマットを変換して現在表示しているプレイリストに追加
※フォーマットはAAC、AIFF、Appleロスレス、MP3、WAVから選択します。
※ビットレートなどの設定はiTunesの「環境設定」の「詳細」>「読み込み」での設定値に従います。
--選択項目のフォーマットを変換して現在表示しているプレイリストに追加 tell application "iTunes" set enc to choose from list (format of encoders as list) with prompt "変換" without multiple selections allowed if enc is false then return set enc to item 1 of (encoders whose format is (item 1 of enc)) set current encoder to enc set cd to (current date) set seltrack to a reference to selection set selplaylist to (view of window 1) set psid to {} repeat with x in seltrack set end of psid to persistent ID of x end repeat repeat with y in psid set x to item 1 of (tracks of (view of window 1) whose persistent ID is y) delay 1 try convert x end try repeat try set convtrack to track 1 of playlist "最近追加した曲" if (name of x is name of convtrack) and (artist of x is artist of convtrack) and (date added of convtrack > cd) then if selplaylist is not smart then duplicate convtrack to selplaylist exit repeat end if end try end repeat end repeat end tell


■再生回数を変更(数値選択式)
tell application "iTunes" try set trk to {} set slct to a reference to selection if slct as list is {} then return repeat with x in slct set end of trk to (name of x as Unicode text) & (" (" & played count of x & ")") end repeat set AppleScript's text item delimiters to ", " set y to choose from list {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20} with prompt ((trk as Unicode text) & " の再生回数を変更") if y is false then return set played count of slct to y end try end tell


■再生回数を変更(数値入力式)
tell application "iTunes" try set trk to {} set slct to a reference to selection if slct as list is {} then return repeat with x in slct set end of trk to (name of x as Unicode text) & (" (" & played count of x & ")") end repeat set AppleScript's text item delimiters to ", " set y to text returned of (display dialog ((trk as Unicode text) & " の再生回数を変更") default answer "") if y is false then return set played count of slct to y end try end tell


■マイレートを変更
tell application "iTunes" try set trk to {} set slct to a reference to selection if slct as list is {} then return repeat with x in slct set end of trk to (((name of x as Unicode text) & " (" & (rating of x) div 20)) & ")" end repeat set AppleScript's text item delimiters to ", " set y to choose from list {0, 1, 2, 3, 4, 5} with prompt ((trk as Unicode text) & " のレートを変更") if y is false then return set rating of slct to (y * 20) end try end tell


■最後に再生した日を変更
--最後に再生した日を変更 tell application "iTunes" set seltrack to a reference to selection repeat with x in seltrack set pldate to ((played date) of x as string) if pldate is "missing value" then set pldate to (current date) as string set newdate to text returned of (display dialog "最後に再生した日を変更" default answer pldate) set (played date) of x to date newdate end repeat end tell


■並べ替えを編集
※並べ替え(ソート、よみがな)が導入されたiTunes7.1以降で動きます。
アルバム、アーティストの並べ替えを一括して編集したいときに。
--並べ替えを編集 tell application "iTunes" set seltrack to a reference to selection set dd to (display dialog "並べ替え" default answer (artist of item 1 of seltrack as Unicode text) buttons {"Cancel", "Album", "Artist"}) set textret to text returned of dd set alar to button returned of dd set psid to {} repeat with x in seltrack set end of psid to persistent ID of x end repeat repeat with y in psid set x to item 1 of (tracks of (view of window 1) whose persistent ID is y) if alar is "Artist" then set (sort artist) of x to (textret as Unicode text) if alar is "Album" then set (sort album) of x to (textret as Unicode text) end repeat end tell


■Podcastの「説明」「長い説明」を編集
tell application "iTunes" set seltrack to a reference to selection set psid to {} repeat with x in seltrack set end of psid to persistent ID of x end repeat repeat with y in psid set x to item 1 of (tracks of (view of window 1) whose persistent ID is y) --操作 set olddes to description of x set oldlon to long description of x set newdes to (display dialog (name of x) & return & "説明を変更" default answer olddes buttons {"キャンセル", "飛ばす", "OK"} default button 3) if button returned of newdes is "OK" then set description of x to text returned of newdes set newlon to (display dialog (name of x) & return & "長い説明を変更" default answer oldlon buttons {"キャンセル", "飛ばす", "OK"} default button 3) if button returned of newlon is "OK" then set long description of x to text returned of newlon end repeat end tell


■現在再生している曲をフェードアウトで終了させて次の曲を再生する
なんか用途はあるかな?
tell application "iTunes" try set currentvolume to (volume adjustment) of current track delay 1 set gain to -5 repeat until gain < -100 set (volume adjustment) of current track to currentvolume + gain delay 0.1 set gain to gain - 5 end repeat delay 1 pause set (volume adjustment) of current track to currentvolume play (next track) end try end tell


■選択した曲のアートワークをプレビューで表示
プレビューはアップルスクリプト未対応なのでコマンドNの動作を再現。
例によって実行前のクリップボードの中身は一旦変数に保存して戻します。
--アートワークをプレビューで表示 set cb to the clipboard as record tell application "iTunes" set seltrack to a reference to selection set theartwork to data of artwork 1 of item 1 of seltrack set the clipboard to theartwork end tell tell application "System Events" tell application "Preview" to activate repeat if (visible of process "Preview") is true then exit repeat end repeat tell application process "Preview" delay 1 keystroke "n" using command down delay 1 end tell end tell set the clipboard to cb


■選択した曲のアートワークを保存
曲のアートワークを任意のフォルダに保存するスクリプト。
JPEG、PNGが保存可能。他のソフトによって埋め込まれたBMPなどには非対応。
(09.5.18)複数のアートワークを保存できるようにした。
(09.11.4)OSX10.6に対応。
--アートワークを保存 tell application "iTunes" set seltrack to a reference to selection set savefolder to (choose folder with prompt "アートワークの保存場所を指定") repeat with x in seltrack set {theartist, thealbum, ext} to {artist of x, album of x, ""} repeat with y from 1 to (count artworks of x) set err to 0 try set theartwork to raw data of artwork y of x set theformat to format of artwork y of x as string if theformat contains "JPEG" then set ext to ".jpg" if theformat contains "PNG" then set ext to ".png" if ext is "" then display dialog theartist & " ¥"" & thealbum & "¥" " & "のアートワークは書き出せません。" set err to 1 end if on error display dialog theartist & " ¥"" & thealbum & "¥" " & "にはアートワークがありません。" set err to 1 end try if err is 0 then set thefilename to theartist & " - " & thealbum & " " & y & ext if character 1 of thefilename is "." then set thefilename to "_" & characters 2 thru -1 of thefilename set newfilename to "" repeat with ltr in thefilename set ltr to ltr as string if ltr is "/" or ltr is ":" then set ltr to "_" set newfilename to newfilename & ltr end repeat try tell application "Finder" to make new file at savefolder with properties {name:newfilename} on error tell application "Finder" if (exists of (((savefolder as string) & newfilename) as alias)) is true then tell application "iTunes" set skipreplace to button returned of (display dialog theartist & " ¥"" & thealbum & "¥" " & "と同じ名前のファイルがすでにあります。" buttons {"キャンセル", "置き換え", "スキップ"} default button 3) end tell end if if skipreplace is "スキップ" then set err to 1 end tell end try end if if err is 0 then set thefile to ((savefolder as string) & newfilename) as alias open for access thefile with write permission write theartwork to thefile close access thefile end if end repeat end repeat end tell OSX 10.4で上のがだめな場合、下のを --アートワークを保存 10.4 tell application "iTunes" set seltrack to a reference to selection repeat with x in seltrack set {theartist, thealbum, ext} to {artist of x, album of x, ""} set savefolder to (choose folder with prompt theartist & " ¥"" & thealbum & "¥" " & "のアートワークの保存場所を指定") repeat with y from 1 to (count artworks of x) set err to 0 try set theartwork to data of artwork y of x set theformat to format of artwork y of x as string if theformat contains "JPEG" then set ext to ".jpg" if theformat contains "PNG" then set ext to ".png" if ext is "" then display dialog theartist & " ¥"" & thealbum & "¥" " & "のアートワークは書き出せません。" set err to 1 end if on error display dialog theartist & " ¥"" & thealbum & "¥" " & "にはアートワークがありません。" set err to 1 end try if err is 0 then set thefilename to theartist & " - " & thealbum & " " & y & ext if character 1 of thefilename is "." then set thefilename to "_" & characters 2 thru -1 of thefilename set newfilename to "" repeat with ltr in thefilename set ltr to ltr as string if ltr is "/" or ltr is ":" then set ltr to "_" set newfilename to newfilename & ltr end repeat try tell application "Finder" to make new file at savefolder with properties {name:newfilename} on error tell application "Finder" if (exists of (((savefolder as string) & newfilename) as alias)) is true then tell application "iTunes" set skipreplace to button returned of (display dialog theartist & " ¥"" & thealbum & "¥" " & "と同じ名前のファイルがすでにあります。" buttons {"キャンセル", "置き換え", "スキップ"} default button 3) end tell end if if skipreplace is "スキップ" then set err to 1 end tell end try end if if err is 0 then set thefile to ((savefolder as string) & newfilename) as alias open for access thefile with write permission write theartwork to thefile set theartwork to read thefile from 223.0 set eof of thefile to 0 write theartwork to thefile close access thefile end if end repeat end repeat end tell


■キャピタライズ(語頭を大文字に)
たとえば「steve howe」を「Steve Howe」に、「DON'T」を「Don't」に、
「Close to the Edge」を「Close To The Edge」に、変換。
「McCartney」「O'Sullivan」「Newton-John」のような父称姓や複合姓に対応。
「DeBarge」「LeAnne」「ABC」「INXS」などには非対応。
固有名詞はキャピタライズの例外が多くて幅広く対応するのは難しいけど、
例外リストを作ってアレコレやるのもどうかと思うので、そこは割り切る。
なお、スクリプト自体かなり洗練されていないので、ちゃんと動かないケースもあるかも。
(09.5.18)アポストロフィーがらみの処理を修正
--capitalize 1.1 tell application "iTunes" set slc to a reference to selection set taisyo to choose from list {"すべて", "曲名のみ", "アーチスト名のみ", "アルバム名のみ"} with prompt "キャピタライズする項目" default items {"すべて"} with multiple selections allowed if taisyo is false then return repeat with w in slc set koumoku to {} set koumokucheck to {} set capkoumoku to {} if taisyo contains {"すべて"} or taisyo contains {"曲名のみ"} then set end of koumoku to (name of w) set end of koumokucheck to 1 end if if taisyo contains {"すべて"} or taisyo contains {"アーチスト名のみ"} then set end of koumoku to (artist of w) set end of koumokucheck to 2 end if if taisyo contains {"すべて"} or taisyo contains {"アルバム名のみ"} then set end of koumoku to (album of w) set end of koumokucheck to 3 end if --各項目を変換 repeat with y in koumoku set capstrings to {} set thestrings to y as string set thestrings to " " & thestrings set lostrings to do shell script "echo " & quoted form of thestrings & " | tr A-Z a-z" set upstrings to do shell script "echo " & quoted form of thestrings & " | tr a-z A-Z" --一字づつ変換判定 set ji to "" set x to 4 repeat (count (characters of thestrings)) - 3 times considering case -- alphabet if character (x - 1) of lostrings is not character (x - 1) of upstrings then set ji to character x of lostrings -- kigou reigai if character (x - 1) of lostrings is character (x - 1) of upstrings then set ji to character x of upstrings -- number reigai if (ASCII number of character (x - 1) of lostrings) > 47 and (ASCII number of character (x - 1) of lostrings) < 58 then set ji to character x of lostrings -- apostrophe reigai if (character (x - 1) of lostrings is "'") and (character (x - 2) of lostrings is not character (x - 2) of upstrings) and (character (x + 1) of lostrings is " ") then set ji to character x of lostrings -- apostrophe reigai 2 if (character (x - 1) of lostrings is "'") and (character (x - 2) of lostrings is not character (x - 2) of upstrings) and (character x of lostrings is in "lrv") then set ji to character x of lostrings --Mc reigai if (character (x - 3) of lostrings is character (x - 3) of upstrings) and (character (x - 2) of lostrings is "m") and (character (x - 1) of lostrings is "c") then set ji to character x of upstrings --O' reigai if (character (x - 3) of lostrings is character (x - 3) of upstrings) and (character (x - 2) of lostrings is "o") and (character (x - 1) of lostrings is "'") then set ji to character x of upstrings set end of capstrings to ji set x to x + 1 end considering end repeat set end of capkoumoku to capstrings as Unicode text end repeat --iTunesに変換した項目を入力 if koumokucheck is {1, 2, 3} then set name of w to item 1 of capkoumoku as Unicode text set artist of w to item 2 of capkoumoku as Unicode text set album of w to item 3 of capkoumoku as Unicode text end if if koumokucheck is {1, 2} then set name of w to item 1 of capkoumoku as Unicode text set artist of w to item 2 of capkoumoku as Unicode text end if if koumokucheck is {1, 3} then set name of w to item 1 of capkoumoku as Unicode text set album of w to item 2 of capkoumoku as Unicode text end if if koumokucheck is {2, 3} then set artist of w to item 1 of capkoumoku as Unicode text set album of w to item 2 of capkoumoku as Unicode text end if if koumokucheck is {1} then set name of w to item 1 of capkoumoku as Unicode text if koumokucheck is {2} then set artist of w to item 1 of capkoumoku as Unicode text if koumokucheck is {3} then set album of w to item 1 of capkoumoku as Unicode text end repeat end tell


■追加日を変更
曲をiTunesから一旦削除し、システムの日時を戻し、再びiTunesに登録する
という一連の作業を自動化したものです。
システムの日時は曲ファイルの作成日+1分を基準に設定しています。
1曲単位または1アルバム単位で選択して作業してください。
チェックマーク、レート、再生回数、最後に再生した日、iTunesがダウンロードしたアートワーク などが消えてしまうので、ご了承ください。
--change added date property logpass : "<< login password >>" --OSXのログイン用のパスワードを書きます --曲を選択 tell application "iTunes" set slct to a reference to selection if slct as list is {} then return set x to (item 1 of slct) set loc to location of x end tell --ファイル作成日時を取得 tell application "Finder" to set loc_date to creation date of loc set cr_date to text returned of (display dialog "" default answer loc_date as Unicode text) set cr_date to (date cr_date) as date set crmo to month of cr_date as number if crmo < 10 then set crmo to "0" & crmo set crda to day of cr_date if crda < 10 then set crda to "0" & crda set crho to hours of cr_date if crho < 10 then set crho to "0" & crho set crmi to (minutes of cr_date) + 1 if crmi < 10 then set crmi to "0" & crmi set crye to year of cr_date set cr_date to crmo & crda & crho & crmi & crye as Unicode text --システム日時を変更 do shell script "echo '" & logpass & "' | sudo -S date " & cr_date --曲を一旦削除 tell application "iTunes" to delete selection delay 1 --ファイルを再度追加 tell application "Finder" set slctloc to items in container of loc end tell tell application "iTunes" repeat with x in slctloc add x as alias end repeat end tell


2ちゃんねる新mac板「おもろい、めずらしいアップルスクリプト発表会」もよろしく。



トップページへ戻る

[PR]DoCoMop̕KI:̉^Ӓق̊ف