0

编写一个脚本来帮助我保持我的播放列表在计算机之间同步。

我想我会通过applescript来做。

前半部分是导出到 m3u,这是我遇到的问题。

代码是:

property delimiter_character : " - "

tell application "iTunes"

set this_playlist to playlist "Alternative Mixtape"
set this_name to (the name of this_playlist) as string

set the playlist_count to the count of tracks of this_playlist
set playlist_data to {}
tell this_playlist
    repeat with i from 1 to the count of tracks
        tell track i
            set the end of the playlist_data to {name, delimiter_character, artist, return, location, return}
        end tell
    end repeat
end tell

end tell

set FileName to "Path:To:File.m3u"
set theFile to open for access FileName with write permission
write playlist_data to theFile
close access theFile

问题是我得到各种“乱码”输出:

listlistutxt Hips Of The Yearutxt - utxtMistutxt
alisvvHDD…ÏXËH+Ï›Hips Of The Year.mp3χ»g∏mMp3 hookˇˇˇˇ Bye Bye…Ï<»»gúMϛϋ’.HDD:Music:Mist:Bye Bye:Hips Of The Year.mp3*Hips Of The Year.mp3HDD(/Music/Mist/Bye Bye/Hips Of The Year.mp3

我尝试将剪贴板转换为纯文本,但在尝试复制为 UTF8 类或记录时不断出现错误。

4

1 回答 1

0

m3u 是一个文本文件。您的问题是在您的代码中 playlist_data 被创建为列表。它实际上是一个更复杂的列表列表。所以你正在将一个列表作为文本写入文件......这就是它被搞砸的原因。试试这个代码。它将 playlist_data 创建为文本而不是列表,以便正确写入文件。我也做了一些其他的优化。我希望它有所帮助。

注意:您必须将 playlistName 和 filePath 更改为您的值。

property delimiter_character : " - "

set playlistName to "CD 01"
set filePath to (path to desktop as text) & "cd01.txt"

tell application "iTunes"
    set theTracks to tracks of playlist playlistName

    set playlist_data to ""
    repeat with aTrack in theTracks
        tell aTrack
            set trackName to name
            set trackArtist to artist
            set trackLocation to location
        end tell
        set playlist_data to playlist_data & trackName & delimiter_character & trackArtist & return & trackLocation & return & return
    end repeat
end tell

try
    set theFile to open for access file filePath with write permission
    write playlist_data to theFile
    close access theFile
on error
    close access file filePath
end try

最后要注意的一件事。您也可以将 playlist_data 列表写入文件。您必须告诉 write 语句将数据作为列表写入这一行“将 playlist_data 作为列表写入文件”。您没有在该语句的“as”部分中指定任何内容,因此它执行将文件写入文本的默认行为。但是您可以根据需要指定“列表”。您会注意到,如果这样做,您将无法使用文本编辑器读取文件,但优点是您可以稍后将该文件“作为列表”读回 AppleScript 并以列表格式取回数据. 不过,这不适合您编写 m3u 文件的任务。

于 2011-11-01T21:21:20.863 回答