【问题标题】:Applescript that looks for specific text in a list of text items在文本项列表中查找特定文本的 Applescript
【发布时间】: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


【解决方案1】:

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

我制作了下面的脚本,它可以满足您的需求。我包括了许多 cmets 以使其清楚(我希望!)。第一行解释了脚本必须为作曲家和链接处理的 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

【讨论】:

  • 非常感谢!这很好用,我非常感谢您的解释。我学到了很多!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-22
  • 1970-01-01
  • 2016-08-21
  • 2014-05-12
  • 1970-01-01
  • 1970-01-01
  • 2017-04-21
相关资源
最近更新 更多