【问题标题】:Reading lyrics from itune to make a file从 iTunes 中读取歌词以制作文件
【发布时间】:2010-08-04 16:31:09
【问题描述】:

我从http://www.dougscripts.com/itunes/ 那里得到了提示,我想出了以下代码。但它需要更多改进。

告诉应用程序“iTunes” 如果播放器状态正在播放,那么 将 sel 设置为当前曲目作为列表 否则,如果选择不是 {} 那么 将 sel 设置为选择 万一 将 noSong 设置为 "" 将summaryFile设置为((作为Unicode文本的桌面路径)&“songs2.txt”) 设置 ff 以打开访问文件 summaryFile 具有写权限 在 sel 中使用 this_track 重复 将 the_lyrics 设置为 this_track 的歌词 将 {art, nom} 设置为 {this_track 的艺术家,this_track 的名称} 如果 the_lyrics 不是 "" 那么 告诉应用程序“TextEdit” 启用 将 myVar 设置为 art & nom & the_lyrics 从 eof 开始将 myVar 写入 ff 结束告诉 别的 嘟 万一 结束重复 结束告诉

【问题讨论】:

    标签: applescript itunes


    【解决方案1】:

    这是我用来管理将数据写入文本文件的两个子例程:

    on WriteLog(the_text)
            set this_story to the_text
            set this_file to (((path to desktop folder) as text) & "MY STORY")
            my write_to_file(this_story, this_file, true)
    end WriteLog
    
    on write_to_file(this_data, target_file, append_data)
        try
            set the target_file to the target_file as text
            set the open_target_file to ¬
                open for access file target_file with write permission
            if append_data is false then ¬
                set eof of the open_target_file to 0
            write this_data to the open_target_file starting at eof
            close access the open_target_file
            return true
        on error
            try
                close access file target_file
            end try
            return false
        end try
    end write_to_file    
    

    请注意 write_to_file() 是 Apple 的代码,来自他们自己的旧 Applescript 站点的 Essential Subroutines。 WriteToLog() 是我自己的子例程,它写入与 write_to_file 的接口,只有一个参数,即文本本身。加盐调味。要重写脚本以适应上述子例程,我会这样做:

    tell application "iTunes"
        if player state is playing then
            set sel to current track as list
        else if selection is not {} then
            set sel to selection
        end if
    
        set noSong to ""
        repeat with this_track in sel
            set the_lyrics to this_track's lyrics
            set {art, nom} to {this_track's artist, this_track's name}
            if the_lyrics is not "" then
                set myVar to art & nom & the_lyrics
                my WriteLog(myVar)
            else
                beep
            end if
        end repeat
    end tell
    

    【讨论】:

    • 感谢脚本,WriteToLog() 应该是 WriteLog(),没有它,它工作正常。
    • 哎呀!固定的。感谢您了解这一点。
    【解决方案2】:

    你只是有一些超出范围的东西所有的打开关闭和文本文件的写入都必须在 iTunes 之外完成,并且不需要 texedit

     set summaryFile to ((path to desktop as Unicode text) & "songs2.txt")
     try
        close access (summaryFile as alias) -- close the file if its open alreayd
     end try
     set ff to open for access file summaryFile with write permission
     tell application "iTunes"
        if player state is playing then
            set sel to current track as list
        else if selection is not {} then
            set sel to selection
        end if
    
        set noSong to ""
        repeat with this_track in sel
            set the_lyrics to lyrics of this_track
            set {art, nom} to {artist of this_track, name of this_track}
            if the_lyrics is not "" then
                tell me
                    set myVar to art & nom & the_lyrics
                    write myVar to ff starting at eof
                end tell
            else
                beep
            end if
        end repeat
     end tell
     close access (summaryFile as alias)
    

    【讨论】:

      猜你喜欢
      • 2019-12-29
      • 2021-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-14
      • 2017-04-15
      • 1970-01-01
      相关资源
      最近更新 更多