【发布时间】:2016-06-11 22:49:36
【问题描述】:
我有许多深奥的 CD,它们的曲目名称并不总是在 CDDB 中。因此,我正在尝试编写一个 Applescript,它将获取我从 AllMusic 复制的歌曲名称列表,并将它们作为曲目名称粘贴到 iTunes 中。从 AllMusic 复制曲目名称列表时,格式始终为 ALMOST:
曲目# 标题 作曲家 轨道长度 指向 Spotify 或亚马逊的链接
这会重复专辑包含的曲目数量。我编写了以下 Applescript,它在上述格式不变时有效:
set tid to AppleScript's text item delimiters
set txt to the clipboard
set p to count paragraphs of (the clipboard)
set i to 2
tell application "iTunes"
activate
if selection is not {} then -- there ARE tracks selected...
set mySelection to selection
repeat with aTrack in mySelection
if i is less than p then
set AppleScript's text item delimiters to ASCII character 13 -- (a carriage return)
set newTxt to text items of txt -- not text of, text items of
set AppleScript's text item delimiters to tid -- restore text delimiters
set name of aTrack to item i of newTxt
set i to i + 5
end if
end repeat
end if
end tell
问题在于,有时会有一个或两个或多或少的类别(例如缺少作曲家)使我的脚本无法运行。理想情况下,有一种方法可以让脚本遍历文本项列表,找到第 1 轨,粘贴到下一个文本项,找到第 2 轨,粘贴到下一个文本项等。关于如何完成这个?
感谢您的帮助。
这是根据我的评论修改后的代码:
...我尝试在顶部添加“set k to 1”,将“set i to i + 5”更改为“set i to i + 4”,并添加: 如果 i 等于 k 那么 将 i 设置为 i + 1 将 k 设置为 k + 1 万一 在重复循环结束时。然后脚本会跳 4 段。如果计数器“k”与它正在查看的段落不匹配(例如,如果 k 是“2”并且第 i 段不是“2”,则它必须意味着它是一组 5 个段落,所以“i”会前进一个。不是很好的脚本,但它可能在大多数情况下都可以工作。问题是我永远无法让“如果 i 等于 k”返回为真,即使它是。这是完整的代码:
set tid to AppleScript's text item delimiters
set txt to the clipboard
set p to count paragraphs of (the clipboard)
set k to 1
set i to 2
tell application "iTunes"
activate
if selection is not {} then -- there ARE tracks selected...
set mySelection to selection
repeat with aTrack in mySelection
if i is less than p then
set AppleScript's text item delimiters to ASCII character 13 -- (a carriage return)
set newTxt to text items of txt -- not text of, text items of
set AppleScript's text item delimiters to tid -- restore text delimiters
set name of aTrack to item i of newTxt
set i to i + 4
if i is equal to k then
set i to i + 1
set k to k + 1
end if
end if
end repeat
end if
end tell
【问题讨论】:
-
那么,每个数据值似乎有一个段落?一段是标题,另一段是艺术家,另一段是作曲家,等等。对吗?根据您当前的代码很难确定。
-
嗨,克雷格。是的,没错。
-
这里的问题是段落列表中可靠内容的位置,作为脚本的基础,因为您不能合理地依赖当前状态下的段落数。那里甚至有一个空白段落吗?
-
不,所有段落连在一起,没有间隙。一致的一件事是轨道号总是后跟制表符。不确定这是否是某种测试。正如我所提到的,每首曲目几乎总是由 5 个段落组成。如果不是,它几乎总是 4 段。也就是说,我可以假设 99.9% 的情况下,一首曲目将有 4 或 5 个段落,尽管一张 CD 可能包含两种类型的曲目。如果我能让脚本甚至只适用于这些场景,那就足够了。为此(请参阅原始帖子所附的注释)
-
如果每个曲目编号后跟一个制表符,您应该可以使用分隔符先将文本除以制表符,然后再除以回车,您应该只剩下曲目名称,这就是你想要的。
标签: applescript itunes