Чтение текстов с itune для создания файла - PullRequest
0 голосов
/ 04 августа 2010

Я получил подсказку от http://www.dougscripts.com/itunes/, чтобы придумать следующий код. Но для этого нужно гораздо больше улучшений.

  • Я получил ошибку, когда закончил генерацию файла, http://a.imageshack.us/img831/1610/screenshot20100804at113l.png, что не так?
  • Мне не нужно использовать «TextEdit» для хранения файла, есть ли лучший способ просто сохранить информацию в файл?
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

Ответы [ 2 ]

2 голосов
/ 05 августа 2010

Вот две подпрограммы, которые я использую для управления записью данных в текстовый файл:

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
1 голос
/ 05 августа 2010

у вас только что были некоторые вещи вне области видимости, все закрытие открытия и запись текстового файла должны выполняться за пределами 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)
...