選択したすべてのトラックを削除するために、次の AppleScript を作成しました。
property okflag : false
-- check if iTunes is running
tell application "Finder"
if (get name of every process) contains "iTunes" then set okflag to true
end tell
if okflag then
tell application "iTunes"
if selection is not {} then
repeat with this_track in selection
try
try
set cla to class of this_track
set floc to (get location of this_track)
delete this_track
on error error_message number error_number
display alert error_message message ("Error number: ") & error_number & "."
end try
if cla is file track then
my delete_the_file(floc)
end if
end try
end repeat
end if
end tell
end if
to delete_the_file(floc)
try
-- tell application "Finder" to delete floc
do shell script "mv " & quoted form of POSIX path of (floc as string) & " " & quoted form of POSIX path of (path to trash as string)
on error
display dialog "Track deleted, but could not be moved to trash" buttons {"Hmm"} default button 1 with icon 1
end try
end delete_the_file
1 つの項目を選択すると問題なく動作しますが、複数の項目を選択すると、「選択した項目 2 の場所を取得できません」(エラー番号 -1728) が表示されます。これは、トラックを削除すると、スクリプトの選択範囲へのインデックスが破損するためだと思います。
最初に削除するトラックの独自のリストを作成してみようと思いました。
tell application "iTunes"
if selection is not {} then
set to_delete to {}
repeat with this_track in selection
try
set cla to class of this_track
set floc to (get location of this_track)
if cla is file track then
set pair to {this_track, floc}
set to_delete to to_delete & pair
end if
end try
end repeat
repeat with pair in to_delete
set the_track to item 1 of pair
set floc to item 2 of pair
delete the_track
my delete_the_file(floc)
end repeat
end if
end tell
しかし、その後、「アプリケーション「iTunes」の選択項目 1 の項目 1 を取得できません。」というメッセージが表示されます。問題は、「this_track」がクラス Track のオブジェクトではなく、セレクションのアイテムであることだと思います。選択項目から実際のトラック オブジェクトを取得するにはどうすればよいですか?
解決策が見つからない場合は、デバッグに関するヒントやその他の提案を歓迎します。