通过Applescript粘贴剪贴板时出错
编写脚本以帮助我保持播放列表与计算机之间的同步 我想我应该通过applescript来做 前半部分是向m3u的导出,这正是我要解决的问题 代码是:通过Applescript粘贴剪贴板时出错,applescript,clipboard,playlist,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
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
问题是我得到了各种各样的“乱码”输出:
listListUxt年度最佳影片Utxt-utxtMistutxt
alisvvHDD…年度最佳髋关节。mp3‡ËH+Ï›g∏mMp3 hookˇBye Bye…Ïm3u是一个文本文件。您的问题在您的代码中,播放列表数据被创建为列表。事实上,这是一个更复杂的列表列表。因此,您正在将列表作为文本写入文件。。。这就是它被搞砸的原因。试试这个代码。它将播放列表_数据创建为文本而不是列表,以便正确写入文件。我还做了一些其他的优化。我希望有帮助
注意:您必须将播放名和文件路径更改为您的值
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
最后要注意一件事。您也可以将播放列表\数据列表写入文件。您必须告诉write语句将数据作为列表写入此行“将播放列表\数据写入文件作为列表”。您没有在该语句的“as”部分指定任何内容,因此它执行将文件作为文本写入的默认行为。但如果需要,可以指定“列表”。您会注意到,如果这样做,您将无法使用文本编辑器读取该文件,但其优点是,您可以稍后将该文件“作为列表”读回applescript,并以列表格式获取数据。不过,这不适合您编写m3u文件。非常感谢,非常适合。我只需要对它运行grep,就可以将它从posix文件路径转换到unix文件路径,我们就可以开始了。谢谢,不客气。如果要在文件中使用unix样式的路径,请将这一行更改为。。。将trackLocation设置为的POSIX路径(获取位置)。
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