从itunes读取歌词并制作文件

发布于 2024-09-12 16:31:33 字数 1084 浏览 3 评论 0原文

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

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 ""
 set summaryFile to ((path to desktop as Unicode text) & "songs2.txt")
 set ff to open for access file summaryFile with write permission
 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
   tell application "TextEdit"
    activate
    set myVar to art & nom & the_lyrics
    write myVar to ff starting at eof
   end tell
  else
   beep
  end if
 end repeat
end tell

I got hints from http://www.dougscripts.com/itunes/ to come up with the following code. But it needs much more improvements.

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 ""
 set summaryFile to ((path to desktop as Unicode text) & "songs2.txt")
 set ff to open for access file summaryFile with write permission
 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
   tell application "TextEdit"
    activate
    set myVar to art & nom & the_lyrics
    write myVar to ff starting at eof
   end tell
  else
   beep
  end if
 end repeat
end tell

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巴黎夜雨 2024-09-19 16:31:33

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

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 在 Essential Subroutines 中来自他们自己的旧 Applescript 站点的代码。 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

Here are two subroutines I use to manage writing data to a text file:

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    

Please note write_to_file() is Apple's code from their own old Applescript site in Essential Subroutines. WriteToLog() is my own subroutine that wrote to interface with write_to_file with only one argument, the text itself. Add salt to taste. To rewrite the script accomodate the above subroutines, I would do it like this:

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
揽清风入怀 2024-09-19 16:31:33

你只是有一些超出范围的事情,所有文本文件的打开、关闭和写入都必须在 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)

you just had some things out of scope all the opening closing and writing of the text file must be done outside of itunes and texedit is not needed

 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文