如何解决我的AppleScript删除一个选择的iTunes曲目?
-
12-09-2019 - |
题
我创建下面的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
当我选择一个项目它工作正常,但是当我选择多于一个我得到:“无法获得选择第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
但后来我得到“无法获得第1项的申请‘iTunes’的选择第1项的。”我认为这个问题是“this_track”是不是类轨道的物体,而是一个选择的项目。如何从选择项目获得实际的跟踪对象?
如果您没有看到解决办法,我会欢迎调试技巧或任何其他建议。
解决方案
变量this_track
是一个对象指定符的引用。你必须使用contents
属性来获取被封闭的物体符。这同样适用于在第二回路访问变量pair
真。参见类reference
类基准区间在 AppleScript的语言引导一>
在列表to_delete
正在修建的方式中存在的另一个问题。声明set to_delete to to_delete & pair
不会产生对的列表,但一个平坦的列表。参见类list
类基准区间在 AppleScript的语言引导一>
这是你的第二个脚本,而这些漏洞已被删除的版本:
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 {contents of this_track, floc}
copy pair to end of to_delete
end if
end try
end repeat
repeat with pair in to_delete
set the_track to item 1 of contents of pair
set floc to item 2 of contents of pair
delete the_track
my delete_the_file(floc)
end repeat
end if
end tell
不隶属于 StackOverflow