applescript - 从 csv 文件填充 iTunes 播放列表

标签 applescript itunes

任何人都可以提供一种方法来使用格式如下的 csv 文件/文本文件中的歌曲填充我的播放列表:
歌名,艺人?
我可以单独为标题做,但不能指定它必须有某个艺术家。

编辑:这是我如何通过标题获取它们的示例:

set TheFile to read file "Macintosh HD:Applications:Automator stuff:01b iTunes Scripts:SongList.txt"
tell application "iTunes"
    set thePlaylist to playlist "SongList"
    try
        delete every track of thePlaylist
    end try
    set MySongs to paragraphs of (TheFile) -- read artist names (separated by newlines) from the file
    repeat with AnItem in MySongs -- get all tracks from each artist
        set AnItem to (contents of AnItem)
        if AnItem is not "" then try -- don't bother with empty names
            set MyTracks to (location of file tracks of playlist "Music" whose name is AnItem)
            --can also modify the above from "is" to "contains" or "_begins with_"
            add MyTracks to thePlaylist
        on error errmess -- oopsie (not found, etc)
            log errmess -- just log it
        end try
    end repeat
end tell

最佳答案

好的,想通了!无法弄清楚如何解决包含逗号的标题(我有几个),所以我最终使用制表符分隔它们。所以,一旦我有了我的制表符分隔文件,这段代码就成功了:

set thisTSVFile to (choose file with prompt "Select the CSV file")
readTabSeparatedValuesFile(thisTSVFile)

set theList to readTabSeparatedValuesFile(thisTSVFile)

tell application "iTunes"
    set myPlaylist to playlist "Test1"
    set sourcePlaylist to playlist "Music"
end tell

repeat with i from 2 to number of items in readTabSeparatedValuesFile(thisTSVFile)
     --gets first column
    set theName to item 1 of item i of theList
     --gets second
    set theArtist to item 2 of item i of theList
    tell application "iTunes"
        duplicate (some track of sourcePlaylist whose name is theName and artist is theArtist) to myPlaylist
    end tell
    delay 0.1
end repeat

on readTabSeparatedValuesFile(thisTSVFile)
    try
        set dataBlob to (every paragraph of (read thisTSVFile))
        set the tableData to {}
        set AppleScript's text item delimiters to tab
        repeat with i from 1 to the count of dataBlob
            set the end of the tableData to (every text item of (item i of dataBlob))
        end repeat
        set AppleScript's text item delimiters to ""
        return tableData
    on error errorMessage number errorNumber
        set AppleScript's text item delimiters to ""
        error errorMessage number errorNumber
    end try
end readTabSeparatedValuesFile

关于applescript - 从 csv 文件填充 iTunes 播放列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49958493/

相关文章:

macos - Sketch App 的 Apple 脚本

iphone - iTunes 商店 - iPhone 应用程序...可定制背景?

rss - 如何获取 RSS 链接以在 iTunes/WinAmp/etc 中自动打开

applescript - 尝试从OS X消息接收带有AppleScript的消息

Applescript 错误无法生成 unicode 文本

macos - 如何预定义 Apple Script 常量或变量

ios - 如何添加 "Get it on iTunes"按钮

app-store - iTunes搜索JSON API返回403在服务器上被禁止

ios - iTunes : App Upload Warning : The app references non-public selectors in :setRefreshInterval

macos - Applescript 获取正在运行的应用程序列表?