使用 AppleScript 选择 iTunes 中的所有曲目

发布于 2024-10-10 22:27:21 字数 530 浏览 3 评论 0原文

我有一个非常基本的 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 中选择曲目?感谢您的任何帮助。

I have this very basic AppleScript which I'm trying to run on my Mac to remove the ratings from all my songs in 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

I've never written anything in AppleScript before, but thought I'd give it a shot (since it's supposed to be so intuitive). Unfortunately, I receive this error when I try to run the script:

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

What is this error? Is there an alternate way of selecting the tracks in iTunes? Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉世弥音 2024-10-17 22:27:21

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

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

tell application "iTunes" to ... 语法就像一个普通的 tell 块,但它只有一个语句长,并且不不要接受结束告诉。您可以立即对列表中的每个条目自动运行 set 命令,这就是您所需要的。一般来说,你很少需要通过索引进行枚举;例如,对于更接近您的解决方案的东西,有等效的

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

这可以避免索引,并且也可能会更快(尽管单行可能是最快的,如果存在差异)。

I don't entirely know why, but the answer is that the library playlist doesn't actually contain tracks. Strange, I know, but since you just want to run this over every track, there's an even simpler solution. Rather than every track of library, just use every track; this will get literally every track in the application, which is what you're trying to do. And with a few other simplifications, this becomes

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

The tell application "iTunes" to ... syntax is just like an ordinary tell block, but it's only one statement long and doesn't take an end tell. And you can automatically run the set command over every entry in the list at once, so that's all you need. In general, you rarely need to enumerate via indices; for instance, for something closer to your solution, there's the equivalent

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

This avoids the indexing, and will also likely be faster (though the one-liner will probably be fastest, if there's a difference).

七色彩虹 2024-10-17 22:27:21

您被误导了: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

如果您有一个大型库,您可能想首先尝试较小的播放列表,例如,在测试播放列表中选择一个曲目,然后替换 在当前播放列表中的repeat语句中。

You've been misinformed: AppleScript is not very intuitive, primarily because so much of its observed behavior is determined by each application's implementation of its object model. While it can be very powerful, you often just have to experiment until you find the right incantations that work for a particular application.

In this case, you need to select the first item of playlists. Note the differences:

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

But what you probably want to do is something more like this:

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

If you have a large library, you might want to first experiment with a smaller playlist, for instance, select a track in a test playlist and then substitute in current playlist in the repeat statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文