2

我有这个非常基本的 AppleScript,我试图在我的 Mac 上运行它来删除 iTunes 中所有歌曲的评分:

tell application "iTunes"
    set sel to every track in library playlist
    repeat with i from 1 to the count of sel
        set rating of track i in sel to 0
    end repeat
end tell

我以前从未在 AppleScript 中写过任何东西,但我想我会试一试(因为它应该如此直观)。不幸的是,当我尝试运行脚本时收到此错误:

error "Can’t get every track of library playlist." number -1728 
from every «class cTrk» of «class cLiP»

这是什么错误?是否有另一种选择 iTunes 中曲目的方法?谢谢你的帮助。

4

2 回答 2

2

我不完全知道为什么,但答案是库播放列表实际上并不包含曲目。奇怪,我知道,但由于您只想在每条轨道上运行它,所以有一个更简单的解决方案。而不是every track of library,只需使用every track; 这将得到应用程序中的每一个轨道,这就是你想要做的。再加上一些其他的简化,这就变成了

tell application "iTunes" to set the rating of every track to 0

tell application "iTunes" to ...语法就像一个普通的块tell,但它只有一个语句长并且不需要end tell. 而且您可以一次自动set对列表中的每个条目运行该命令,这就是您所需要的。通常,您很少需要通过索引进行枚举;例如,对于更接近您的解决方案的东西,有等价的

tell application "iTunes"
  repeat with t in every track
    set the rating of t to 0
  end repeat
end tell

这避免了索引,并且也可能更快(尽管单线可能会最快,如果有区别的话)。

于 2011-01-07T00:37:40.817 回答
1

您被误导了:AppleScript 不是很直观,主要是因为它观察到的大部分行为是由每个应用程序对其对象模型的实现决定的。虽然它可能非常强大,但您通常只需要进行试验,直到找到适用于特定应用程序的正确咒语。

在这种情况下,您需要选择播放列表的第一项。注意区别:

get library playlist
    Result:
        library playlist  -- the class
get library playlists
    Result:
        {library playlist id 51776 of source id 67 of application "iTunes"} -- a list
get first library playlist
    Result:
        library playlist id 51776 of source id 67 of application "iTunes" -- first item

但你可能想要做的是更像这样的事情:

tell application "iTunes"
    repeat with tr in every track in first Library playlist
        set rating of tr to 60  -- values are 0 to 100
    end repeat
end tell

如果您有一个大型库,您可能希望首先尝试使用较小的播放列表,例如,在测试播放列表中选择一个曲目,然后in current playlistrepeat语句中替换。

于 2011-01-07T00:39:38.803 回答