【问题标题】:Populate iTunes playlist from csv file从 csv 文件填充 iTunes 播放列表
【发布时间】:2018-10-02 03:58:42
【问题描述】:

谁能提供一种方法来用来自 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

【问题讨论】:

  • 向我们展示你是如何为标题做的。

标签: applescript itunes


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    您可以将 ObjectiveC(或者也可能是 Swift)与 XCode 一起使用来完成繁重的工作(解析文件),然后从那里点击 iTunes,尽管它可能比通过其脚本菜单在 iTunes 进程中运行要慢很多。

    这里有一些获取当前曲目标题的 ObjectiveC 代码;您可以调整该方法以适应更复杂的脚本,例如填充播放列表。

    +(NSString *)getTitle {
        return [self runAppleScriptAndReturnResult:@"Tell application \"iTunes\" \nreturn the name of the current track\nend tell"];
    }
    
    +(NSString *)runAppleScriptAndReturnResult:(NSString*)script {
        NSAppleScript *appleScript=[[NSAppleScript alloc] initWithSource:[NSString stringWithFormat:@"with timeout of 3 seconds\n%@\nend timeout\n", script]];
        return [[appleScript executeAndReturnError:nil] stringValue];
    }
    

    【讨论】:

    • 谢谢,查理。我对 Objective-C 一无所知,所以可能会坚持基于 AppleScript 的想法。
    猜你喜欢
    • 2018-08-19
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    • 1970-01-01
    • 2019-08-16
    • 2011-09-25
    相关资源
    最近更新 更多