【问题标题】:Error when pasting clipboard via Applescript通过 Applescript 粘贴剪贴板时出错
【发布时间】:2011-11-02 05:21:20
【问题描述】:

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

我想我会通过 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 类或记录时一直出错。

【问题讨论】:

    标签: applescript clipboard playlist


    【解决方案1】:

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

    【讨论】:

    • 非常感谢,完美运行。我只需要对其运行 grep 即可将其从 posix 文件路径转换为 ​​unix 文件路径,我们就可以开始了。 谢谢
    • 不客气。如果您想要文件中的 unix 样式路径,请将一行更改为此...将 trackLocation 设置为(获取位置)的 POSIX 路径。
    猜你喜欢
    • 1970-01-01
    • 2017-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-30
    • 2017-12-23
    • 2015-07-29
    • 2013-07-08
    相关资源
    最近更新 更多