在文本项列表中查找特定文本的 Applescript
Applescript that looks for specific text in a list of text items
我有很多深奥的 CD,它们的曲目名称并不总是在 CDDB 中。所以我正在尝试编写一个 Applescript,它将获取我从 AllMusic 复制的歌曲标题列表,并将它们作为曲目名称粘贴到 iTunes 中。从 AllMusic 复制曲目名称列表时,格式几乎总是:
曲目#
标题
作曲家
轨道长度
A link 到 Spotify 或 Amazon
对专辑中包含的所有曲目重复此操作。我写了下面的 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",并添加:
如果我等于 k 那么
将 i 设置为 i + 1
将 k 设置为 k + 1
万一
在重复循环的末尾。然后脚本会跳 4 段。如果计数器 'k' 与它正在查看的段落不匹配(例如,如果 k 是“2”而段落 i 不是“2”,那一定意味着它是一组 5 个段落,所以 "i" 会提前一个。不是很好的脚本,但在大多数情况下它可能会工作。问题是我永远无法将 "if i is equal to k" 到 return 设为真,即使它是真的。这是完整代码:
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
感谢您确认格式。主要问题是它不是一种简单的格式!因此,脚本必须查看下一个选项卡或 return 序列以确定什么是什么。
我制作了下面的脚本,它可以满足您的需要。我包含了许多评论以使其清楚(我希望!)。第一行解释了脚本必须为 composer 和 link(每个 yes/no)处理的 4 种可能的文本格式。
我已经在同一文本中按顺序测试了 4 种格式的脚本。您必须添加您的 iTunes 例程,它可以使用定义的变量:TTrack、TComposer(可以为空)、TArtist、TLength 和 TLink(可以为空)。
我在必须添加 iTunes 例程的地方插入评论。
脚本循环直到剪贴板中的所有 txt 都被使用。
(* format of input text is made of :
track# <tab> title <return> composer <return> artist <return> track_length <tab> link <tab>
- title, artist and track_length are mandatory
- composer and link are optional
Therefore, there are 4 possible formats :
track# <tab> title <return> composer <return> artist <return> track_length <tab> link <tab> (= complete)
track# <tab> title <return> artist <return> track_length <tab> link <tab> ( = no composer)
track# <tab> title <return> composer <return> artist <return> track_length <tab> ( = no link)
track# <tab> title <return> artist <return> track_length <tab> ( = no link, no composer)
*)
set Ctab to ASCII character 9
set Creturn to ASCII character 13
set txt to the clipboard
repeat while length of txt > 1
set AppleScript's text item delimiters to Creturn
set Track_Title to text item 1 of txt -- the first item always contains Track# tab Title
set L to (length of Track_Title) + 1 -- L will be the ofset for next track
set T2 to text item 2 of txt -- the second item could be composer or artist
set T3 to text item 3 of txt -- the third item could be artist or (track_length tab link tab track# tab title)
if (offset of Ctab in T3) = 0 then -- the third item is artist
set TComposer to T2
set TArtist to T3
set L to L + (length of TComposer) + 1 + (length of TArtist) + 1
set NextT to text item 4 of txt -- NextT = (track_length tab link tab track# tab title) or (track_length tab track# tab title)
else -- the third item is (track_length tab link tab track# tab title) or (track_length tab track# tab title)
set TComposer to ""
set TArtist to T2
set L to L + (length of TArtist) + 1
set NextT to text item 3 of txt
end if
set AppleScript's text item delimiters to Ctab
set TTrack to text item 1 of Track_Title
set TTitle to text item 2 of Track_Title
set TLength to text item 1 of NextT
set L to L + (length of TLength) + 1
if (count of text items of NextT) is 4 then -- format is (track_length tab link tab track# tab title)
set TLink to text item 2 of NextT
set L to L + (length of TLink) + 1
else -- format is (track_length tab track# tab title)
set TLink to ""
end if
-- replace this dialog by your iTunes procedure !!!!!!
display dialog "track=" & TTrack & return & "title=" & TTitle & return & "composer=" & TComposer & return & "Artist=" & TArtist & return & "length=" & TLength & return & "Link=" & TLink & return & "L=" & L
if length of txt > (L + 1) then
set txt to text (L + 1) thru -1 of txt -- remove all current track from text
else
set txt to ""
end if
end repeat
我有很多深奥的 CD,它们的曲目名称并不总是在 CDDB 中。所以我正在尝试编写一个 Applescript,它将获取我从 AllMusic 复制的歌曲标题列表,并将它们作为曲目名称粘贴到 iTunes 中。从 AllMusic 复制曲目名称列表时,格式几乎总是:
曲目# 标题 作曲家 轨道长度 A link 到 Spotify 或 Amazon
对专辑中包含的所有曲目重复此操作。我写了下面的 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",并添加: 如果我等于 k 那么 将 i 设置为 i + 1 将 k 设置为 k + 1 万一 在重复循环的末尾。然后脚本会跳 4 段。如果计数器 'k' 与它正在查看的段落不匹配(例如,如果 k 是“2”而段落 i 不是“2”,那一定意味着它是一组 5 个段落,所以 "i" 会提前一个。不是很好的脚本,但在大多数情况下它可能会工作。问题是我永远无法将 "if i is equal to k" 到 return 设为真,即使它是真的。这是完整代码:
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
感谢您确认格式。主要问题是它不是一种简单的格式!因此,脚本必须查看下一个选项卡或 return 序列以确定什么是什么。
我制作了下面的脚本,它可以满足您的需要。我包含了许多评论以使其清楚(我希望!)。第一行解释了脚本必须为 composer 和 link(每个 yes/no)处理的 4 种可能的文本格式。
我已经在同一文本中按顺序测试了 4 种格式的脚本。您必须添加您的 iTunes 例程,它可以使用定义的变量:TTrack、TComposer(可以为空)、TArtist、TLength 和 TLink(可以为空)。
我在必须添加 iTunes 例程的地方插入评论。
脚本循环直到剪贴板中的所有 txt 都被使用。
(* format of input text is made of :
track# <tab> title <return> composer <return> artist <return> track_length <tab> link <tab>
- title, artist and track_length are mandatory
- composer and link are optional
Therefore, there are 4 possible formats :
track# <tab> title <return> composer <return> artist <return> track_length <tab> link <tab> (= complete)
track# <tab> title <return> artist <return> track_length <tab> link <tab> ( = no composer)
track# <tab> title <return> composer <return> artist <return> track_length <tab> ( = no link)
track# <tab> title <return> artist <return> track_length <tab> ( = no link, no composer)
*)
set Ctab to ASCII character 9
set Creturn to ASCII character 13
set txt to the clipboard
repeat while length of txt > 1
set AppleScript's text item delimiters to Creturn
set Track_Title to text item 1 of txt -- the first item always contains Track# tab Title
set L to (length of Track_Title) + 1 -- L will be the ofset for next track
set T2 to text item 2 of txt -- the second item could be composer or artist
set T3 to text item 3 of txt -- the third item could be artist or (track_length tab link tab track# tab title)
if (offset of Ctab in T3) = 0 then -- the third item is artist
set TComposer to T2
set TArtist to T3
set L to L + (length of TComposer) + 1 + (length of TArtist) + 1
set NextT to text item 4 of txt -- NextT = (track_length tab link tab track# tab title) or (track_length tab track# tab title)
else -- the third item is (track_length tab link tab track# tab title) or (track_length tab track# tab title)
set TComposer to ""
set TArtist to T2
set L to L + (length of TArtist) + 1
set NextT to text item 3 of txt
end if
set AppleScript's text item delimiters to Ctab
set TTrack to text item 1 of Track_Title
set TTitle to text item 2 of Track_Title
set TLength to text item 1 of NextT
set L to L + (length of TLength) + 1
if (count of text items of NextT) is 4 then -- format is (track_length tab link tab track# tab title)
set TLink to text item 2 of NextT
set L to L + (length of TLink) + 1
else -- format is (track_length tab track# tab title)
set TLink to ""
end if
-- replace this dialog by your iTunes procedure !!!!!!
display dialog "track=" & TTrack & return & "title=" & TTitle & return & "composer=" & TComposer & return & "Artist=" & TArtist & return & "length=" & TLength & return & "Link=" & TLink & return & "L=" & L
if length of txt > (L + 1) then
set txt to text (L + 1) thru -1 of txt -- remove all current track from text
else
set txt to ""
end if
end repeat