2010-08-04 24 views
0

我從http://www.dougscripts.com/itunes/得到了提示,想出下面的代碼。但它需要更多的改進。從itune中讀取歌詞來製作一個文件

 
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 

回答

1

你只是有一些事情超出範圍的文本文件中的所有開閉和寫作必須的iTunes之外完成,不需要

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) 
2

這裏texedit是兩個子程序我用於寫入數據管理到一個文本文件:

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()是蘋果公司從自己的老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 
+0

謝謝你的腳本,WriteToLog()應該已經WRITELOG(),如果沒有它,它工作正常。 – prosseek 2010-08-05 18:55:43

+0

哎呀!固定。感謝您的支持。 – 2010-08-05 19:33:14

相關問題