iTunes関連のアップルスクリプト



iTunesで複数の曲のチェックマークをつける/はずす

iTunesの曲をAAC/AIFF/Appleロスレス/MP3/WAVに変換する

iTunesの曲の再生回数を変更する

iTunesの曲のマイレートを変更する

iTunesの曲の「最後に再生した日」を変更する

iTunesの曲の「並べ替え」を編集する

iTunesでPodcastの「説明」「長い説明」を編集する

iTunesで現在再生している曲をフェードアウトで終了させて次の曲を再生する

iTunesの曲のアートワークを「プレビュー」で表示する

iTunesの曲のアートワークを保存する

iTunesの曲の追加日を変更する



★ iTunesで複数の曲のチェックマークをつける/はずすアップルスクリプト

チェックがついているものははずれ、ついていないものはつきます。

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


★ iTunesの曲をAAC/AIFF/Appleロスレス/MP3/WAVに変換するアップルスクリプト

ビットレートなどの細かい設定はiTunesの「環境設定」での設定値に従います。

tell application "iTunes"
	set seltrack to a reference to selection as list

	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
	
	try
		convert seltrack
	end try
end tell


★ iTunesの曲の再生回数を変更するアップルスクリプト

数値選択式か数値入力式、使いやすい方を。

--数値選択式はこちら
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


★ iTunesの曲のマイレートを変更するアップルスクリプト

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


★ iTunesの曲の「最後に再生した日」を変更するアップルスクリプト

--最後に再生した日を変更
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


★ iTunesの曲の「並べ替え」を編集するアップルスクリプト

並べ替え(ソート、よみがな)が導入された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


★ iTunesでPodcastの「説明」「長い説明」を編集するアップルスクリプト

tell application "iTunes"
	set seltrack to a reference to selection
	repeat with x in seltrack
		--操作
		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


★ iTunesで現在再生している曲をフェードアウトで終了させて次の曲を再生するアップルスクリプト

なんか用途はあるかな?

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

★ iTunesの曲のアートワークを「プレビュー」で表示するアップルスクリプト
プレビューはアップルスクリプト未対応なのでコマンド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

★ iTunesの曲のアートワークを保存するアップルスクリプト
曲のアートワークを任意のフォルダに保存するスクリプト。JPEG、PNGが保存可能。他のソフトによって埋め込まれたBMPなどには非対応。
--アートワークを保存
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

★ iTunesの曲の追加日を変更するアップルスクリプト
曲を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