通过Applescript粘贴剪贴板时出错

时间:2011-11-01 18:47:09

标签: applescript clipboard playlist

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

我想我会通过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或记录时,我一直收到错误。

1 个答案:

答案 0 :(得分:0)

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

注意:您必须将播放列表名称和文件路径更改为您的值。

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文件的任务。

相关问题