Applescript-如何在轨迹上迭代
我是applescript的新手。我试着从各种渠道学习,比如,和这个 为了便于学习,我尝试使用以下代码在屏幕上打印所有曲目名称:Applescript-如何在轨迹上迭代,applescript,itunes,Applescript,Itunes,我是applescript的新手。我试着从各种渠道学习,比如,和这个 为了便于学习,我尝试使用以下代码在屏幕上打印所有曲目名称: tell application "iTunes" set myTracks to (tracks of library playlist 1) repeat with aTrack in myTracks get name of aTrack end repeat end tell 但它只打印一个曲目名称,可能是最后一个 那
tell application "iTunes"
set myTracks to (tracks of library playlist 1)
repeat with aTrack in myTracks
get name of aTrack
end repeat
end tell
但它只打印一个曲目名称,可能是最后一个
那么,迭代列表的最佳方式是什么
蒂亚
鲍勃:我认为你的答案是正确的。我相信Apple脚本编辑器列中的结果只打印脚本的最后一个结果。如果查看事件和回复,您应该会看到脚本正确返回了答案 我试着用这个脚本:
tell application "iTunes"
set myTracks to (tracks of library playlist 1)
repeat with i from 1 to number of items in myTracks
get name of item i of myTracks
end repeat
从答复中可以看出:
- 获取的文件跟踪id 4050的名称 源id的库播放列表id 3379 41
- 获取的文件跟踪id 4051的名称 源id的库播放列表id 3379 41
tell application "iTunes"
set myTracks to (tracks of library playlist 1)
repeat with i from 1 to number of items in myTracks
display dialog name of item i of myTracks as string
end repeat
end tell
所以它是有效的,你只需要在循环结束前做你想做的事情
另外,我建议您阅读苹果官方文档:AppleScript语言指南。免费和非常完整的开始
希望这有帮助 您的代码很好;似乎什么也没有发生的原因是,
get…
所做的只是查找一个值并返回它。但是,您不会对返回的值执行任何操作,因此它将被忽略,只有循环的最后一次迭代才会返回任何内容。您需要在循环内部执行外部世界可见的操作:分配变量、显示对话框等等
如果要收集项目名称列表,可以执行以下操作:
tell application "iTunes"
set trackNames to {}
repeat with aTrack in tracks of library playlist 1
set trackNames to trackNames & name of aTrack
end repeat
end tell
但是,您可以将其收紧。AppleScript的一个强大功能是,正如您可以获取曲目名称一样,您也可以获取列表中每个曲目的名称,并对其进行迭代:
tell application "iTunes"
set trackNames to {}
repeat with aName in name of tracks of library playlist 1
set trackNames to trackNames & aName
end repeat
end tell
但是在这一点上,你甚至不需要循环,你可以使用更简单的方法
tell application "iTunes" to name of tracks of library playlist 1
作为奖励,它会快得多:在我做的一个快速测试中,三个版本分别用了16.189秒、32.656秒和0.296秒。只想补充一点,这些类型的AppleScript脚本在MacOS Catalina上仍然有效,但由于iTunes已重命名为“音乐”,您必须更改
告诉应用程序“iTunes”
到告诉应用程序“音乐”
tell application "iTunes"
set trackNames to {}
repeat with aName in name of tracks of library playlist 1
set trackNames to trackNames & aName
end repeat
end tell
tell application "iTunes" to name of tracks of library playlist 1