Вот две подпрограммы, которые я использую для управления записью данных в текстовый файл:
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