在文本项列表中查找特定文本的Applescript

时间:2016-02-29 01:04:43

标签: applescript itunes

我有很多深奥的CD,其曲目名称并不总是在CDDB中。所以我正在尝试编写一个Applescript,它将收集我从AllMusic复制的歌曲标题列表,并将它们作为曲目名称粘贴到iTunes中。从AllMusic复制曲目名称列表时,格式始终为ALMOST:

追踪# 标题 作曲家 跟踪长度 指向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 + 1                 将k设置为k + 1                 万一 在重复循环结束时。然后脚本将跳过4段。如果反“k”与它所查看的段落不匹配(例如,如果k为“2”且段落i不是“2”,则必须表示它是一组5个段落,因此“i”将推进一个。不是很好的脚本,但它可能在大多数情况下都有效。麻烦的是我永远不会得到“如果我等于k”就会返回为真,即使它是真的。这是完整的代码:< / p>

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

1 个答案:

答案 0 :(得分:0)

感谢您对格式的确认。主要问题是它不是一种简单的格式!因此,脚本必须查看下一个选项卡或返回序列是什么,以确定它是什么。

我制作了下面的脚本,它可以满足您的需求。我提出了许多意见以明确(我希望!)。第一行解释了脚本必须为作曲家和链接处理的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