●アップルスクリプト その3 (iTunes関連) [PR]Ył܂H:{̎͂ɂ



その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などには非対応。
--アートワークを保存 tell application "iTunes" set seltrack to a reference to selection repeat with x in seltrack set err to 0 try set {theartist, thealbum, ext} to {artist of x, album of x, ""} set theartwork to data of artwork 1 of x set theformat to format of artwork 1 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 & 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 set savefolder to (choose folder with prompt theartist & " \"" & thealbum & "\" " & "のアートワークの保存場所を指定") 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 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」などには非対応。
固有名詞はキャピタライズの例外が多くて幅広く対応するのは難しいけど、
例外リストを作ってアレコレやるのもどうかと思うので、そこは割り切る。
なお、スクリプト自体かなり洗練されていないので、ちゃんと動かないケースもあるかも。
--capitalize 1.0.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 --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


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



トップページへ戻る

[PR]p۱èތ:͔hɂѹ60ԕԋۏ