2011-11-01 101 views
0

编写脚本以帮助我保持我的播放列表在计算机之间保持同步。通过Applescript粘贴剪贴板时出错

我想我会通过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或作为记录时出现错误。

回答

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列表写入文件。你必须告诉写入语句将数据写入列表中“将playlist_data写入文件列表”。您没有在该语句的“as”部分中指定任何内容,因此它会将文件编写为文本的默认行为。但是你可以指定“列表”,如果你想。你会注意到,如果你这样做,你将无法使用文本编辑器读取文件,但优点是你可以稍后将该文件“以列表形式”读回到applescript中,并以列表格式获取数据。这不适合你编写m3u文件的任务。

+0

非常感谢你,完美的作品。 我只需要运行一个grep就可以将它从posix转换为unix文件路径,我们很好。 **谢谢** – MrSunshine

+0

不客气。如果你想在文件中使用unix风格的路径,那么将一行更改为...将trackLocation设置为(获取位置)的POSIX路径。 – regulus6633