【问题标题】:applescript transforming a list into a txt fileapplescript将列表转换为txt文件
【发布时间】:2013-07-03 22:49:58
【问题描述】:

我正在尝试将我的 iTunes 中的所有歌曲名称写入 txt 文档。我遇到的第一个问题是我似乎无法正确循环操作。这是我的 iTunes 中前 15 首歌曲的测试用例:

tell application "TextEdit"  
    make new document  
end tell  
tell application "iTunes"  
    set trNameID1 to name of track 1  
    set trNameID2 to name of track 2  
    set trNameID3 to name of track 3  
    set trNameID4 to name of track 4  
    set trNameID5 to name of track 5  
    set trNameID6 to name of track 6  
    set trNameID7 to name of track 7  
    set trNameID8 to name of track 8  
    set trNameID9 to name of track 9  
    set trNameID10 to name of track 10  
    set trNameID11 to name of track 11  
    set trNameID12 to name of track 12  
    set trNameID13 to name of track 13  
    set trNameID14 to name of track 14  
    set trNameID15 to name of track 15  
    tell application "TextEdit"  
        set text of document 1 to {trNameID1 & "  
", trNameID2 & "  
", trNameID3 & "  
", trNameID4 & "  
", trNameID5 & "  
", trNameID6 & "  
", trNameID7 & "  
", trNameID8 & "  
", trNameID9 & "  
", trNameID10 & "  
", trNameID11 & "  
", trNameID12 & "  
", trNameID13 & "  
", trNameID14 & "  
", trNameID15} as text  
    end tell  
end tell

当我尝试循环播放时,txt文档只包含最后的歌曲名称,例如:

tell application "TextEdit"  
    make new document
end tell
tell application "iTunes"
    set trNum to 1
    repeat 15 times
        set trNameID to name of track (trNum)
        tell application "TextEdit"
            set text of document 1 to trNameID & "
"
        end tell
    end repeat
end tell

这只会将第十五首歌曲的名称输出到 txt 文档中。

我意识到这可能是非常基本的,但实际上我已经使用 applescript 大约 48 小时了,我似乎无法弄清楚这一点。我希望所有歌曲名称都在一个 txt 文档中,这样我就可以阅读和分析 C++ 中的字符串。有人有什么想法吗?

另外,我不确定在 AppleScript 中是否有办法查看整个 iTunes 库并查看最后一首歌曲,在 iTunes 中记录该歌曲的 id,然后重复循环遍历该 id .这样,循环将完全适用于库中的歌曲数量。

任何想法都将不胜感激!

【问题讨论】:

    标签: applescript itunes-sdk


    【解决方案1】:

    您根本不需要重复循环。您可以直接从 iTunes 获取曲目名称。您以列表格式获取它,因此我们只需将该列表转换为使用返回字符分隔列表项的字符串。然后我们将其写入 TextEdit。所以这段代码优化了@Michele Percich 的代码,消除了重复循环并使用applescript 的文本项分隔符将列表转换为字符串以供在TextEdit 中使用。

    tell application "iTunes"
        set trackNames to name of every track in (first playlist whose special kind is Music)
    end tell
    
    set text item delimiters to return
    set trackNames to trackNames as text
    set text item delimiters to ""
    
    tell application "TextEdit"
        make new document
        set text of document 1 to trackNames
    end tell
    

    【讨论】:

      【解决方案2】:

      您需要在重复循环结束时增加 trNum 变量的值:

      set trNum to trNum + 1
      

      或者更好地使用不同的repeat 语法:

      repeat with trNum from 1 to 15
      

      还要在文档中添加(而不是替换)曲目名称:

      set text of document 1 to text of document 1 & trNameID & return
      

      但是,这可能是做你想做的更好的方法:

      tell application "iTunes"
          set trackList to ""
          set allTracks to every track in (first playlist whose special kind is Music)
          repeat with currentTrack in allTracks
              set trNameID to name of currentTrack
              set trackList to trackList & trNameID & return
          end repeat
      end tell
      tell application "TextEdit"
          make new document
          set text of document 1 to trackList
      end tell
      

      【讨论】:

        【解决方案3】:

        我看到你们都在使用:

        告诉应用程序“TextEdit”

        制作新文件

        将文档 1 的文本设置为 trackNames

        说完

        命令 您可以使用更快的方式:

        将文本位置设置为“/users/yourusername/desktop/test.txt”

        将 Line_1 设置为“您好,这是第一行,如果您想要更多行,只需复制 > 此脚本并更改变量。”

        执行 shell 脚本 "echo " & Line_1 的引用形式 & " >> " & textlocation 的引用形式

        您可以在脚本中看到 2 个“>>”符号,这会将每个文本行添加到 txt 文件的新行中。 如果只有一个“>”,则该文本将替换其他文本。

        这是一个例子:

        首先有 2 个“>>”行

        do shell script "echo Hey this is one line. >> /Users/Yourusername/desktop/Add.txt"

        do shell script "echo 这是第二个。>> /Users/Yourusername/desktop/Add.txt"

        这个脚本会生成一个这样的txt文件:

        嘿,这是一行。

        这是第二个。

        现在有 2 行“>”

        do shell script "echo Hey this is one line > /Users/Zl109819/desktop/Add.txt"

        do shell script "echo 这是第二个 > /Users/Zl109819/desktop/Add.txt"

        这个脚本会生成一个这样的txt文件:

        这是第二个。

        【讨论】: